Controlling Your Mac with AppleScript and Java
by Scott D.W. Rankin
02/25/2003
Editor's note -- Even though there isn't a lot of formal support for Java in AppleScript, you can execute AppleScripts from Java. Why would you want to do this? Because by combining these technologies, you can easily interact with and control your Mac from remote locations. What follows is a look at how to set that up.
With the introduction of Mac OS X, Apple has made the Java language a
first-class citizen in the Mac development world. The Cocoa framework
ships by default with bindings for both Objective-C (a very well-designed
language, started in the NeXT world) and Java. Developers who want to
write a native OS X application can use either or both, for that matter,
and they will look and feel the exact same way.
Apple has given Java developers total access to the Cocoa libraries and
frameworks. This means that Java developers are no longer limited to the
Sun-specified APIs for interacting with the operating system; Apple has
opened up a whole new world of functionality to us.
Another incredibly powerful tool in the Mac OS X arsenal is
AppleScript. Virtually every Apple-produced application, including the
Finder, is controllable through AppleScript. iTunes, iPhoto, Mail:
they're all scriptable. But AppleScript, while a fantastic language for
running scripts locally, has minimal support for doing anything else, like
sockets or serving web pages. This is where Cocoa and Java come in.
Cocoa has a class, NSAppleScript, which allows a Cocoa
application to interact with scriptable applications. So now we can
combine the power of Java with the power of AppleScript to make a remote
control center for any AppleScriptable application on your computer. In
this article, we'll examine the Cocoa classes that deal with AppleScript,
and then we'll create a simple web-based application that enables you to
control iTunes from any computer on your network.
NSAppleScript -- Executing a Script from Java
There are two primary Cocoa/Java classes that we will be using when
interacting with AppleScripts: NSAppleScript
and NSAppleEventDescriptor. For
dealing with AppleScripts that send only, NSAppleScript is
all you need. Using NSAppleScript is extremely simple: in
its constructor, it takes a String containing the text of the AppleScript
that you wish to execute. You then can call the execute() method and the
AppleScript gets executed.
Before executing a script in Java, it's often helpful to try out the
script in ScriptEditor first. The ScriptEditor application is found in
/Applications/AppleScript/ScriptEditor; launch it and you will get a
window that looks like this:
|

Apple's ScriptEditor application.
|
You can type the AppleScript in the top and any results will come back
at the bottom. Here's a simple AppleScript that will make a new folder on
your desktop:
tell application "Finder"
make new folder at desktop
end tell
Type this into the ScriptEditor, and press Run. You should see a new
folder named "untitled folder" appear on your desktop:
|

The result of the first script: a new folder.
|
Now let's see how to do this in Java. The first thing we want to do is
create an empty Java class as a framework. Open up your favorite text
editor. If you use Apple's Text Edit, be sure to go to the Format menu
and choose "Make Plain Text" or it will not let you save it as a .java
file. Here is the framework of the Java class:
import com.apple.cocoa.application.NSApplication;
import com.apple.cocoa.foundation.*;
public class AppleScriptTest
{
public static void main(String[] args)
{
// Our code will go here
System.out.println("Yay AppleScript!");
}
}
Copy or type this in to your file, and save it as
AppleScriptTest.java. Next we need to compile and run our framework. Do this
by opening up Terminal (if you're using Terminal to edit the file, press
Command-N to open up a new terminal window) and changing to the
directory where you have AppleScriptTest.java. Type the following
command at the prompt (all on one line):
% javac -classpath /System/Library/Java:. AppleScriptTest.java
This will compile your class. To run it, type the following:
% java -classpath /System/Library/Java:. AppleScriptTest
It should output "Yay AppleScript!".
|

This is what you should see in the Terminal window
|
Now we need to add the code to execute the AppleScript that we wrote
above. When writing AppleScripts in Java, you should keep in mind that
AppleScript needs the quotes and line breaks to understand the script. So
we must use Java's escape characters for quotes (\") and newlines (\n) to
make sure the script goes through properly. Here is the code to insert
that will run a simple AppleScript:
// This line of code is necessary because
// of a change introduced with QuickTime 6.3
// This line loads the Cocoa libraries.
NSApplication.sharedApplication();
// This is the text of the AppleScript
String script = "tell application \"Finder\" \n"
+ " make new folder at desktop \n"
+ "end tell";
// This creates a new NSAppleScript
// object to execute the script
NSAppleScript myScript =
new NSAppleScript(script);
// This dictionary holds any errors that are
// encountered during script execution
NSMutableDictionary errors =
new NSMutableDictionary();
// Execute the script!
myScript.execute(errors);
You should compile and run your script again using the javac and java
commands from above. Not only should you see "Yay AppleScript" in the
Terminal, but if you look at your Desktop, you should see another new
folder, too.
|

The script has run, and created a second new folder, this time from Java.
|
Next we turn to reading AppleScript replies.