Ever since I upgraded to Mountain Lion, I can't run my AppleScript code through the Java ScriptEngineManager.
The sample code found on Apple's page (link) returns null for the engine
object.
public static void main(String[] args) throws Throwable {
String script = "say \"Hello from Java\"";
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("AppleScript");
engine.eval(script);
}
Anybody know of any workarounds?
The AppleScriptEngine class is already in rt.jar (Java 7 for Mac OS X). If you follow the information here (http://docs.oracle.com/javase/7/docs/api/?javax/script/package-summary.html) to make it 'discoverable' by placing a file called javax.script.ScriptEngineFactory under META-INF/services, then it can work without relying on the AppleScriptEngine.jar in /System/Library/Java/Extensions/. Personally, I think it's more robust to use what's already in rt.jar.
I posted this issue on bugreport.apple.com (bug id: 12692742). I received this response:
20-Feb-2013 04:21 PM Apple Developer Bug Reporting Team : We think you had an install of Lion with JavaDeveloper. You upgraded to Mountain Lion, which removed (by design) all traces of previously installed Java SE 6 under /System. This left a functional SE 6 JDK bundle under /Library/Java/JavaVirtualMachines, but the contents of /System/Library/Java/Extensions were gone.
You can resolve this a couple ways:
1) Re-install the JavaDeveloper package on the Mountain Lion system.
2) Re-install Java for OS X by removing any JDK bundles under /Library/Java/JavaVirtualMachines and /System/Library/Java/JavaVirtualMachines and running 'java -version' or '/usr/libexec/java_home --request' to initiate install-on-demand.
3) Install Java 7 from Oracle, which bundles AppleScriptEngine.
Of the 3 options, #3 is the recommend one, as developers should be moving to Java 7 anyway.
I got it working by adding a file named "javax.script.ScriptEngineFactory" in the folder "META-INF/services" of my jar as ytw indicated.
I also have to change a bit of code: language seems to no longer be "AppleScript" but "AppleScriptEngine". So this should work:
At least this works on my MacOS X Mavericks with JDK 1.7.45...
I got this working by copying AppleScriptEngine.jar and libAppleScriptEngine.jnilib from /System/Library/Java/Extensions/ on Mac OS X 10.7 (Lion) and adding it to my classpath.
Not sure why Apple removed these extensions in Mountain Lion. I've asked about it on their developer forums here: link.
On Mac OS Yosemite, java -version : 1.7.0_40-ea
ScriptEngine engine = mgr.getEngineByName("AppleScriptEngine");
In your src directory create directory META-INF
In your src directory create directory META-INF/services
Create file META-INF/services/javax.script.ScriptEngineFactory
In this file is one line:
apple.applescript.AppleScriptEngineFactory
Build and run application ! (BTW it is not more Java, it is magic)