AppleScript is a scripting language for the Macintosh. It is a high-level English-like language that has been compared to batch files on PCs, but it is more like UNIX shell scripts in its power and flexibility. AppleScript scripts are recordable, have access to just about all Finder functions, and let one use several apps together to manupulate data.

Its simplicity, power, and elegance are a testament to the innovation Apple Computer put into the MacOS.

Example Task

To explain a little bit about how to use AppleScript, I'll tell you about a small task I wanted to accomplish with the language. This consumed several hours of my time, but it was worth it - I can now control iTunes from the command line!

The AppleScript to make iTunes play is very simple :

tell application "iTunes"
end tell
Obviously, various other commands could be substituted for "play", here - such as pause, playpause, or other more complex instructions. Here are some different ways to accomplish this, from various sources:

  • osascript and the shell
  • The program osascript is a simple way to run scripts in any scripting language that is an "open scripting architecture" (OSA) language - including, of course, AppleScript. So, just like the perl interpreter, you can type 'osascript -e "tell application..."' or 'osascript scriptfile' , although you can't seem to use #! then /usr/bin/osascript. Probably doing something wrong there.

    Anyway, you could type the relevant commands as arguments to osascript every time you wanted iTunes to play a song. Unfortunately, each line of the script is a separate -e, and that gets tiresome. One very simple solution is to use alias : in csh, you would put 'alias play osascript ...' (where ... is the commands given to osascript as above) into your .cshrc.

    This is going to look a little messy with many of these aliases, so a shell script that wraps an iTunes command helps. This is just :

    osascript -e 'tell application "iTunes"' -e "$1" -e "end tell"
    so that the aliases now just become 'alias play play' - although this may only work due to quirks of quoting in csh. The grandest shell solution is a whole shell script 'interface', which you can find elsewhere on the web.

  • perl and java
  • Clearly, although this might work, it's not exactly the most maintainable solution. That doesn't matter much for iTunes! However, for scripting other applications from the command line, a more advanced language than shell scripts might be helpful. perl is the obvious choice here - especially since there is a module Mac::AppleScript.

    As for java; it is included for completeness. I like the language I really do, but for simple tasks that need to be done quickly, like changing tracks, it really isn't suitable. It is agony to wait the few seconds as various libraries are setup and virtual machines are initialised and so forth - just to send a simple "play" message Obviously, if you want to make a whole new interface to iTunes using swing...

    Here is the java code, for interest :

    public class AppleScriptTest {
            public static void main(String[ ] args) {
                    String scriptHead = "tell application \"iTunes\"\n";
                    String scriptTail = "\nend tell";
                    NSAppleScript nsa = 
                      new NSAppleScript(scriptHead+args[0] + scriptTail);
                    NSMutableDictionary errors = new NSMutableDictionary();
                   if (errors.count() > 0) System.out.println(errors)
    to use the cocoa classes, /System/Library/Java needs to be in the classpath. At least it tries to tell you if there have been some errors, although the speed penalty of this approach really doesn't make it worth it.

    sources :


Log in or register to write something here or to contact authors.