Preferences for a script
Preferences for a script
- Subject: Preferences for a script
- From: David Durkee <email@hidden>
- Date: Wed, 30 Jul 2003 14:23:11 -0500
I have a situation where I need a script to maintain persistent data
but, because the script file or applet has to be able to be locked, I
can't use properties. I came up with the following to read and write a
preference file with the fields I want to persist all in a record named
prefs. The only piece of code in this that is specific to a particular
implementation is in SetDefaultPrefs, which has to set default values
for all fields of the prefs record so that no code that accesses them
gets an error. I've found it best, when accessing prefs fields, to do
it with parentheses, i.e. (sourcefile of prefs) to prevent the
AppleScript parser from getting confused.
A script that used this would call InitPrefs and ReadPrefs at the
beginning and WritePrefs at the end. This is obviously made more
complicated if the script might have multiple entry points. You can
just put a call to WritePrefs on any possible exit point without much
danger, but putting ReadPrefs at an entry point that might not be the
first entry point would overwrite and values that had changed (e.g. in
a script applet that keeps running and may have both a run handler and
an open handler).
I was wondering if anyone knew of a better technique for this. I
noticed that an applet that uses Nav services in X leaves a .plist file
for itself behind in the Preferences folder. It would be nice to be
able to get the scripts persistent data written out to the .plist too.
David
global prefspath
global prefs
on InitPrefs()
set prefspath to (path to preferences from user domain as string) &
FileNameFromPath(path to me as string) & ".prefs"
end InitPrefs
on SetDefaultPrefs()
set prefs to {version:1, destlist:{}, sourcefile:""}
end SetDefaultPrefs
on ReadPrefs()
try
set fileref to open for access file prefspath without write permission
set prefs to read fileref as record
on error
SetDefaultPrefs()
end try
close access fileref
end ReadPrefs
on WritePrefs()
set fileref to open for access file prefspath with write permission
write prefs to fileref
close access fileref
end WritePrefs
--
David Durkee
email@hidden
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.