Prevent AppleScript script name change when changing passed arguments

I have an AppleScript script that I use to display iTunes track information on my desktop using GeekTool. I use on runargv to pass various parameters to the script, so I can show different parts of the track information without duplicating the script (for example, I can only get 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 date the file was changed changes — it seems like the script is writing something to itself or doing some kind of modification.

This is usually not a problem, with the exception of OS X 10.7, where Lion introduced file locking for files that have not been modified in more than 2 weeks. When my iTunes script is locked by the OS, it will not be able to do more of these invisible self-services when it starts, and my system log is riddled with such errors:

 osascript: couldn't save changes to script /path/to/script: error -54 

I can fix this temporarily by making some changes to the script manually (for example, adding blank lines), but after two weeks it all breaks down again because Lion blocks it.

I could theoretically disable file locking all over the country to fix this, but I would prefer - I like it for other things.

So, how can you use on run argv to pass arguments to AppleScript files without changing the date that this script changed?

Here is a minimal working example. If you run this from the command line ( oscascript test.scpt blah ), the date the script was changed 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 
+4
source share
2 answers

In addition to the properties that are defined in regular AppleScript (and not in a variant of Xcode), all global variables are persistent and stored using a script (at least until the moment of recompilation). This includes elements defined in the run handler (explicit or implied), for example:

 on run try display dialog someVariable -- will error the first time on error -- set global beep set someVariable to time string of (current date) end try end run 

You can try moving the code to a handler so that global variables are not set or changed.

+4
source

You can try submitting the script to osascript via stdin instead of reading directly.

 osascript - args < /path/to/script 

or

 cat /path/to/script | osascript - args 

You need a dash (see the man page for osascript )

However, I cannot reproduce the behavior modified by the modification time (I'm on Snow Leopard), so I do not know if it will be otherwise. But this may work, since osascript not in direct contact with the file (if it is really an osascript error).

Strange problem anyway

0
source

Source: https://habr.com/ru/post/1398872/


All Articles