Setting Default Open With for File Types

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 : /

Not sure what you mean. When I remove import Foundation I get exactly the same errors.

What happens when you replace import Foundation with import Cocoa ?

I get this error instead... Which seems to indicate that Foundation's module isn't missing after all? So strange...

Playground execution failed: Test KM.playground:1:8: error: no such module 'Cocoa'

import Cocoa

** ^**

Have you created an iOS or tvOS Playground? You need a macOS Playground.

52-pty-fs8

Had mistakenly chosen iOS... Getting a different error now, thanks so much for helping!

Playground execution failed: test playground mac.playground:4:68: error: type 'LSRolesMask' has no member 'all'

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

** ^~~~~~~~~~~ ~~~**

** ^~~~~~~~~~~ ~~~**

??

In your Playground with the script code, when you ⌘-right-click the word “LSRolesMask”, do you then get this…

(Note the possible members: none, viewer, editor, shell, all)

…or something different?

You can also try it with LSRolesMask.editor instead of LSRolesMask.all.

Hah, this solved it! My members are all capitalized, so as soon as i changed it to .All (vs .all) it worked! Probably a different version of the library, right?

Thank you so much for sticking with me here, hope this helps debug the issue for others in the future too!