on default_Application_ID(theExtension)
set testFilePath to (path to temporary items folder from user domain as Unicode text) & "TEST." & theExtension
set testFilePosixPath to POSIX path of testFilePath
do shell script "touch " & quoted form of testFilePosixPath
tell application "System Events"
try
set defaultAppName to text 1 thru -5 of (get name of default application of (get properties of file testFilePath))
on error
delete file testFilePath
return false
end try
delete file testFilePath
end tell
return id of application defaultAppName
end default_Application_ID
default_Application_ID("webloc")
Another method. Not my code. I apologize to the author that I did not store the source in the code like I usually do.
System_DefautltAppGivenExtension("psd")
-->{appName:"Adobe Photoshop 2024", appAlias:alias "Macintosh HD:Applications:Adobe Photoshop 2024:Adobe Photoshop 2024.app:", appBundle:"com.adobe.Photoshop"}
on System_DefautltAppGivenExtension(theExt)
set d to {}
set thePath to current application's NSTemporaryDirectory()'s stringByAppendingString:("temp." & theExt)
current application's NSFileManager's defaultManager()'s createFileAtPath:thePath |contents|:(missing value) attributes:(missing value)
set theWorkspace to current application's NSWorkspace's sharedWorkspace()
set defaultAppURL to theWorkspace's URLForApplicationToOpenURL:(current application's |NSURL|'s fileURLWithPath:thePath)
if defaultAppURL = missing value then return missing value -- or false
set f to (defaultAppURL as «class furl»)
set b to ((current application's NSBundle's bundleWithURL:defaultAppURL)'s bundleIdentifier() as text)
set n to (defaultAppURL's URLByDeletingPathExtension()'s lastPathComponent() as text)
return {appName:n, appAlias:f as alias, appBundle:b}
end System_DefautltAppGivenExtension
Often, on forums the question is asked about determining the default browser, email client or graphical text editor.
Typically, solutions to this problem used parsing the property list of LaunchServices.
Since this property list changes its structure with OS updates, or is missing altogether, this approach rarely works.
On the other hand, we know that System Events knows about the default application for each individual file.
Since the default application for an individual file can be changed by the user, you need to create a new temporary file with the desired extension and receive the default application from System Events - until the user overrides the default application for the new file.
The AsObjC script you provided works fine. I also don’t know who its author is, but I immediately noticed that his brain worked in the same direction as mine.