Property variable losing value between script launches

Hi

I am encountering behaviour that I cannot explain and which is causing me a headache.

I have a script with a number of settings defined as property variables, which can be altered by the user.

When I save the script and run it from Script editor. All is fine. Any changes made to the property variables show the changed values when I run the script again from Script Editor.

However, when I run the same script by selecting it from the iTunes script menu, it loses all the user changes between launches and reverts to the default values set in the script itself.

Can anyone help on this? What is going on?

Thanks

Model: iBook
AppleScript: Latest
Browser: Safari 417.8
Operating System: Mac OS X (10.4)

Many attachable apps don’t bother to store the changed script back to file upon quitting. You’ll need to store any persistent state externally; e.g. see:

http://bbs.applescript.net/viewtopic.php?id=15581

I see.

Thanks for the tip and heads up

Not that hhas’ solution needs any validation, but as the poster in the thread referenced above and a user of the solution I can testify that it works perfectly! It really is a very handy way to save data from one run of a script to another without losing it if you recompile.

In the end, I used John’s Unix suggestion below from the same thread (shorter code:))

It works well for me using Tiger. Presumably it will work in earlier OS X versions as well

Cheers

As an alternative, I’ve used the unix defaults command to do this in the past.

Open this Scriplet in your Editor:

set myPrefDomain to “com.myUniqueDomain.myPrefs”
– something that’s not likely to be used by anyone else

set myVar to text returned of (display dialog “What do you want saved?” default answer “I’d like this saved.”)
–Write to your Prefs folder
do shell script "defaults write " & myPrefDomain & " myVar " & quoted form of myVar

–Read from your prefs folder
set myStored to do shell script “defaults read " & myPrefDomain & " myVar”
display dialog myStored
It is important that the name of the domain is unique. ‘defaults write’ writes a plist file in your preferences folder, if one already exists with that name the preference will be written into it (overwriting one with the same key if it exists).

See man defaults in the Terminal.

Best wishes

John M

Shorter, definitely. Faster, no.

Speed is irrelevant as it’s not a performance-critical part of the program. The main limitation of using the ‘defaults’ command is that it’s only good for values that are easily passed via ‘do shell script’ (i.e. easily converted to and from Unicode text and not too large for the command line). Serializing a record or script object is more powerful and flexible, but if all you have is a few strings or numbers then ‘defaults’ will be ‘good enough’ and the rest is really just a matter of taste.

HTH

Hi kiwilegal,

Another thing is, you should be able to save your script as an application and run it through iTunes. This preserves the updated property or global variables. You can easily make it background only if you don’t want it to show in the dock.

gl,