Saturday, October 1, 2022

#1 2007-07-07 11:54:48 pm

Kevin Bradley
From:: Independence, MO
Registered: 2006-03-13
Posts: 548

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.


property NW98firstRun : "YES"
property NW98taxRate : 0
property NW98Logo : "None"

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.


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

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.


tell user defaults
       set NW98firstRun to contents of default entry "NW98firstRun"
       set NW98taxRate to (contents of default entry "NW98taxRate") as real
       set NW98Logo to contents of default entry "NW98Logo"
end tell

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.


tell user defaults
       set contents of default entry "NW98firstRun" to NW98firstRun
       set contents of default entry "NW98taxRate" to NW98taxRate
       set contents of default entry "NW98Logo" to NW98Logo
end tell
call method "synchronize" of object user defaults

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.



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)