Hello. I am making an applescript studio application with a combo box to select a voice. If a user doesn’t like Agnes or Bruce, etc. and wants to enter a custom voice name, such as “Cepstral Diane”, it will work fine. The problem is that the custom voice needs to be re-entered every time the application is launched. Is there a simple way to make it remember custom values and add them to the list of choices? I’m thinking the answer is no, that it would need a preference file or something to save the history in, but I figured it wouldn’t hurt to ask.
Thanks in advance,
Cody J
The short, blunt answer is exactly what you feared it would be - You have to save them in preferences. However, that’s really not as hard as it sounds. I’ve been using them for a while now, and below is a set of handlers from one of my programs, and I’ve just cut out all but one of the pref key/value pairs:
on initPrefs()
make new default entry at end of default entries of user defaults with properties {name:"NW98firstRun", contents:NW98firstRun}
end initPrefs
on readPrefs()
set NW98firstRun to contents of default entry "NW98firstRun" of user defaults
end readPrefs
on writePrefs()
set contents of default entry "NW98firstRun" of user defaults to NW98firstRun
call method "synchronize" of object user defaults
end writePrefs
You use initPrefs in your “will finish launching” event handler. I know it sounds weird, but you make a new pref even if it already exists. No, you won’t over-write it! The system is intelligent and if you try to make a pref that already exists, nothing happens to the value that is already in it. It just makes sure that the pref exists so that when you read it, it doesn’t cause an error.
You generally read the prefs right after you init them, to load the pref values into your program.
You write the prefs anytime you change them, and also at the end of your program. Use the “will quit” event handler for this.
Prefs are much easier than they were pre-Panther. In Jaguar you had to do some voodoo to get the use of prefs, but Panther and Tiger are much smarter about them.
If you need help, post back or check out the AS Studio docs. Just search for “user defaults.”