Instantly customize familiar file associations
Automated the task of file associations, that is, the choice of the program that will open the file from Explorer / Finder. And sharing.
First, the problems. Files of the required extensions often do not open by default with anything, and if they open, then with some iTunes. Under windows, the necessary associations, it happens, are completely lost when installing (or even uninstalling) programs: you demolish, it used to be, GIMP, and ico-files are transferred from the usual file viewer to the standard Photo Gallery. Why? What for? It is unknown ... And if you find a new editor or, for various reasons, a fresh installation? And if the computer is not alone? In general, clicking with the mouse in dialogs is such an entertainment.
Instead, I saved two files on Dropbox and now I can bring the computer world to its familiar state almost instantly. And what has been waiting for so many years ... Next is the recipe for Windows and macOS.
Windows
In the Windows console, cmd.exe
this is done in two stages:
ftype my_file_txt="C:\Windows\notepad.exe" "%1"
assoc .txt=my_file_txt
Changes take effect immediately. Despite the fact that the association is registered for the current user, you need to run these commands for some reason with administrator rights.And do not forget to double the percent symbol (%% 1) when starting from a bat-file. The magical world of Windows 7 Ultimate 64-bit ...
UPD The experiments showed that manipulations with ftype / assoc affect all users on the machine (our editorial office did not expect such a turn). However, the scheme remains operational. But I will find out how not to affect the rest.
macOS
In makoshi, associations are convenient to set with the duti utility . It is installed through brew install duti
. Usage example:
duti -s com.apple.TextEdit .txt "editor"
Changes take effect immediately; sudo is not required. Here the argument "com.apple.TextEdit" is the so-called "bundle id" of the program we need. The argument "editor" is the type of association: "editor" for editing, "viewer" for viewing, "all" for everything.
You can find the "bundle id" like this: if there is a "/ Applications / Sublime Text.app" of the third version, then the idle bundle will have "com.sublimetext.3", well, or some other:
> osascript -e 'id of app "Sublime Text"'
com.sublimetext.3
Tested 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 :eof
Final 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 ac3