Skip to content

Instantly share code, notes, and snippets.

@AlanGabbianelli
Last active July 27, 2023 12:19
Show Gist options
  • Save AlanGabbianelli/b083d94380b2c6a4f8d382e326b78cdf to your computer and use it in GitHub Desktop.
Save AlanGabbianelli/b083d94380b2c6a4f8d382e326b78cdf to your computer and use it in GitHub Desktop.
Associate various file extensions with a specified application on macOS

Purpose

This script is designed to associate various file extensions with a specified application on macOS. By default, it associates the file extensions with Visual Studio Code - Insiders (because that's what I use).

Dependencies

  • Homebrew
  • duti
  • The application to associate the file extensions with

Usage

  • To use the default editor (Visual Studio Code - Insiders), run:
    $ associate_file_extensions.sh
    
    # or to avoid downloading it:
    $ /bin/bash -c "$(curl -L https://bit.ly/AssociateFileExtensionsScript)"
  • To use a different editor, run:
    $ associate_file_extensions.sh <app_name>
    where <app_name> is the name of an editor of your choice.

How it works

The script defines a few functions for printing colored text to the terminal. It then sets the HOMEBREW_NO_AUTO_UPDATE environment variable to prevent brew from auto-updating during installation. It installs duti via brew install duti if it's not already installed.

The script defines an array of file extensions to associate with the specified application. For each extension, it runs a duti command that associates the extension with the specified application.

The duti command has this syntax: duti -s <app_id> <extension> all, where <app_id> is the ID of the application to associate the file extension with, and <extension> is the file extension to associate. The all flag is used to associate the extension with all possible file types (e.g., public.json, public.text, etc.).

The script prints a yellow message to the terminal before running each duti command to let the user know what command is being executed. If the command is successful, it prints a green message. If the command fails, it prints a red error message.

Notes

To associate additional file extensions with the specified application, they can be added to the extensions array.

Credits

Highly inspired by Darragh ORiordan's article Set vscode as the default editor for text files on mac.

#!/bin/bash
# Highly inspired by Darragh ORiordan's article "Set vscode as the default editor for text files on mac"
# See https://www.darraghoriordan.com/2021/09/15/vscode-default-text-files-mac/
function print_green {
printf "\e[32m$1\e[0m\n"
}
function print_yellow {
printf "\e[33m$1\e[0m\n"
}
function print_red {
printf "\e[31m$1\e[0m\n"
}
export HOMEBREW_NO_AUTO_UPDATE=1
brew ls duti &>/dev/null || brew install duti # https://github.com/moretension/duti/
app_name=${@:-Visual Studio Code - Insiders} # https://code.visualstudio.com/insiders/
app_id=$(osascript -e "id of app \"${app_name}\"")
extensions=(
"public.json"
"public.plain-text"
"public.python-script"
"public.shell-script"
"public.source-code"
"public.text"
"public.unix-executable"
"public.data"
".c"
".cpp"
".cs"
".css"
".go"
".java"
".js"
".sass"
".scss"
".less"
".vue"
".cfg"
".json"
".jsx"
".log"
".lua"
".md"
".php"
".pl"
".py"
".rb"
".ts"
".tsx"
".txt"
".conf"
".yaml"
".yml"
".toml"
".html"
".css"
)
for extension in "${extensions[@]}"; do
duti_command="duti -s ${app_id} ${extension} all"
print_yellow "Running ${duti_command}"
if ${duti_command}; then
print_green "Associated ${extension} files with ${app_name}"
else
print_red "🚨 Error associating ${extension} files with ${app_name} 🚨"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment