Storing Persistent Values in Preferences


(Mark Alldritt) #1

If any top-level variables in a script contain Cocoa values, you lose persistence of globals. In script bundles and applets you can use NSUserDefaults to store values in a standard preference property list file.

In the case of a .scptd file, you need to initialize defaults with a suite name that matches the file’s bundle id, like this:

use framework "Foundation"
use scripting additions

set theDefaults to current application's NSUserDefaults's alloc()'s initWithSuiteName:(my id)

From then on you can simply read (using objectForKey:) or write (using setObject:forKey:) to your defaults.

For applets, it’s a bit trickier: applications normally use standarUserDefaults to access their defaults, but if you do that, when you edit and test you will be dealing with the preferences of the script editor, not the applet. You can cover all options like this:

use framework "Foundation"
use scripting additions
global 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

This approach will work for both applets and bundle files. Using a global variable lets you access the defaults object easily from anywhere in your script.

It is obviously very important to ensure your files have unique bundle ids.

(Doeke Zanstra) #2

Good to know. This works with the defaults system, right?

I made some functions to work with do shell script "defaults read...", but this seams easier to work with.

(Shane Stanley) #3

This is the defaults system, yes. You use the above, and you can set your factory defaults using registerDefaults:, passing a record/dictionary of values.

(Jim Underwood) #4

Thanks for sharing this, Mark. It looks very useful.

Could anyone share an example of read/write of the defaults for noobs like me?

(Shane Stanley) #5

Let’s say you want to store a number, which the user can change when the script runs. You might want a default of 1. So you’d use the above plus something like:

theDefaults's registerDefaults:{myNumber:1}

If you don’t want a default value, you can leave that line out.

To set a new value, you would use:

theDefaults's setObject:2 forKey:"myNumber"

And to read the value:

set theNumber to (theDefaults's objectForKey:"myNumber") as integer

When you read the value, you will get whatever was last set, but if it’s never been set you get the value you set using registerDefaults: (or missing value if you didn’t register a default).

There are some other methods specialised for things like URLs, but that’s the gist of it. You can only store values that can be stored in property lists (URLs get converted to data or strings).

(Jim Underwood) #6

thanks, Shane. That’s very helpful. I now have a complete working model. :+1:

(Shane Stanley) #7

FYI, here are some prose and cons of using properties versus using defaults:

Near automatic; simple to use
Store all non-ASObjC classes

Can’t be used with code-signed apps
Fail (silently) in several circumstances: locked scripts, scripts on locked volumes, scripts with top-level ASObjC values, scripts that get interrupted
Can’t store Cocoa values
Results in changed modification date and size of scripts

Neither pro nor con:
Stores per-script

Can be used with code-signed scripts
Reliable – work on locked volumes, with ASObjC values, and unaffected by script interruptions
Values are transferrable
Some visibility
Can be shared

Requires ASObjC
More complex to use, especially with non-bridged AS classes

Neither pro nor con:
Stores per-user