Ash: An AppleScript Shell

by Erica Sadun

TrackListEveryPic.jpg

Looking for an easier alternative to osascript that still runs in the command line? Check out AppleScript Shell from Hayne of Tintagel. It's a Perl-powered script that lets you interactively execute AppleScript commands (including multi-line tell commands) as well as create interpretable scripts (via #!/usr/bin/env ash). There are a number of useful flags built in that can be used to execute commands from a specified file, echo values during execution, to display the current AppleScript, to repeat the most recent script, to call Unix commands, and so forth. e.g.


tell application "Finder"
set theSelection to selection
set n to number of items in theSelection
-echo "number of items selected: " & n
repeat with i from 1 to n
-echo "item " & i & " is " & (item i of theSelection as alias)
end repeat
end tell

The script downloaded easily--although you do have to gunzip it and chmod it to make it executable. I pasted in one of my standard rename scriptlets and it ran perfectly, updating a playlist in iTunes. There were some strange echos during the execution trace, i.e. it listed the whole script up to the current line for every step along the way, but I'm guessing this could be turned off by one of the built-in flags or by editing the source itself, which is quite short.


4 Comments

Cameron Hayne
2007-01-04 11:23:45
I have received two reports of problems with Ash from people with Intel Macs, so I suspect there is a PowerPC vs Intel issue.
For these people, Ash fails on all AppleScripts with an error like the following:
Can't call method "get" on an undefined value at /usr/local/bin/ash line 1652, line 1.


The problem is something to do with the Perl module "Mac::OSA::Simple" that is used to compile and execute the AppleScript.
I now vaguely recall something about some of the Perl modules that call Carbon having byte-order problems, so it's probably something like that in the Mac::OSA::Simple module that is causing the problem. (I don't have an Intel Mac, so I've only tested Ash on PowerPC.)


A work-around is to use Ash's command-line option "-osaMethod" to change the method used to execute the AppleScript to use the '/usr/bin/osascript' tool instead of using that Perl module. To do this, start Ash with the command:


ash -osaMethod osascript


Using the 'osascript' method in Ash is fine and you could change this to be the default by editing the Perl script and changing the variable 'defaultOsaMethod' on line 86. The only issue is that Ash will be slightly less efficient since with this method, it execs /usr/bin/osascript each time you execute an AppleScript command. Most people won't notice the small overhead this causes.

MikeFromAmerica
2007-01-04 13:10:35
Great little program but one problem: There is already a shell named "ash" [http://en.wikipedia.org/wiki/Almquist_shell]. It's not included in Mac OS X but is available via Fink. (It is also included by default in several Linux distros.)


Just thought I'd mention it in case there is any confusion...

Cameron Hayne
2007-01-04 16:02:32
There were some strange echos during the execution trace, i.e. it listed the whole script up to the current line for every step along the way, but I'm guessing this could be turned off by one of the built-in flags


Ash is intended for interactive execution, so if you paste in a multi-line script, it is as if you typed in each line in succession.
You might prefer to use the "-batch" command to go into batch mode (use the "-end" command to finish batch mode) before pasting such multi-line scripts.


Or if you have the script in a file (in text format), you could use the "-batch" command and supply the name of the text file as an argument to read the file of AppleScript commands and execute them in batch mode all in one go.

has
2007-01-04 17:23:02
Cameron Hayne: "I have received two reports of problems with Ash from people with Intel Macs, so I suspect there is a PowerPC vs Intel issue."


The Apple event-related Perl modules originally bundled with Tiger were older PPC-only versions. There are newer i386-compatible versions on CPAN, but I don't think Apple have included those in any of their Tiger updates. Best thing would be to ask their maintainer, Chris Nandor.