How to Automate Changing the Default Open-With Application for Specific Files

Hey Folks,

Most everyone knows how to change a file association on the macOS using the Finder's UI.

image

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:


To set default apps for file types in en mass see the command line tool duti.


Hex Editors recommended for research:


-Chris

4 Likes

Great exposition Chris - taught me a lot. Thanks :blush:

1 Like

Hello Chris (@ccstone)

Thanks for posting this great exposure - very helpful.

Speaking of changes en mass using duti - of course is this an option.

But my thoughts are what to do if you did something wrong selecting the files before running batch changing workflow ?

This is no criticism here - just me thinking out loud….

My advice of creating a macro based on your exposure here would be creating something that does the reading and comparing in KM‘s native way storing every application‘s hex code inside the workflow and let the change to the desired app with all Parameters with AppleScript in a for each loop built with KM actions - this way you can pick easily up any single file which was changed by mistake and revert this file‘s change using the Finder.

What do you think ?

Greetings from Germany

Tobias

No AppleScript is needed for this, particularly since the heavy lifting is done via shell script.


Macro to Extract the OpenWith x-Attribute from the file selected in the Finder.


  • Manually adjust the desired default app of a file in the Finder.
  • Select the file.
  • Run the macro.
  • Use the captured Hex in this macros's companion macro.

image


Download: Read OpenWith X-Attribute of Selected File as Hex v1.00.kmmacros (6.7 KB)

Macro-Image

Keyboard Maestro Export

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2




Companion Macro to Write an Extracted x-Attribute to Selected Files in the Finder – i.e. Change the File's Associated Open-With app.


  • The current OpenWith configuration is Drafts.app.
    • Users can employ the companion macro to change the designated app.
  • Select the files you want to adjust in the Finder.
  • Run the macro.

Download: Write OpenWith X-Attribute of Selected File v1.00.kmmacros (8.1 KB)

Macro-Image

Keyboard Maestro Export

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

The proposed method is akin to hammering away at the flea with a tank engine. Using AppleScript doesn't necessarily command that GUI scripting is the only recourse. The simplest solution is to use the default application property of the file or alias in the System Events object model, like so:

tell application "System Events" to set default application of file _HFS_path_to_file to file _HFS_path_to_application

Alias can be put instead of file. You then use the code in any context requiring automation, e.g., create a folder action where every file added to the folder is set to open with the designated application.

1 Like