You’re right, in applescript studio properties do not retain their value between application launches. This is where you need a preferences file (ideally located in the users’ preferences folder). Basically, when your application quits you want to save any variables to a preferences file. Then at app launch first thing to do is read in the saved preferences and restore those values to the app.
In applescript studio you can use the “user defaults” system to read/write to the preferences. At app launch call the register_Preferences() and the read_Preferences() handlers. At app quit call the save_preferences().
So any variables you want to restore in your app, set them up as properties in your code so they are global. Set them up with a value of missing value. Then make a line in each of the handlers for that property so they are registered with an initial value, written at quit, and restored at launch. Make sense?
on register_Preferences()
tell user defaults -- the "make new default entry" will only make the entry if the default doesn't exists
make new default entry at end of default entries with properties {name:"myVariable", contents:"initial value"}
end tell
end register_Preferences
on read_Preferences()
try
tell user defaults
set myVariable to contents of default entry "myVariable"
end tell
return true
on error errorText number errorNumber
log "Error reading the user defaults."
log " Error " & errorNumber & ": " & errorText
return false
end try
end read_Preferences
on save_Preferences()
try
tell user defaults
set contents of default entry "myVariable" to myVariable
end tell
return true
on error errorText number errorNumber
log "Could not save user defaults."
log " Error " & errorNumber & ": " & errorText
return false
end try
end save_Preferences
If you don’t know how to use those handlers then try these. Since they use the command line tool “defaults” they’re useful for regular scripts but they can also be used in applescript studio to create a preferences file . You can look at the man page for “defaults” if you are unsure about the inputs to the handlers.
on writeDefault(prefsFilePath, KeyName, keyType, keyValue)
try
set keyType to ("-" & keyType) as Unicode text
if prefsFilePath contains "/" then
do shell script "/usr/bin/defaults write " & quoted form of prefsFilePath & space & quoted form of KeyName & space & quoted form of keyType & space & quoted form of keyValue
else
do shell script "/usr/bin/defaults write " & quoted form of POSIX path of prefsFilePath & space & quoted form of KeyName & space & quoted form of keyType & space & quoted form of keyValue
end if
on error errorText number errorNumber
log "Error in writeDefault: " & errorNumber & ": " & errorText
return false
end try
return true
end writeDefault
on readDefault(prefsFilePath, KeyName)
try
if prefsFilePath contains "/" then
set theVal to do shell script "/usr/bin/defaults read " & quoted form of prefsFilePath & space & quoted form of KeyName
else
set theVal to do shell script "/usr/bin/defaults read " & quoted form of POSIX path of prefsFilePath & space & quoted form of KeyName
end if
on error
set theVal to "Does Not Exist"
end try
return theVal
end readDefault