I’m using the following app to test a routine to determine the default app that will start, based on the extension of a data file. The routine will not return my app as the default name even with the association being set by hand. The routine does, however, work for the std apps shipped with Mac Mojave.
Perhaps someone has seen this behaviour and suggest a solution?
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
on run
-- Allow the user to change it
set theResponse to display dialog "Default 'Buddy' (aka metadata) file extension:" default answer "meta" with icon note buttons {"Ok"} default button "Ok"
set theExt to (text returned of theResponse) as string
-- Determine the default app for theFile...
set theDefaultApp to getDefaultApp(theExt)
display dialog theDefaultApp
end run
-- -------------------------------------------------------------
-- getDefaultApp(theFileExt)
-- Return the default app for a specified extension.
-- Based on @ShaneStanley's script & enhancements (2017-11-26)
-- http://forum.latenightsw.com/t/how-do-i-get-the-default-app/830/2
-- Consider using actual file to get extension rather than a dummy one.
-- theFileExt : The extension of a file as a string.
-- return : The default application as text.
-- ------------------------------------------------------------
on getDefaultApp(theFileExt)
local theFilename -- The filename of a temporary file.
local theWorkspace -- The workspace of the current App.
local NSCurrentApp -- The current App.
local NSAppURL -- The URL for the default App.
local NSResult -- The result of getting the resource value of the default App.
local NSAppName -- The filename of the default App.
local NSError -- The error code for getting the resource value of the default App.
-- Define a temporary filename, with the target extension, to be created in the temporary directory.
set theFilename to (POSIX path of (path to temporary items)) & "temp." & theFileExt
-- Determine the current application.
set NSCurrentApp to current application
--- Create the temporary file.
NSCurrentApp's NSFileManager's defaultManager()'s createFileAtPath:theFilename |contents|:(missing value) attributes:(missing value)
-- Determine the workspace for the current application.
set theWorkspace to NSCurrentApp's NSWorkspace's sharedWorkspace()
-- Get URL of default App for the temporary file using "URLForApplicationToOpenURL"
-- - (NSURL *)URLForApplicationToOpenURL:(NSURL *)url;
-- The URL of the default app that would open the specified url.
-- Returns nil if no app is able to open the url, or if the file url does not exist.
-- This is the programmatic equivalent of double clicking a document in the Finder.
-- It is safe to call this method from any thread of your app.
set NSAppURL to theWorkspace's URLForApplicationToOpenURL:(NSCurrentApp's |NSURL|'s fileURLWithPath:theFilename)
if NSAppURL is not (missing value) then
--- Get the Filename of the default App.
set {NSResult, NSAppName, NSError} to NSAppURL's getResourceValue:(reference) forKey:(NSCurrentApp's NSURLLocalizedNameKey) |error|:(reference)
--- Convert the name of the default App to text.
set theappName to NSAppName's stringByDeletingPathExtension() as text
-- Return the name of the default App as text.
else
set theappName to ("#Error: Missing NSAppURL for: " & theFileExt) as text
end if
return theappName
end getDefaultApp