📜 ⬆️ ⬇️

Instant customization of familiar file associations

Automated setting of file associations, that is, the choice of the program that will open the file from Explorer / Finder. And share.


First, the problematic. Files of the necessary extensions often do not open by default with anything, and if opened, then some iTunes. Under windows, the necessary associations, it happens, are completely lost during the installation (or even uninstallation) of the programs: you demolish GIMP, and ico-files passed from the usual file viewer to the standard Photo Gallery. Why? What for? Unknown ... And if you found a new editor or, for various reasons, a fresh installation? And if the computer is not alone? In general, clicking the mouse in the dialogues - the entertainment is currently.


Instead, I saved two files on Dropbox and now the computer world can be brought back to its usual state almost instantly. And what waited for so many years ... Next, the recipe for Windows and macOS.


Windows


In the Windows cmd.exe console, this is done in two steps:


 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 prescribed for the current user, for some reason you need to run these commands 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. Experiments have shown that manipulations with ftype / assoc affect all users on the machine (our editorial team did not expect such a turn). However, the scheme remains working. But I will find out how not to touch the rest.


macOS


In the makos association it is convenient to set the utility duti . It is installed through brew install duti . Example of use:


 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 an association type: "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 it will have a bundle of "com.sublimetext.3", or some other:


 > osascript -e 'id of app "Sublime Text"' com.sublimetext.3 

Tested on macOS Sierra.


Summary 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 

The 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 

')

Source: https://habr.com/ru/post/449820/


All Articles