Instant setup of familiar file associations

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

First, the problem. Files of the necessary extensions are often not opened by default by anything, and if they are opened, then by some iTunes. Under windows, the necessary associations are sometimes completely lost when installing (or even uninstalling) programs: you demolished GIMP, and ico files were taken over from the usual file viewer to the standard Photo Gallery. Why? For what? It is not known… What if you found a new editor or, for various reasons, a fresh installation? What if there is more than one computer? In general, clicking with mice in dialogues is such entertainment.

Instead, I saved two files to Dropbox and now you can bring the computer world back to its usual state almost instantly. And what have you been waiting for for so many years… Here is the recipe for Windows and macOS.

Windows

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

ftype my_file_txt="C:Windowsnotepad.exe" "%1"
assoc .txt=my_file_txt

Changes take effect immediately. Despite the fact that the association is registered for a specific user, for some reason you need to run these commands from under the administrator. And don't forget to double the percent symbol (%%1) when running from a .bat file. The Magical World of Windows 7 Ultimate 64-bit…

MacOS

In macros, it is convenient to set associations with the utility duti. It is installed via brew install duti. Usage example:

duti -s com.apple.TextEdit .txt "editor"

The changes take effect immediately, no sudo is required. Here, the “com.apple.TextEdit” argument is the so-called “bundle id” of the program we need. The "editor" argument is the association type: "editor" for editing, "viewer" for viewing, "all" for everything.

You can find the “bundle id” like this: if there is “/Applications/Sublime Text.app” of the third version, then the bundle id will be “com.sublimetext.3”, 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)XnViewxnview.exe
set SUBLIME=C:Program FilesSublime Text 3sublime_text.exe
set FOOBAR=C:Program Files (x86)foobar2000foobar2000.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 """ || 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" "." "$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: habr.com

Add a comment