Hey Folks,
Most everyone knows how to change a file association on the macOS using the Finder's UI.
Here you can change the default app for a single file or for all files of a given type.
But how can you automate that? Using UI-Scripting is complicated and slow...
It would seem that for single file associations the macOS writes an x-Attribute to the file:
com.apple.LaunchServices.OpenWith:
00000000 62 70 6C 69 73 74 30 30 D3 01 02 03 04 05 06 57 |bplist00.......W|
00000010 76 65 72 73 69 6F 6E 54 70 61 74 68 5F 10 10 62 |versionTpath_..b|
00000020 75 6E 64 6C 65 69 64 65 6E 74 69 66 69 65 72 10 |undleidentifier.|
00000030 00 5F 10 18 2F 41 70 70 6C 69 63 61 74 69 6F 6E |._../Application|
00000040 73 2F 44 72 61 66 74 73 2E 61 70 70 5F 10 1C 63 |s/Drafts.app_..c|
00000050 6F 6D 2E 61 67 69 6C 65 74 6F 72 74 6F 69 73 65 |om.agiletortoise|
00000060 2E 44 72 61 66 74 73 2D 4F 53 58 08 0F 17 1C 2F |.Drafts-OSX..../|
00000070 31 4C 00 00 00 00 00 00 01 01 00 00 00 00 00 00 |1L..............|
00000080 00 07 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000090 00 6B |.k|
00000092
My default text editor is BBEdit, and I've obviously set the above file to Drafts.
To prepare for automating the process.
- Change the default-app of an applicable file to the desired app manually.
- Read the OpenWith hex string using:
xattr -px com.apple.LaunchServices.OpenWith ~/path-to-file
- Compose an
xattr
command the hex string.
This shell script assigns the given file a default app of Drafts.app:
xattr -w -x com.apple.LaunchServices.OpenWith '62706C69 73743030 D3010203 04050657 76657273 696F6E54 70617468 5F101062 756E646C 65696465 6E746966 69657210 005F1018 2F417070 6C696361 74696F6E 732F4472 61667473 2E617070 5F101C63 6F6D2E61 67696C65 746F7274 6F697365 2E447261 6674732D 4F535808 0F171C2F 314C0000 00000000 01010000 00000000 00070000 00000000 00000000 00000000 006B' ~/Downloads/test-xattr.txt
It ain't pretty, but it works well enough and is way better than having to do the job via the macOS UI.
I leave the composition of a macro up to the user for the time being.
References:
- macos - Change file association in terminal? - Ask Different
- applescript - Change the default application (for a file extension) via script/command line? - Ask Different
- objective c - How to set default application for specific file types in Mac OS X? - Stack Overflow
To set default apps for file types in en mass see the command line tool duti
.
- duti man page
- Also available via MacPorts and Homebrew.
Hex Editors recommended for research:
-
Hex Fiend
- Able to open x-Attributes directly.
-
Synalyze It!
- Also available on the Apple App-Store.
- A commercial (Pro) version is also available.
-Chris