Setting Default Open With for File Types

I used to use a combination of a terminal utility called Duti and AppleScript called by KM to set my flavor of the day default choice for PDF files depending on what I was doing with them (reading in Preview or serious drawing markup in Revu?) Duti basically turns this into a common line operation.

Unfortunately, Duti no longer seems to work with Sierra (although there are some rather elaborate recompiling options but those are currently beyond my ken). Does anyone have thoughts on how else I might achieve this result? I thought of using KM to Get Info on a currently selected finder file, them work with that. I can get the buttons at the same place regularly by using Moom to relocate the window to upper left of the screen, but I've lost my way after that. I can't seem to get to the Open With selection using a consistent number/type of keystrokes and even once I get there, not all the apps I am interested in using appear in the drop down.

Any pointers would be appreciated.

A couple of options that could be incorporated in a macro:

  1. The AppleScript “open” command will open an object (file) with an option to specify the opening application – this is like the standard Finder “Open With” command – so you could, for example, have a series of macros with the same shortcut – each macro including a small script to tell Finder to open the file with xxx application. By using the same shortcut for each application choice, you would get a conflict menu from which you can choose the app flavor of the day.

  2. For a persistent resetting of the default application you could incorporate an Automator workflow in your macro and use Automator’s “Set Application for Files” command – which has exactly the same result as Finder’s Get Info “Open With xxx > Change All” action.

If you install duti via Homebrew it does work with Sierra. (It seems Homebrew is using a version precompiled for Sierra (duti-1.5.3.sierra.bottle.2.tar.gz).)

  1. Install Homebrew, if not already done.
  2. Install duti: brew install duti

PS:

Alternatively – without any third-party tools – you could execute this as Swift script in KM:

import Foundation

LSSetDefaultRoleHandlerForContentType("com.adobe.pdf" as CFString, LSRolesMask.all, "com.apple.preview" as CFString)

Replace the BundleIdentifier (com.apple.preview in the example script) according to your needs.

Note: You can quickly get the BundleIdentifier of any app by executing this in the Terminal (example):

osascript -e 'id of app "TextEdit"'

Edit (2017-01-07):

Here is a little macro to get the Bundle ID of an app.

3 Likes

I don’t think most people know what a “bundle identifier” is, nor do they know how to find the bundle identifier for their desired application. Perhaps you could clarify?

And then once you do that, how about making a macro with this piece of code in it along with instructions on how to find the bundle identifiers? I know it’s only one line of code, but it would be nice to have a macro that does this, regardless of how long or short it is… Just my 2 cents worth.

Thanks, Dan, for your comment.

I don’t think most people know what a “bundle identifier” is, nor do they know how to find the bundle identifier for their desired application. Perhaps you could clarify?

I have added some information. I hope it is clearer now.

I know it’s only one line of code, but it would be nice to have a macro that does this, regardless of how long or short it is.

Well, I gave the tested sample code for a KM action. The description clearly says “execute this as Swift script”. I assume the user finds the Execute Swift Script action. If not, a search on the Wiki for “swift script” gives this as top hit.

I’m not sure if it is overly useful to reduplicate the Wiki content in each post?

Besides that, my impression is that the OP’s main issue is to reestablish the functionality of duti on macOS Sierra, which is solved in my post above. The Swift script alternative is just an afterthought / an addition, where I’m not sure if the OP is even interested in.

If the demand for a fully-fledged macro involving the Swift script, with variables and user prompt, etc., arises, I’m always happy to help. You know that.

No problem. Just a thought. Thanks!

Hi Dan,

I’ve posted a little macro “Get Bundle ID” now :grinning:

1 Like

Thanks, Tom. The swift script works great.

You have seen my post before the Swift post (duti on Sierra) too?

I did, however, the Applescript that goes with duti is significantly more complex than the Swift script. Simple wins the day.

Hi everyone,

Created a "Execute swift script" action to change my Mac's PDF default handling to Acrobat Pro:

import Foundation

LSSetDefaultRoleHandlerForContentType("com.adobe.pdf" as CFString, LSRolesMask.all, "com.adobe.Acrobat.Pro" as CFString)

After running the macro, however, Preview still opens PDFs when I click them on Finder... Any tips to fix this, please? I'm on KM 8.2.4 / macOS 10.13.6 (High Sierra).

Thanks!

Bruno

Just tested, and it still works fine on my machine.

Since it isn’t 100% clear from your code snippet quote, the import Foundation is part of the script. Without it it won’t work. So the entire script is this:

import Foundation
LSSetDefaultRoleHandlerForContentType("com.adobe.pdf" as CFString, LSRolesMask.all, "com.adobe.Acrobat.Pro" as CFString)
1 Like

Thanks Tom. Yes, had done that already. Just copy/pasted yours and it still doesn't work, must be something specific about my machine : /

Run the code in an Xcode Playground and see it if it works there. (Xcode > File > New > Playground)

I have Adobe Acrobat DC. If you have a different Acrobat version the bundle identifier might be different, not sure.

Yeah, I suspect it might be something with Swift support on my machine... Any way to debug this directly on Keyboard Maestro (ie, if the script is even running)? I get no confirmation at all when I run the macro...

As long as you have “display results in a window” enabled in your Swift Script action it should show you any error. But to be sure run it in a Playground. This way you can exclude any KM issues.

Got this error on Xcode Playground (but NOT on KM, nothing ever returns there):

Playground execution failed: Test KM.playground:3:1: error: use of unresolved identifier 'LSSetDefaultRoleHandlerForContentType'
LSSetDefaultRoleHandlerForContentType("com.adobe.pdf" as CFString, LSRolesMask.all, "com.adobe.Acrobat.Pro" as CFString)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Test KM.playground:3:68: error: use of unresolved identifier 'LSRolesMask'
LSSetDefaultRoleHandlerForContentType("com.adobe.pdf" as CFString, LSRolesMask.all, "com.adobe.Acrobat.Pro" as CFString)
^~~~~~~~~~~

Maybe Foundation isn't being imported?

According to the “unresolded identifier” errors, yes, it seems so.

Did some searching on Stack Overflow and it seems like Foundation should be available on Xcode by default. Not a Swift programmer so no idea what to do next : /