I have an AppleScript script that I use to display iTunes track information on my desktop with GeekTool. I use on runargv to pass different parameters to the script so I can show different parts of the track information without duplicating the script (for example, I can get just the title by running osascript itunes.scpt title and I can get the artist/album by running osascript itunes.scpt album).
However, every time I run the script with an argument, the actual file modification date is changed—it seems that the script is writing something to itself or making some sort of modification.
This isn’t ordinarily a problem, except in OS X 10.7, where Lion introduced file locking for files that haven’t been modified in more than 2 weeks. Once my iTunes script is locked by the OS, it can’t make any more of these invisible self updates when it runs, and my system log gets riddled with errors like this:
osascript: couldn't save changes to script /path/to/script: error -54
I can fix this temporarily by making some modification to the script manually (adding empty lines, for example), but two weeks later it all breaks down again because Lion locks it.
I could theoretically disable file locking systemwide to fix this, but I’d rather not—I like it for other things.
So, how can you use on run argv to pass arguments to AppleScript files without changing the modification date of that script?
Here’s a minimal working example. If you run this from the command line (oscascript test.scpt blah), the script’s modification date will change.
--test.scpt
on run argv
tell application "iTunes"
if player state is playing then
set trck to current track
set title_text to (get name of trck)
return title_text & " " & item 1 of argv
end if
end tell
end run
In addition to properties that are defined in a regular AppleScript (not the Xcode variety), all global variables are persistent and are saved with a script (at least until it is recompiled). This includes items defined in the run handler (either explicit or implied), for example:
You might try moving your code to a handler so that global variables are not set or changed.