I have stumbled on a trick which might or might not be a good thing - I’m hoping to get some advice from some more experienced scripters.
The idea of persistent properties fits what I need. By that I mean calculating a property value once, and it becomes “permanent”, at least until a re-compile.
I found that I can define a property to have a value that is the result of a function. For example, here’s some code that I’m playing with:
on GetMyAppFolder() tell application "Finder" set MyApp to application file id MyAppCreator -- MyApp is a file reference if exists MyApp then set MyAppPath to MyApp as alias as string -- get full path set I to length of MyAppPath repeat until character I of MyAppPath = ":" set I to I-1 --- back up until colon found end repeat return (characters 1 through I of MyAppPath) as text else error "Cannot find MyApp" number -1700 end if end tell end GetMyAppFolder
I haven’t timed it. It’s not a terribly long operation, but I’m trying to stay as unobtrusive as possible - this script will run in the background periodically.
The magic happens (for me) when I add one line, after the above:
property MyAppFolder: GetMyAppFolder()
What this means, I found out, is that the function is called ONCE after compiling, and the result is stored. Every time after that (until a re-compile), the stored result is regurgitated, which HAS to be a faster response.
I realize that if MyApp is moved, the script will not work correctly until it’s re-compiled, since the stored value is no longer correct. I think I can live with that in my situation.
I’m guessing that the property value is stored in a resource in the script app file since it survives even a reboot, therefore, the script app must be writable (not on a CD-ROM, for example).
Are there any other drawbacks that I’m not seeing? This will run on OS 9.x only.