So here is the actual lib code. Save as DefaultsLib.scptd in ~/Library/Script Libraries.
use AppleScript version "2.3.1"
use scripting additions
use framework "Foundation"
script forInheritance
-- required for inheritance
end script
on makeDefaultsWithID:scriptId factoryValues:initialValues
script
property parent : forInheritance
property scriptDefaults : missing value
-- Storage handlers
-- This method is use for strings, records, lists, Cocoa dates
on setObject:anObject forKey:aKey
scriptDefaults's setObject:anObject forKey:aKey
end setObject:forKey:
on setInteger:anInteger forKey:aKey
scriptDefaults's setInteger:anInteger forKey:aKey
end setInteger:forKey:
on setReal:aDouble forKey:aKey
scriptDefaults's setDouble:aDouble forKey:aKey
end setReal:forKey:
on setBool:aBool forKey:aKey
scriptDefaults's setBool:aBool forKey:aKey
end setBool:forKey:
-- File storage methods
on setFile:aFile forKey:aKey
set thePath to POSIX path of theFile
set theURL to current application's class "NSURL"'s fileURLWithPath:thePath
scriptDefaults's setURL:theURL forKey:aKey
end setFile:forKey:
on setAlias:anAlias forKey:aKey
set thePath to POSIX path of anAlias
set theURL to current application's class "NSURL"'s fileURLWithPath:thePath
set theURL to theURL's fileReferenceURL()
scriptDefaults's setURL:theURL forKey:aKey
end setAlias:forKey:
-- Retrievers, returning AppleScript values
on stringForKey:aKey
return (scriptDefaults's stringForKey:aKey) as string
end stringForKey:
on listForKey:aKey
return (scriptDefaults's arrayForKey:aKey) as list
end listForKey:
on recordForKey:aKey
return (scriptDefaults's dictionaryForKey:aKey) as record
end recordForKey:
on integerForKey:aKey
return (scriptDefaults's integerForKey:aKey) as integer
end integerForKey:
on realForKey:aKey
return (scriptDefaults's doubleForKey:aKey) as real
end realForKey:
on boolForKey:aKey
return (scriptDefaults's boolForKey:aKey) as boolean
end boolForKey:
on fileForKey:aKey
set theURL to scriptDefaults's URLForKey:aKey
return (theURL's |path|() as text) as «class furl»
end fileForKey:
-- Handlers for dealing with AS dates
on registerDateDefault:aDate forKey:aKey
set theData to current application's NSKeyedArchiver's archivedDataWithRootObject:aDate
set aDictionary to current application's NSDictionary's dictionaryWithObject:theData forKey:aKey
scriptDefaults's registerDefaults:aDictionary
end registerDateDefault:forKey:
on setDate:aDate forKey:aKey
set theData to current application's NSKeyedArchiver's archivedDataWithRootObject:aDate
scriptDefaults's setObject:theData forKey:aKey
end setDate:forKey:
on dateForKey:aKey
set theData to scriptDefaults's objectForKey:aKey
return (current application's NSKeyedUnarchiver's unarchiveObjectWithData:theData) as date
end dateForKey:
-- Generic handlers for AppleScript values (dates, enumerators, etc)
on registerASDefault:aValue forKey:aKey
set theData to current application's NSKeyedArchiver's archivedDataWithRootObject:aValue
set aDictionary to current application's NSDictionary's dictionaryWithObject:theData forKey:aKey
scriptDefaults's registerDefaults:aDictionary
end registerASDefault:forKey:
on setASValue:aValue forKey:aKey
set theData to current application's NSKeyedArchiver's archivedDataWithRootObject:aValue
scriptDefaults's setObject:theData forKey:aKey
end setASValue:forKey:
on ASValueForKey:aKey
set theData to scriptDefaults's objectForKey:aKey
set aValue to (current application's NSKeyedUnarchiver's unarchiveObjectWithData:theData)
set anArray to current application's NSArray's arrayWithArray:{aValue}
return item 1 of (anArray as list)
end ASValueForKey:
end script
set ASPrefs to result
if scriptId = missing value then -- must be running as applet
set theDefaults to current application's NSUserDefaults's standardUserDefaults()
else
set theDefaults to current application's NSUserDefaults's alloc()'s initWithSuiteName:scriptId
end if
theDefaults's registerDefaults:initialValues
set ASPrefs's scriptDefaults to theDefaults
return ASPrefs
end makeDefaultsWithID:factoryValues: