I automated the task of file associations, meaning the selection of the program that will open a file from Explorer/Finder. And I'm sharing it.
First, the issue. Files with the necessary extensions often do not open by default with anything, and when they do, it might be something like iTunes. In Windows, necessary associations can sometimes be lost during the installation (or even uninstallation) of programs: you might delete GIMP, and suddenly ico files are taken over from the usual file viewer by the default Photo Gallery. Why? Why? Who knows⦠And what if you found a new editor or, for various reasons, did a fresh install? What if the computer isn't alone? In general, clicking through dialogs isnāt much fun.
Instead, I saved two small files on Dropbox and now restoring the computer world to its familiar state can be done almost instantly. What was I waiting for all these years⦠Next, hereās the recipe for Windows and macOS.
Windows
In the Windows console, cmd.exe this is done in two steps:
ftype my_file_txt="C:\Windows\notepad.exe" "%1"
assoc .txt=my_file_txtThe changes take effect immediately. Although the association is set for a specific user, these commands need to be run as an administrator for some reason. Don't forget to double the percent symbol (%%1) when executing from a bat file. The magical world of Windows 7 Ultimate 64-bitā¦
macOS
In macOS, associations can conveniently be set using the utility. It can be installed via brew install duti. Example usage:
duti -s com.apple.TextEdit .txt "editor"Changes take effect immediately; no sudo is required. Here, the argument "com.apple.TextEdit" is the so-called "bundle id" of the program we need. The argument "editor" specifies the type of association: "editor" for editing, "viewer" for viewing, "all" for everything.
To find the "bundle id", you can do the following: if you have "/Applications/Sublime Text.app" version three, its bundle id will be "com.sublimetext.3", or some other:
> osascript -e 'id of app "Sublime Text"'
com.sublimetext.3Tested on macOS Sierra.
Final script for Windows (.bat)
@echo off
set XNVIEW=C:\Program Files (x86)\XnView\xnview.exe
set SUBLIME=C:\Program Files\Sublime Text 3\sublime_text.exe
set FOOBAR=C:\Program Files (x86)\foobar2000\foobar2000.exe
call :assoc_ext "%SUBLIME%" txt md js json css java sh yaml
call :assoc_ext "%XNVIEW%" png gif jpg jpeg tiff bmp ico
call :assoc_ext "%FOOBAR%" flac fla ape wav mp3 wma m4a ogg ac3
goto :eof
:assoc_ext
set EXE=%1
shift
:loop
if "%1" neq "" (
ftype my_file_%1=%EXE% "%%1"
assoc .%1=my_file_%1
shift
goto :loop
)
goto :eofFinal script for macOS (.sh)
#!/bin/bash
# this allows us terminate the whole process from within a function
trap "exit 1" TERM
export TERM_PID=$$
# check `duti` installed
command -v duti >/dev/null 2>&1 ||
{ echo >&2 "duti required: brew install duti"; exit 1; }
get_bundle_id() {
osascript -e "id of app "${1}"" || kill -s TERM $TERM_PID;
}
assoc() {
bundle_id=$1; shift
role=$1; shift
while [ -n "$1" ]; do
echo "setting file assoc: $bundle_id .$1 $role"
duti -s "$bundle_id" ".${1}" "$role"
shift
done
}
SUBLIME=$(get_bundle_id "Sublime Text")
TEXT_EDIT=$(get_bundle_id "TextEdit")
MPLAYERX=$(get_bundle_id "MPlayerX")
assoc "$SUBLIME" "editor" txt md js jse json reg bat ps1 cfg sh bash yaml
assoc "$MPLAYERX" "viewer" mkv mp4 avi mov webm
assoc "$MPLAYERX" "viewer" flac fla ape wav mp3 wma m4a ogg ac3Source: habr.com
