Having issues in getting persistent user defaults to work.

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

Your problem is here:

That’s going to register your value for a key named “theKey”. AppleScript doesn’t let you use a string as a label in a record, so for the approach you’re taking you need to create a dictionary. Something like this:

	set theDict to current application's NSDictionary's dictionaryWithObject:theValue forKey:theKey
	theDefaults's registerDefaults:theDict

Thank you, Shane. Still learning…

There’s quite a steep learning curve to AppleScript and ASOC. Having just purchased the Script Debugger, I’m dealing with that too :>)