AppleScript Shell
AppleScript is a very powerful scripting language available on all Macintosh computers. Usually, you write an applescript and then compile it, at which point it can be run like any other Mac application. In Mac OS X (which is Unix-based), it is possible to type commands in a Unix shell (e.g. bash). I was curious to see if I could make an "AppleScript shell" which would allow immediate execution of AppleScript commands. I implemented "ash" as a Perl script and it works quite well. I'm not sure whether anyone will find it useful - send me an email if you do.
You need Mac OS X to use it. After uncompressing it, save the file
somewhere appropriate (I have it in a folder "Scripts" under my home folder
which is in my PATH), make it executable, and then run it in the Terminal.
When it is running, you will see the "ash>" prompt.
Type the command "-help" (without the quotes, but don't omit
the initial dash ("-")) at the prompt to get more information
about how to use it.
(Here is the output of the "-help all" command
if you want to read it now.)
You can also look at a PDF version of the man page for 'ash'.
Features
- Interactive execution of AppleScript commands
- Handles multi-line commands (e.g. 'tell') by going into a mode - current mode is indicated by the prompt
- Subroutines (starting with "on" or "to") and script objects (starting with "script") are persistent and thus are available for use in any subsequent interactive command
-
Can be used as the
"shebang"
interpreter for stand-alone scripts that run non-interactively.
The "shebang" line for such scripts would be: #!/usr/bin/env ash
(assuming that the 'ash' script is in your PATH) You can also execute files of AppleScript commands by supplying the filenames on the command-line when you invoke 'ash'. - The "-source" command can be used to execute the commands in a file (like the 'source' command in 'tcsh' and 'bash') This is especially useful for bringing in subroutine definitions.
-
The "-echo" command can be used to output the values of AppleScript
expressions - this is especially useful for debugging. For example:
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 "-abbrev" command allows creation of abbreviations for commonly used phrases
- The "-show" command displays the current AppleScript
- The "-editor" command sends the current AppleScript to Apple's "Script Editor"
- The "-rerun" command reruns the most recently executed AppleScript
- The "-batch" command allows a bunch of AppleScript commands to be batched up for later execution
- The "-read" command can be used to read from the keyboard into an AppleScript variable.
- The "-cd", "-pwd", "-ls" commands operate like the standard shell commands.
- The "-!" escape can be used to run an arbitrary Unix command.
- The "-createMan" command generates a 'man page' file for 'ash'.
- You can use the "-oneoff" command-line option to have 'ash' automatically exit after running one (interactively supplied) AppleScript command.
- You can enable a "trace facility" for the execution of your AppleScripts via the "-trace" option. In trace mode, the execution pauses after each AppleScript statement and displays the result from the previously executed statement.
- More detailed usage information is available via the "-help" command.
Perl techniques
The 'ash' Perl script is one of the longest and most complicated that I have written. It illustrates several techniques of Perl programming:- POD documentation with embedded Perl variables providing customization
- generation of a man page via Pod::Man from a string as opposed to a file
- execution of AppleScript (embedded or user-supplied) via three different methods (Mac::OSA::Simple, Mac::Perl, /usr/bin/osascript)
- extracting AppleScript error messages when using Mac::OSA::Simple
- use of the 'autouse' pragma to avoid bringing in modules that are only used when the user uses certain options
- use of Text::ParseWords (including working around bugs relating to mismatched quotes) to split user input into words before doing substitution of abbreviations.
- look-up table of subroutines for command handling
- use of Term::ReadLine to provide command-line facilities
- use of Getopt::Long with subroutines for option handling