Running AppleScript from the Command Line

  1. AppleScript and the Command Line
  2. Calling the Command Line from AppleScript

The easiest way to add an AppleScript to your command line application is through the “open” command. If you type “open ~/Documents/MyAppleScript” on the command line, your AppleScript called “MyAppleScript” in your Documents folder will run. You can use “open” in conjunction with cron, with Perl scripts, or with any of the shell scripting languages. Call it as you would any other command line utility.

You can also call—and create—AppleScripts with the “osa” commands. These are “osascript”, “osacompile”, and “osalang”. In some versions of OS X, you will need to call these by using the full path to the command, that is, “/usr/bin/osascript”. If you simply type “osascript” on the command line, it will give you an obscure error. The “osa” commands cannot be used via cron if you intend to call another application, such as Microsoft Word or FileMaker. Due to the internal workings of Mac OS X’s version of Unix, cron does not have permission to call GUI applications (this is a horribly simplified reason, look on the newsgroups if you want more information). The “open” command will work from cron, however.

And, of course, remember that if you plan on calling an application from AppleScript, that application must be able to run. If it is a GUI application, then your GUI must be running: you must be logged into your computer. If you leave OS X turned on but logged out, GUI applications will be unable to open.

AppleScript is great for those scripting tasks for which the Unix “pipeline” metaphor doesn’t make sense.

  1. AppleScript and the Command Line
  2. Calling the Command Line from AppleScript