Methods for Saving Preferences

Does anyone have any good methods for saving preferences for a script?

Script properties have proven to be imperfect (and they are not actually working now).

I generally write a file or use the shell’s defaults.

Will you provide sample code and OS details where properties do not work? While I’ve moved away from the use of properties for such things, I haven’t encountered a problem when using them (so far).

– Rob

System is 10.2.6 (freshly reinstalled at some point, still not enabling them). Usual calling is through the iTunes script menu, though that may change.

Even simple scripts which only have a property and display a dialog exhibit this problem (on my machine). Since my scripts are usually in constant development, i’m also looking for a way to keep the properties during recompilations.

I have used flat files in the past, but wanted to move to something a easier to parse and a little more substantial.

Aha! I have run into this issue with script menus in a couple of applications (I forgot!). One of the apps in question is NetNewsWire. I’ve had discussions with Brent Simmons, the developer, and he said that the problem is with the way he has implemented the script menu. I consider this a bug in the application(s) and urge you to file bug reports with the developers of applications that have this problem (even Apple).

– Rob

I’ve had issues when running scripts from the iTunes menu as well. I’ve found that if I save my scripts as applications and then run them from the iTunes menu, they work much better (and there is no difference in the appearance in the script menu although if you truly are targeting iTunes you should tell iTunes to activate early in the script because running as an application makes the script app the frontmost process and not iTunes). As Rob says, this may apply to other applications as well. I find I have much better luck using the Script Menu as opposed to individual applications menus when running compiled scripts that rely on properties.

Jon

If you need to save the data between compiles then the best way is to read/write the data to a file. Try working with this basic code, note the try statements which ensure that if an error is detected the file is closed by the script to avoid any problems.


on WritePrefs (TheData)
try
    open for access alias "ThePrefsFile" with write permission
    set eof alias "ThePrefsFile" to 0
    write TheData to alias "ThePrefsFile" as list
    close access alias "ThePrefsFile"
on error
    close access alias "ThePrefsFile"
end try
end WritePrefs

on ReadPrefs ()
    try
        open for access alias "ThePrefsFile"
        set x to read alias "ThePrefsFile" as list
        close access alias "ThePrefsFile"
    on error
        close access alias "ThePrefsFile"
    end try
end ReadPrefs