I’m working on an AppleScript studio application. I have a list of records that I want to save as presets. The program will have a default and maybe one or two others and there will be a function that the user can save their own to the list. The behavior that I am working on creating is to have a persistant “current” setting from close to close. I think that I will be able to use User Defaults for these without too much problem. However when I get to the “dynamic” list of presets then I think that the matched pairs of User Presets is too limiting to save the list of records. That bieng said I am looking for another way to store this data so that the application can load it on launch and save it for later use.
--Data Example:
set ThePresets to {{PresetName:"[Default]", DocumentSize:{8.5, 11}, Orientation:"Portrait"}, {PresetName:"Landscape", DocumentSize:{8.5, 11}, Orientation:"Landscape"}}
In the past I have written lists to a file using stock AppleScripts read/write commands. This works pretty well, it would be better if I could read/write the list of records and have it come back, but I haven’t found the syntax that will allow this. I can read a record, or a list but not a list of records similar to the above example. Does anyone know if there is somthing that might work better than coercing the list of records to a list, and saving that to a file then when needed having a rutine that will rebuild the list of records back into a variable?
set PrefPath to ((path to me as Unicode text) & "Contents:Resources:myPrefs.scpt")
script myPrefs
property thePresets : {}
end script
try
set myPrefs to load script file PrefPath
on error
set thePresets of myPrefs to {{PresetName:"[Default]", DocumentSize:{8.5, 11}, Orientation:"Portrait"}, {PresetName:"Landscape", DocumentSize:{8.5, 11}, Orientation:"Landscape"}}
store script myPrefs in file PrefPath replacing yes
end try
So basically what you are doing is (dynamically) creating a script object in the same location of the script/application. That script will act as a preferences file which will store the property thePresets. I will pull out my books and read over those commands to see if I can get a better understanding of it, but it looks like this will work better than the methods I was thinking of.
Ok, I think I have this figured out. If saved as a bundled script then:
--This basically saves the presets to the script and displays the count. If the list is empty then it resets it, if not it empties it. Not very usefull but it helped me grasp what was happening and displayed the persistancy that I need in the property.
set PrefPath to ((path to me as Unicode text) & "Contents:Resources:myPrefs.scpt")
--this loads the last iteration of the script object, if it is not present then it creates one based on the script myPrefs below.
try
set myPrefs to load script file PrefPath
on error
--if the script is not present then these lines set the default values and saves the script object as part of the script bundle.
set thePresets of myPrefs to {{PresetName:"[Default]", DocumentSize:{8.5, 11}, Orientation:"Portrait"}, {PresetName:"Landscape", DocumentSize:{8.5, 11}, Orientation:"Landscape"}}
store script myPrefs in file PrefPath replacing yes
end try
--I can grab the data from thePresets property of myPrefs into a variable from the loaded script
set x to thePresets of myPrefs
display dialog (count of x)
if thePresets of myPrefs is {} then
--here I can set the value of the property of thePresets of myPrefs like this.
set thePresets of myPrefs to {{PresetName:"[Default]", DocumentSize:{8.5, 11}, Orientation:"Portrait"}, {PresetName:"Landscape", DocumentSize:{8.5, 11}, Orientation:"Landscape"}}
else
set thePresets of myPrefs to {}
end if
set x to thePresets of myPrefs
display dialog (count of x)
--here I can the saved script so that the property thePresets of myPrefs will be updated to the latest values the next time the script object is loaded.
store script myPrefs in file PrefPath replacing yes
script myPrefs
property thePresets : {}
end script
Now this should work with an AppleScript studio application as well and since the script object will be saved in the application bundle it will also travel with the application.
Another way that I found that might work as well, though I’m not sure if it is any better and would probably take me longer to figure out how to do it, might be to use the document suite commands Data Representation and Load Data Representation. I will have to look at the Task List sample to see if I can get a better understanding of these comands.
Again thanks for the help Stefan. I have read about script objects, but my books are pre-OS X so they are a bit out of date. For the most part I haven’t used them because I for easier installation on other computers. I never really looked into the whole “Bundle” thing, this looks like a VERY usefull thing and I will keep it in mind for future projects. I guess I should get some up to date reference books. :lol: