Today I was looking into Java and AppleScript to see what is possible. But since Java has this nice little brother called Groovy, I wanted to try to execute AppleScript from within a Groovy script. With Java SE 6 AppleScript has been provided as a ScriptEngine to the javax.script API, which makes using it much easier. My little example script opens an URL in the default browser, takes a screenshot and notifies the user.
import javax.script.* def takeScreenshot = """\ open location \"http://www.bitwalker.eu\" delay 5 do shell script \"screencapture screenshot.pdf\" """ ScriptEngineManager mgr = new ScriptEngineManager() ScriptEngine engine = mgr.getEngineByName("AppleScript") engine.eval(takeScreenshot) engine.eval("say \"Your screenshot is ready\"")
You can run this directly in the GroovyConsole or as regular Groovy script. Don’t forget to set JAVA_HOME to 1.6.