How do I add user preferences to my application?

To make use of preferences, what Applescript Studio calls "user-defaults", you need some script properties to hold the prefs while your application is running. You'll set these properties to the default values for your preferences.

Applescript:

You create preferences by making new user default keys and writing these initial values to those keys. The best place to do this is when your application starts up, so I put them in the will finish launching event handler. You turn this event on in Interface Builder, by selecting the "File's Owner" item and checking the box "will finish launching" in the Application section of the Applescript item in the Inspector.

Applescript:

tell user defaults
make new default entry at endof default entries with properties {name:"NW98firstRun", contents:NW98firstRun}
make new default entry at endof default entries with properties {name:"NW98taxRate", contents:NW98taxRate}
make new default entry at endof default entries with properties {name:"NW98Logo", contents:NW98Logo}endtell

OK, here's where things get strange. You do this every time the application runs, even if you have already created the default items before. Why? Because you don't have a way to tell if this is the VERY first time the application is run. Applescript is smart enough to NOT bother existing preference values if the specified keys already exist.

After you initialize the preferences, you can then read them. If you just wrote them, you'll get back the original default values. If this isn't the first time the application is run and the user has changed some of the preferences, you'll get back whatever they were changed to the last time.

Finally, when your application ends is time to write the user defaults back to the preferences file. A good time to do this is in the will quit handler. You should also do this when the user changes the defaults; usually this will be when the user quits the preferences dialog.

The "synchronize" call method flushes the defaults and makes sure they are actually written to disk. Originally, the Applescript "register" command was meant to do this, but as of Applescript Studio 1.4 (the current version), it does not do this.

Nitewing '98--I distrust morning people, largely because I suspect them of getting together early one day while the rest of us were asleep and setting up the rules of civilization.