Skip to main content
Last updated November 07, 2013 19:56, by Thomas Kuenneth

Welcome to Java-AppleScript-Connector

AppleScript is a powerful scripting language, which is very popular among Mac users. Scripts consist of small English sentences, for example tell application "Finder" to open the trash. Mac OS X ships with lots of interesting examples which show how to use AppleScript. Of course, the Internet offers a wealth of freely available scripts, too.

Java Standard Edition 6 and later versions allow the tight integration of Java and scripting languages through a standard Java Scripting API, which is specified in JSR-223: Scripting for the Java Platform. My article Scripting for the Java Platform offers an in-depth look at how Java developers can make use of this API.

Currently, Java SE 6.0 for operating systems other than Mac OS X ships with one pre-installed scripting language, the Mozilla Rhino implementation of JavaScript. Independent developers provide implementations of some other scripting languages. In April 2008 Apple issued an update to Mac OS X 10.5 (Leopard), adding support for Java SE 6. This version included support for AppleScript. However, it required a 64-bit Intel-based Mac.

This project offers an alternative approach. It provides a wrapper which acts as a bridge between Java and AppleScript. My article Making Scripting Languages JSR-223-Aware offers some insights on how to make existing scripting languages ready for the new Java Scripting API and is based on my work on jasconn. Java-AppleScript-Connector is not a re-implementation of AppleScript. Therefore it will only work on Mac OS X. In addition please note that Java-AppleScript-Connector requires Java SE 6.0 or later versions.

How does it work?

Earlier versions of jasconn needed to talk to Cocoa to access AppleScript. Unfortunately the Cocoa-Java API is deprecated in Mac OS X version 10.4 and later. To solve that problem, I might have written wrapper classes that use JNI. But I decided to follow a less sophisticated approach. Apple provides /usr/bin/osascript to access AppleScript in shell scripts or at the command line prompt. jasconn invokes this command and uses simple input/output redirection to run AppleScript scripts and communicate with them.

Getting started

You can check out the sources and build Java-AppleScript-Connector on your own, or you can grab a copy of the latest jar from the Downloads section on the left hand side.

To use jasconn, you just have to...

  • ...copy jasconn.jar (whether you built it on your own or you downloaded it) to /Library/Java/Extensions
  • ...or add it to the Libraries section of your projects.

The new scripting engine will then be detected automatically by JSR-223 compliant Java programs. The jar contains a demo which you can run as follows:

java de.thomaskuenneth.jasconn.demo.ScriptDemo1

jasconn also includes the so called ScriptEngineBrowser. To have a look at it, just double click on jasconn.jar or type java -jar jasconn.jar. As you can see on the left hand side, the General tab shows some information about a scripting engine. To evaluate a script, please active the ScriptRunner tab, enter the script and press the Run button.

Important notice

At the moment Java-AppleScript-Connector is far from being a finished product. It is in its infancy instead. For example some optional classes, interfaces and methods have not yet been implemented. Additionally, please expect to find bugs. Do not hesitate to contact me, I will try to fix them. Still, I may not be held responsible for any damage caused by the use of this class library. Please refer to the GNU general public licence version 2 for more legal details.

Please Confirm