Contents
Double Tell etc.
Applescript Links
Lists & Newgroups
English Dialect
Scripting Eudora
Contents
Storing Prefs
Unicode Greek
|

applescript@eremita
Storing Preferences in a Script File
• The Application Support folder
Before proceeding with the main topic, a mention of the hitherto rarely used “Application Support” folder in the System Folder. This item has been available since MacOS 7.6 and can be used to advantage. If you don't have such a folder, it doesn't matter; the single line
path to «constant ****asup»
which will resolve, when compiled in MacOS 8.* to “path to application support folder”, will create it if it does not exist. Instead of keeping preferences in the Preferences folder along with all the other often outdated plethora of garbage, we can use the Application Support folder as a resource for our applications, and here we are talking, of course, of script applets.
• Properties
A variable that is declared as a property of a script differs from local and global variables in that it remains a property of the script after the script has run. The property is declared, usually at the head of the script, using the following syntax
property someLabel:""
From then on, someLabel can be regarded as a variable and set and got just as a local or global variable, with the difference that its value persists. The initial value assigned to the property -- whatever comes after the colon -- is arbitrary, however it is important to note that when a script is edited and recompiled, the value of its properties is initialized; any values assigned in previous runs will be lost. This might sound drastic at first hearing; in practice it is not.
• Type Class «class type» of Properties
In the declaration of someLabel above, the initial value of someLabel was set to "" or a null string. It could have been set to the integer 1 or to the empty list {} or to an object of any kind. The initial value of the property does not determine the type class of any value that is later assigned to it. So int the example below, we assign a file specification to UFO as its initial value but nothing prohibits the subsequent assignment to it of a long date value or any other class of thing.
property UFO : path to preferences folder
set ufo to date "1/1/11"
return UFO
--> date "Sunday, 01 January, 1911 12:00:00 am"
• Load Script and Store Script
These two verbs come from the Standard Additions osax and are crucial to our task. Using “load script” we can import a script, whether from a compiled script or from an applet, into our current script or applet, run the script, run handlers from the script and get and set its properties. Just as it is possible to have a script object written as part of the current script, so it is possible to work with external scripts by temporarily importing them.
The load script command can be followed by either an alias or a path string.
If properties of the loaded script are changed, we need to use the verb “store script” to register the changes. Here we need to provide the path to the file as alias and, usually, add “with replacing” in order to avoid a dialog asking whether we want to overwrite.
• The Preferences Script
"Down to business -- with all of the above in mind, let us create a folder to contain our support files, prefs files and what not:
set asup to path to «constant ****asup»
set appletSupport to (asup as string) & "Applet Support:"
tell application "Finder"
if not (exists folder appletSupport) then
set appletsSupportFolder to ¬
make folder at asup with properties {name:"Applet Support"}
end if
end tell
• A Simple Example
This example involves two scripts, one passive “preferences” script, which may be an applet or a compiled script, and another active script which may also be either.
property theDate : current date
property theTime : time string of (current date)
set d to theDate
set theDate to current date
set ago to (current date) - d
display dialog "
You last looked " & ago & " seconds
ago at " & theTime buttons "OK" default button 1
set theTime to time string of (current date)
Save this script in “:Applet Support:” as “Last time”
Now instead of running the script as an applet or opening it in the editor and running it, we can load it into any other script, run it and have the properties saved:
path to «constant ****asup» as string
set f to result & "Applet Support:Last time"
set scpt to load script f
tell scpt to run
store script scpt in alias f with replacing
Now try running the script without the “store script”. You will see that the properties are not saved and that the dialog lies.
This technique is similar to what happens when we call a script from OSA Menu or from the scripts menu in Eudora or Tex-Edit Plus.
• A more complicated Example
Now let's create a small script file to hold data. Type the line beginning “property...” into a Script Editor or Smile script window and save it as "Applet Preferences" in the newly created "'"Applet Support:"''" folder.
-- compiled script at “Application Support:Applet Support:Applet Preferences”
--------------------------------
property musicalInstruments : {violins:{}} as record
--------------------------------
Doesn't look very promising? Well let's see. We can now load thiis little script into any applet or script, get and set properties and have them stored here.
set asup to path to «constant ****asup»
set f to (asup as string) & "Applet Support:Applet Preferences"
--
set prefsScript to load script f
--> «script prefs»
set Pianos to {Pianos:{}}
tell prefsScript
get its musicalInstruments
-->{violins:{}}
set its musicalInstruments to the result & Pianos
set newItem to ¬
{Steinway101414:{model:"O", date:1912, finish:"rosewood", price:9000}}
set Pianos to get the Pianos of its musicalInstruments
set the Pianos of its musicalInstruments to Pianos & newItem
end tell
store script prefsScript in alias f with replacing
--
set prefsScript to load script f
tell prefsScript
get violins of its musicalInstruments
--> {}
date of Steinway101414 of Pianos of its musicalInstruments
--> 1912
end tell
|
Composed by
John Delacour
using Frontier 5.0b20
Last updated Mon, 23 Aug, 1999 at 4:06:25 pm
| |
|