Ant and AppleScript
Pages: 1, 2
osacompile
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 oftype: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 toToyS, 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
|
Session by Matt Neuburg: 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 |
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.
osascript
Like its cousin osacompile, osascript has several flags that can be included as attributes to control how the task operates:
-
language: Behaves identical to thelanguageattribute of theosacompiletag, 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");
i++;
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
java.io.File) 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);
r.waitFor();
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(
result.getErrorStream()));
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
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 9 of 9.
-
bash shell
2004-01-07 10:46:35 anonymous2 [Reply | View]
-
bash shell
2004-01-15 20:17:12 anonymous2 [Reply | View]
export JAVA_HOME=/Library/Java/Home
export ANT_HOME=/usr/local/ant
export PATH=/usr/local/bin:$PATH
-
bash shell
2004-01-16 07:20:01 David Miller |
[Reply | View]
Place these commands in a file called .bash_login in your home directory (ie: ~/.bash_login), and they will automatically be executed every time you log in to your account. If you want them to take effect immediately after creating the file, enter the command "source ~/.bash_login". Hope this helps.
-
Antfarm Source
2003-10-06 07:57:11 David Miller |
[Reply | View]
That is a good point; there are several reasons why the source isn't being released at this time.
The biggest one being the fact that the code is being re-worked and developed on a regular basis. As you can see from the other comments on this article, several last-minute changes caused the given example build file in the article to break. Thank-you to the anonymous donor who pointed out the problems and fixes for the example file.
I don't see the point in opening up the project until it's stabilized. My intention is to release the source code once it has reached a state of maturity that I'm happy with; by reading the first comment on this article, you can see that there is atleast one area where things can be cleaned up. From then on, those who want to run with it are free to do so.
Stay tuned to http://www.sqlmagic.com/d/antfarm if you are interested in the project. Regular updates to the code will be made and released from there. -
Antfarm Source
2003-10-07 01:34:25 sanchonevesgraca [Reply | View]
It's up to you to decide if and how to release the source code. I am happy to learn that you do intend to release it. However, you could think of opening up the development itself. That is, allowing other people to read and commit to your repository. This does not mean hosting work for you if you move the project to a third-party open repository (e.g., Sourceforce, Tigris). Likewise, the license can be picked-up quite easily from those at opensource.org (the Apache license is popular, the GPL license is polemic). The whole point of open source projects is that people contribute to the betterment of the product in an evolutionary way, often without any short-term monetary profit in mind. If you think the comments posted to this article will contribute to your project, think again. If you look at past O'Reilly articles on any subject, most comments die out within one or two months after the article has been published. This is because of the natural way conversations are carried out: articles are read and then filed away, while mailing lists or bug reports are actively maintained. Visiting your website is likewise not very helpful: downloading binary packages is the luxury that only big software houses can still afford to impose on developers. For example, I would have a go at your iTunes Java API if the source code was available. It's not and that put me off completely. Wouldn't you learn more about the art of programming if you let other people read your code and make it better?
-
Source code
2003-10-06 06:24:47 sanchonevesgraca [Reply | View]
While you brush on the merits of Ant and implictly on open source development model, you do not provide the source code for your package. Do you have a good reason for this?
-
Example messed up
2003-10-05 10:58:03 anonymous2 [Reply | View]
Given my very limited exposure to Ant, the example file you point to is messed up. In the project line the default value apparently should point to a target, but no such target exists in the example file. It works after changing the value to match one of the target names in the file.
Further down, the instructions for running the second target tell you to enter ant osascript in terminal, but once again there is no such target. The second target is named execute and if you enter ant execute it will work.
-
Ant Farm example failure
2003-10-05 09:53:18 anonymous2 [Reply | View]
* Tried installing Ant Farm and running the example file. Got this result:
[ip133:local/ant/example] bfr% ant
Buildfile: build.xml
BUILD FAILED
Target `osacompile' does not exist in this project.
Total time: 6 seconds
-
Improved Java Code
2003-10-03 20:50:55 anonymous2 [Reply | View]
I thought your java code could use some improvement. Since you are only appending items to the array list cmd, it would have been simpler to just do:
cmd.add("/usr/bin/osascript");
if (language != null) {
cmd.add("-l");
cmd.add(language);
}
...
Also, ArrayList defines the method toArray(Object[]), so constructing the array could have been done like this:
String[] cmdArray = (String[]) cmd.toArray(new String[0]);
I think these changes would make the code a lot cleaner.






Anyone know what the bash equivalent be so that ant can be launched by simply typing "ant"
tcshrc is only mentioned in the article as below:
setenv JAVA_HOME /Library/Java/Home
setenv ANT_HOME /usr/local/ant
setenv PATH /usr/local/ant/bin:$PATH
thanks... i'm stuck =)
- cb