oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Ant and AppleScript
Pages: 1, 2


The osacompile task will compile plain-text files into AppleScripts. There are several flags, specified via the tag's attributes, that can be optionally set to control how the resulting script will be created:

  • language: By default, OSA scripts are created in AppleScript. However, they can be created in other languages. If you are using a language other than AppleScript, set this attribute to the language you're using.
  • resourceFork: If specified, the script will be placed in the resource fork of the output file. Note that the value must be in the format of type:id.
  • dataFork: If set to true, the script(s) will be contained in the data fork of the output file(s).
  • creator: This string specifies the file creator of the resulting script(s); if not specified, the string will be set to ToyS, which will associate the script with the Script Editor application.
  • executeOnly: If set to true, the resulting script(s) won't be editable.

In addition to the above options, the osacompile task can operate in two modes: file-based or directory-based. In file-based operation, the task will attempt to compile only one input file into a script. In directory-based operation, the task will attempt to compile all files within a directory (not including sub-directories) into individual scripts; this option allows the compilation of multiple scripts in one step.

Let's take a look at several examples of using the osacompile task:

  • Compile the file example.appleScript in the project's base directory, save it as ex.scpt in the same directory, and set the file creator value so that it is associated with Script Editor:
    <osacompile srcfile="example.appleScript" destfile="ex.scpt" creator="ToYS" />
  • Compile all files in the source directory (using the JavaScript OSA bindings) of the project's source/ directory, and save them as scripts in the scripts/ directory:
    <osacompile srcdir="source" destdir="scripts" language="JavaScript" />
  • Compile all files in the source/ directory that have "appleScript" as the file extension and save them as un-modifiable scripts in the scripts/ directory:
    <osacompile srcdir="source" destdir="scripts" filter="appleScript" executeOnly="true" />

Example Uses of osacompile

Mac OSX Conference

Session by Matt Neuburg:
AppleScript Everywhere

AppleScript is Apple's built-in scripting language for letting you automate the Mac. You can relieve drudgery, reduce many steps to one, and communicate between applications. But this session isn't about WHAT you can do with AppleScript; it's about WHERE you can do it. In this session, drawn from the upcoming new book, AppleScript: The Definitive Guide, you'll learn about all the many ways you can access AppleScript in Mac OS X: in a dedicated script editor, in a scripting environment, in an internally scriptable application, in a script runner, in an application you write, and even from a Unix script.

O'Reilly Mac OS X Conference
October 27-30, 2003
Santa Clara, CA

By integrating the compilation of our scripts into Ant, we can now create an automated build and distribution process for AppleScripts. And by storing the source of our AppleScripts in plain-text files, we can now take advantage of other resources, such as BBEdit for editing and CVS for revision control (see this series of articles on Mac DevCenter for an introduction to CVS).

And because our development is now done entirely via the command line, we can log into our Mac remotely using SSH and work on our scripts from anywhere in the world using any computer that has an SSH client installed, since Antfarm frees us from using Script Editor.


Like its cousin osacompile, osascript has several flags that can be included as attributes to control how the task operates:

  • language: Behaves identical to the language attribute of the osacompile tag, in that it specifies which language the scripts to be executed are created in.
  • filter: The extension for files that are to be executed; files that have extensions other than this string are left alone.

Now let's take a look at some examples of using osascript:

  • Execute the file example.appleScript in the project's base directory:
    <osascript srcfile="example.appleScript" />
  • Execute the file example.appleScript in the source/ directory, and interpret it as a JavaScript file:
    <osascript srcfile="source/example.appleScript" language="JavaScript" />
  • Execute all files in the scripts/ directory:
    <osascript srcdir="scripts" />
  • Execute all files in the mixed/ directory that have "appleScript" as the filename extension:
    <osascript srcdir="mixed" filter="appleScript" />

Example Uses of osascript

Using AppleScripts within Ant gives the developer access to a whole new level of functionality that was previously impossible with Ant's tasks composed of Java classes. For example, using osascript we can now:

  • Execute a script that uses the GUI Scripting framework to script OS X's interface, allowing us to change system preferences and other settings.
  • Execute a script that FTPs a .zip archive containing the necessary files via Transmit.
  • Play a song in iTunes for each step of the build process.
  • And countless others.

Antfarm Issues

At least one recent version of OS X has had its AppleScript support somewhat broken due to an update (see the comments on this article for a description). If Antfarm seems to be misbehaving on your version of OS X, please report the problems in the comments section and we'll see if we can't iron them out.

How Antfarm Works

The last section of this article is targeted towards Java developers, and provides a brief overview of how the tasks wrap around the osacompile and osascript applications. Below is a code sample that illustrates how the osascript task works; the osacompile task is nearly identical, apart from handling the optional parameters of the task.

1. Instantiate an ArrayList object that will hold the arguments to be passed to the Runtime.exec(String[]) method, which is how we'll execute the script. We also need a counter variable to keep track of how many arguments are being passed to the command, and the index of the next empty "slot" in the list:

ArrayList cmd = new ArrayList(); 
int i = 0;

2. The first thing we'll need to construct the command is the name of the program that we want to run:

cmd.add(i, "/usr/bin/osascript");

3. If the language attribute of the tag has been included, we'll add the necessary command-line arguments to the array:

if (language != null) {
  cmd.add(i, "-l"); i++;
  cmd.add(i, language); i++;

4. Finally, add the location of the file (which is kept track of using the script object, which is an instance of to the list of arguments, and copy the results of the ArrayList into a static array:

cmd.add(i, script.getPath());

String[] cmdArray = new String[cmd.size()];
for (i = 0; i < cmdArray.length; i++) {
  cmdArray[i] = (String) cmd.get(i);

5. Execute the command using our array of arguments, and wait until it has finished running before we move on to the next steps in our code:

Process r = Runtime.getRuntime().exec(cmdArray);

6. If something bad happened while trying to run the script, throw a BuildException and let the user know what the problem is:

if (r.exitValue() != 0) {

  BufferedReader err =
    new BufferedReader(
      new InputStreamReader(
  String line, error = "";
  while ((line = err.readLine()) != null) {
    error = line + "\n";
  throw new BuildException(error);		

As you can see, the code isn't very complex, and given the foundation that Ant gives you to start from, writing your own Ant task is pretty simple.

Garbage Collection

In this article we've seen how to use Ant and AppleScript, two seemingly distinct tools, as stepping stones for developers in OS X. One is open source and targeted towards high-level application developers on just about every platform in the market today, and the other is a niche tool targeted towards professionals who use Macs to do anything but use the command line. Who would've thought that they would have so much fun in the sandbox?

David Miller is combining his passions of photography and working on the web at iStockphoto; when not hacking away with a text editor and a few web browsers in hand, he can be seen huddled over his laptop tweaking levels and curves for his freelance photography. Keep track of David's latest projects over at his home on the web.

Return to the Mac DevCenter