I am testing my script on Mojave and inside Script Debugger 7. I’m not getting the code to work and there’s not a lot of visibility into what’s happening, so I’d appreciate any feedback.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
use framework "AppKit"
on run
local theDefaults
local theMetaDataExt
set theDefaults to getStdUserDefaults()
-- This will only set the key to "meta" if the key does not exist.
registerParameter(theDefaults, "theMetadataExt", "meta")
-- Now, get it back...
set theMetaDataExt to getParameter(theDefaults, "theMetadataExt")
display notification "Default Registered" with title "Metadata extension" subtitle theMetaDataExt
end run
-- -------------------------------------------------------------
-- getStdUserDefaults()
-- Get the current application's NSUserDefaults
-- return : current application's standard user defaults.
-- -------------------------------------------------------------
on getStdUserDefaults()
local theDefaults
if my id = missing value then error "This code works only in an applet or script bundle."
if current application's id = my id then -- must be running as applet
set theDefaults to current application's NSUserDefaults's standardUserDefaults()
else -- being run by editor or other host app
set theDefaults to current application's NSUserDefaults's alloc()'s initWithSuiteName:(my id)
end if
return theDefaults
end getStdUserDefaults
-- -------------------------------------------------------------
-- registerParameter(theDefaults, theKey, theValue)
-- Register a user default parameter
-- return : void.
-- -------------------------------------------------------------
on registerParameter(theDefaults, theKey, theValue)
local theDefaults
local theKey
local theValue
theDefaults's registerDefaults:{theKey:theValue}
return
end registerParameter
-- -------------------------------------------------------------
-- setParameter(theDefaults, theKey, theValue)
-- Register a user default parameter
-- return : void.
-- -------------------------------------------------------------
on setParameter(theDefaults, theKey, theValue)
local theDefaults
local theKey
local theValue
theDefaults's setObject:theValue forKey:theKey
return
end setParameter
-- -------------------------------------------------------------
-- getParameter(theDefaults, theKey)
-- Register a user default parameter
-- return : object.
-- -------------------------------------------------------------
on getParameter(theDefaults, theKey)
return theDefaults's objectForKey:theKey
end getParameter
Model: MacBook Pro (retina)
AppleScript: 2.9
Browser: Firefox 64.0
Operating System: macOS 10.14