Created
March 11, 2025 21:14
-
-
Save ws/d3529ff975d7b6994a8e5137e49819c6 to your computer and use it in GitHub Desktop.
Tell MacOS to use VScode for all code-like file extensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script sets Visual Studio Code as the default application for various code-related file extensions | |
# using `duti`. If `duti` is not installed, the script prompts the user to install it via Homebrew. | |
# | |
# Due to macOS system restrictions, setting .html as the default may fail if Chrome, VS Code, or other editors are open. | |
# In such cases, the script will notify the user to close those apps and retry. | |
# Check if duti is installed | |
if ! command -v duti &> /dev/null; then | |
echo "duti is not installed." | |
read -p "Would you like to install it? (Y/n) " response | |
response=$(echo "$response" | tr '[:upper:]' '[:lower:]') # Convert input to lowercase | |
if [[ -z "$response" || "$response" == "y" || "$response" == "yes" ]]; then | |
HOMEBREW_NO_AUTO_UPDATE=1 brew install duti | |
if ! command -v duti &> /dev/null; then | |
echo "Failed to install duti. Please install it manually." | |
exit 1 | |
fi | |
else | |
echo "duti is required to proceed. Exiting." | |
exit 1 | |
fi | |
fi | |
# Set VS Code as the default for code-related extensions (excluding .html) | |
EXTENSIONS=( | |
js ts jsx tsx css scss less json yaml yml xml md sh py c cpp h hpp java go rs php sql lua toml ini dockerfile gitignore env | |
) | |
SUCCESSFUL_EXTENSIONS=() | |
for ext in "${EXTENSIONS[@]}"; do | |
if duti -s com.microsoft.VSCode .$ext all; then | |
SUCCESSFUL_EXTENSIONS+=("$ext") | |
fi | |
done | |
# Attempt to set .html separately with a failure warning | |
if duti -s com.microsoft.VSCode .html all; then | |
SUCCESSFUL_EXTENSIONS+=("html") | |
else | |
echo -e "⚠️ Warning: Failed to set Visual Studio Code as the default for .html files." | |
echo "This can happen if Chrome, VS Code, or other editors are open." | |
echo "Close those applications and try running the script again." | |
fi | |
# Print final success message with a bullet-point list | |
echo "✅ Successfully set Visual Studio Code as the default for:" | |
for ext in "${SUCCESSFUL_EXTENSIONS[@]}"; do | |
echo " • .$ext" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment