macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Ant and AppleScript

by David Miller
10/03/2003

This article is aimed at two seemingly distinct sets of developers: application developers who spend the bulk of their time churning out Java classes, and professionals who use AppleScript to automate their workflow.

While there is overlap between the two communities, each serves a distinct need: Java developers are building applications designed to run on any platform that supports a Java Runtime Environment (JRE), while AppleScripters are writing scripts to act as the glue between applications on the Macintosh. However, we'll see that tools from each community can aid the other in the development process: Ant to aid in the development of AppleScripts, and AppleScript to aid in the development of Java applications.

This article contains an introduction to Ant for those who haven't worked with it before. If you already use Ant on a regular basis, most of the material in the first part of this article will be will be familiar to you, in which case you can skip ahead to the section "Installing Antfarm." For those readers who are new to Ant, the beginning of this article will serve as an introduction.

Introduction to Ant

In recent years, the Apache Software Foundation's Ant (an abbreviation for Another Neat Tool) has become the de facto standard for automating the build process of Java projects. Like the rest of Apache's offerings, Ant is open source software. So if you're looking for a task that isn't included in the distribution, there's a strong chance that someone else has written the code and is willing to share their work with you.

There are several reasons why Ant is so popular among software developers (see the list of rewards that it has received here), the most important being the fact that it has made building Java projects, both big and small, easy. Other reasons include:

  • Extensibility: If your project requires a task that Ant doesn't support, it is a easy process to write your own task that will suit your needs.
  • Open source: It costs absolutely nothing to download and use Ant, and there are countless (also free) resources on the Web, in case you run into any problems using it.
  • Cross-platform: Java programs are designed to run on any platform that supports a JRE, and because Ant is Java-based, it too can be used on any platform with Java installed. This means that you can use the same files on a Mac, Windows, or Linux box, and the build process will behave identically across all machines. However, it should be noted that the OSA architecture is only available on OS X machines; thus, any project that uses Antfarm tasks can only be built on an OS X machine.

Installing Ant

Since Java is included in Mac OS X, installing Ant is a straightforward process. Version 1.5.4, which is the latest version at the time of this writing (September 2003), can be downloaded here. Because the project's codebase is constantly being updated and revised, it's a good idea to keep an eye out for new releases, as they will add new features and fix bugs afflicting older versions; by joining the mailing list, such notifications will automatically be delivered to your inbox.

Once you've downloaded the latest release to your desktop, open up a Terminal window and execute the following commands:


d@localhost> cd ~/Desktop
d@localhost> sudo sh  
Password:
root@localhost> mv apache-ant-1.5.4-bin.zip /usr/local/
root@localhost> cd /usr/local/
root@localhost> unzip apache-ant-1.5.4-bin.zip
root@localhost> chown d:staff apache-ant-1.5.4
root@localhost> ln -s apache-ant-1.5.4 ant

For those not familiar with the command line, here is a brief explanation of what the above commands accomplish. If these commands are still completely foreign, you might want to invest in a UNIX book to brush up on the innards of OS X.

Related Reading

Ant: The Definitive Guide
By Jesse E. Tilly, Eric M. Burke

  • Change our location in the filesystem to our Desktop, which is where we saved the .zip file.
  • Authenticate ourselves as root (otherwise known as the superuser), so that we are able to work in directories that we aren't able to modify from our regular account.
  • Copy the .zip file to the directory /usr/local, which is where Ant will be installed.
  • Change our location to the above directory, so that the following commands will affect this directory.
  • Expand the .zip file.
  • Change the ownership of the directory to our regular account (replace "d" with your account name), which will allow us to modify the installation directory in the future without needing to authenticate as root again.
  • This last step creates a link, which is UNIX's equivalent to the Mac's alias; while it is not necessary to complete this step, we will now be able to refer to the directory as /usr/local/ant, instead of the long-winded /usr/local/apache-ant-1.5.4.

Now that everything is in place, we can invoke Ant with the command /usr/local/ant/bin/ant. This is a little long-winded and a pain to type; what we'll do now is set up our account so that we can simply type the command ant instead. In order to do this, we'll have to edit our tcsh initialization file: open up a Terminal and type emacs ~/.tcshrc, and ensure that it contains the following three lines:


setenv JAVA_HOME /Library/Java/Home
setenv ANT_HOME /usr/local/ant
setenv PATH /usr/local/ant/bin:$PATH

To save our file and exit emacs, type Ctrl-X Ctrl-S Ctrl-X Ctrl-C. Once that's done, quit the Terminal application and open it again to allow our new settings to take effect. We will now be able to invoke Ant by simply typing the command ant in a Terminal window; if you receive an error, review the steps listed above and ensure that they were successfully completed.

Unfortunately, a comprehensive introduction to tcsh is beyond the scope of this article; if you're interested in learning more about it, see the tcsh man page online, or pick up Using csh & tcsh by Paul Dubois (O'Reilly & Associates, 1995).

How Ant Works

Once invoked, Ant looks for a build file, which is simply an XML file that contains instructions on what has to be done to build this project (by default, this file is named build.xml, and is stored in your project's root directory). Each project is composed of targets, which, in turn, are made up of one or more tasks. Typical tasks include compiling classes, copying or deleting files/directories, creating documentation, compressing files into .zip archives, composing and sending email messages, and countless other actions. To see which tasks are included with Ant, view the list from the online documentation.

Targets are created by specifying a list of tasks that are to be executed. For example, a typical build file for Java projects may contain these targets:

  • clean: Remove any existing remnants from the previous build that may still be lying around.
  • prepare: Ensure that everything is in place for the following steps, such as creating directories for the documentation and classes.
  • compile: Compile all classes used in the project.
  • deploy: For web applications, this task will copy the necessary files (for example: JSPs, servlets, and other classes) to the server's application directory.

Take a look at this sample project to see what a very basic Ant build file looks like. Unfortunately, a thorough tutorial on using Ant is beyond the scope of this article; to gain a deeper understanding of how to use Ant, refer to the manual that is included in the distribution.

Installing Antfarm

Once Ant is installed, you'll need Antfarm, which is available here. De-compress the .zip file, and place the .jar file in Ant's library directory (if Ant is installed in /usr/local/ant, the library can be found at /usr/local/ant/lib). The Antfarm classes are now available to Ant, which allows us to use them in our build files. This last step is crucial; if antfarm.jar is placed in the wrong location, Ant won't know where to find the tasks, and we'll be stuck with error messages instead of AppleScripts.

Using Antfarm

The com.fivevoltlogic.antfarm package (Antfarm) contains two classes that allow us to bridge the gap between AppleScript and Java: osacompile will compile source files into scripts, while osascript will execute files/scripts (similar to Apple's Script Editor application or Script Menu extension).

Using the two OSA tasks in your own build files is much like using any other Ant task; simply include the task's XML tag (and optional attributes to control how the task operates) in your project's targets. However, because the tasks are not included in the standard distribution of Ant, we'll have to tell it which class is used to implement which task. This can be accomplished by including the following tags in each build file that uses Antfarm:


<taskdef task="osacompile" class="com.fivevoltlogic.antfarm.OSACompile" />
<taskdef task="osascript" class="com.fivevoltlogic.antfarm.OSAScript" />

Pages: 1, 2

Next Pagearrow