The properties of my script are not retained. Why?

When you run a script, it is supossed to save the last value of your properties and globals, being stored for the next run. However, quoting Chris Nebel: “Well, they’ll try to save them. Whether or not they succeed is a different question”.

For instance, let’s see this script:

property x : 0

set x to x + 1

display dialog x

If you save it as an applet, you should see dialogs displaying the numbers 1, 2, 3…

Now, let’s crash the script:

property x : 0

set x to x + 1

display dialog x
do shell script "kill -14 " & word 1 of (do shell script "ps -xww | grep -i \"" & POSIX path of (path to me) & "\";")

Every time you run this code you will see allways the number 1. The script crashed and it wasn’t able to save changes.

Now, take the first script and change its permissions, so nobody is allowed to write it. Save it as applet and run this from a Terminal window (adjust “/path/to/script” to reflect the location of your app):

chmod 555 /path/to/script

Run it. You will see allways the number 1. Properties are not saved, because your script is not writeable and, consequently, was unable to store the changes.

It can be lots of “why” your script doesn’t retain properties, specially in OS X. So, you can follow Apple’s advice about storing your properties in plist files or simply plain text files. Quoting again to C. Nebel, “I wouldn’t go so far as to call using a prefs file preferred, but there are definite points in its favor: the preferences folder is guaranteed writable, so you don’t have to worry about permissions; the preferences will be per-user, not global; and your application’s modification date won’t keep mysteriously changing. On the other hand, you’ll have to write some code. Properties will continue to work for the foreseeable future, though that behavior comes with certain liabilities.”