AppleScript Shell

December 19th, 2006

A few months ago, Cameron Hayne told me about his nifty AppleScript Shell (ash), which offers an interactive command-line interface to AppleScript, and lets you do some nifty tricks like “tracing” a script’s execution, printing the resulting value for each executed statement.

But I just discovered something about ash that’s truly magical. Think “Remote Script Editor”! I’m in a hotel when I realize I need to access some information from an Address Book entry back home. So I ssh into my home computer, and start slogging through the Address Book data folder. Ugh! If only I could easily ask for the scripted information I need. Of course, there’s osascript, which is where I started heading. But ash makes this process so much easier, because of its interactive and forgiving interface:

iBook> ./ash
Welcome to ash (AppleScript Shell) version 0.60
Type: -help for help, type -exit to exit
ash> tell application "Address Book"
tell application "Address Book"
ash tell> set myPerson to first person whose name is "John Doe"
tell application "Address Book"
    set myPerson to first person whose name is "John Doe"
ash tell> value of phones of myPerson
tell application "Address Book"
    set myPerson to first person whose name is "John Doe"
    value of phones of myPerson
ash tell> end
tell application "Address Book"
    set myPerson to first person whose name is "John Doe"
    value of phones of myPerson
end tell
{"(415) 555-1384", "(415) 555-4424"}
ash> 

See how ash takes one line at a time, and then when I end the “tell” block, it shows me the results of my query. Now I can call my friend even though I’m nowhere near my Mac.

Yeah, yeah, yeah. Or I could run VNC, or ya know, put phone numbers on my iPod. But this is just one example of a scenario where interactive scripting could be handy through a remote ssh connection.

AppleScript Shell, check it out!

9 Responses to “AppleScript Shell”

  1. Peter Hosey Says:

    Sounds a bit like a Terminal-based version of CLImax.

  2. Nicholas Riley Says:

    Or you could use contacts, a command-line client to OS X’s Address Book.

    For scripting from the command line I prefer the Python scripting bridge appscript, which has a great contextual terminology browser (just use .help() on any object), in combination with the enhanced Python shell IPython.

  3. ssp Says:

    Does this require you to be logged into the machine? I’ve always wondered what happens when running an AppleScript via ssh. Can it launch GUI applications and all although you don’t have a desktop? Will it just fail? (Will it access the apps of the currently logged in user? I guess not, but you never know ;)

  4. Steve Streza Says:

    ssp, it’ll launch applications just fine, you just won’t be able to see them on the remote machine. You don’t even have to be logged in through the OS X login panel.

  5. adrianm Says:

    why not just use osascript -e ?
    bash lets you write a multiline script before execution.

  6. Daniel Jalkut Says:

    adrianm: it’s just a step up from the “all at once” execution style of osascript. With ash, you can play around with the script interactively. The example I pasted above was sort of a “staged” example in that I had already figured out how to do everything. But the interactiveness of ash is what makes it more useful for this type of thing – you can explore the results of commands and examine properties of AppleScript objects in order to figure out how to access what you want. Just like in Script Editor.

  7. Daniel Jalkut Says:

    ssp: I’m not sure – I am almost always logged in as me, so it’s not an issue in my case. I thought I heard something about Mac OS X getting better at running “login-less” at some point (10.4?). Maybe that is what Steve is alluding to.

  8. Cameron Hayne Says:

    I have now 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.

  9. FredB Says:

    Really nice!

    Not a big deal but at first I thought you were talking about ash.

Comments are Closed.

Follow the Conversation

Stay up-to-date by subscribing to the Comments RSS Feed for this entry.