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"
play
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 scr.sh 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 :

    import com.apple.cocoa.foundation.NSAppleScript;
    import com.apple.cocoa.foundation.NSMutableDictionary;
    
    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();
                    nsa.execute(errors);
                   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 :

    • http://www.macdevcenter.com/pub/a/mac/2002/11/22/itunes_perl.html
    • http://www.malcolmadams.com/itunes/scripts/scripts02.shtml
    • http://www.macdevcenter.com/pub/a/mac/2003/02/25/apple_scripting.html
    • http://www.macosxhints.com/article.php?story=20011108211802830