I just found out there is a similar function LSCopyDefaultRoleHandlerForContentType that lets you get the default handler for a given Content Type (aka UTI).
It works very smililar to the above URL-handler function:
--> --> Put the file somewhere in your PATH <-- <--
The source:
import Foundation
let arguments = Array(CommandLine.arguments.dropFirst())
for arg in arguments {
let ext = arg as CFString
let contentType = UTTypeCreatePreferredIdentifierForTag(
kUTTagClassFilenameExtension, ext, nil)?.takeRetainedValue()
if let myApp = LSCopyDefaultRoleHandlerForContentType(contentType!, LSRolesMask.editor) {
let bundleID = Unmanaged.fromOpaque(myApp.toOpaque()).takeUnretainedValue() as CFString
print ("\(bundleID) <-- \(contentType!) <-- \(ext)")
} else {
print ("[No default handler] <-- \(contentType!) <-- \(ext)")
}
}
###Please note:
Since one Extension can have associations with more than one Content Type (there is something like a Content Type Tree), this script does not do exactly the same thing as the above script where you have to enter the Content Type.
###PS (2017-12-06):
Since I’ve seen that this post got some likes:
There is a more complete version of this macro in the Macro Library of the forum:
It combines the extension query with the UTI query and contains both scripts in one download.
Tom, many thanks for sharing all of your Swift solutions!
They all look very interesting and useful. I just need time to digest it all.
ASObjC Script to Get Default App
Just for completeness, and for those interested in ASObjC, here’s a script written by @ShaneStanley, and enhanced by @ionah, posted in the Script Debugger 6 Forum:
Not so difficult as it looks. The last two posts have download links for compiled Swift binaries. So just throw them into your path and find out if it serves for you. (In the Terminal or via KM.)