简体   繁体   中英

AppleScript via Java on Mac OS X El Capitan

Prior to El Capitan, java program could invoke AppleScript using the ScriptEngineManager as follows:

ScriptEngine se = new ScriptEngineManager().getEngineByName("AppleScript");

and then setting properties and executing script with se.eval method.

After updating to El Capitan, the constructor statement shown above returns null

I am using latest Java 8 implementation from Oracle. Wondering if anyone has experienced this problem and how to solve it?

tl;dr - reinstall apple java support to get applescript support back.

You need to actually check what scripting engines are supported. The following code returns the script engines that are supported, and those are the only supported scripting engines:

import java.util.List;
import javax.script.*;

public class ListEngines {
        public static void main(String args[]) {
                ScriptEngineManager sem = new ScriptEngineManager();
                for (ScriptEngineFactory factory : sem.getEngineFactories()) {
                        System.out.println(factory.getEngineName());
                        System.out.println(factory.getNames());
                }
        }
}

Prior to installing java 6 on El Cap, I got the following reports for both Oracle JREs:

When I run it on java 8 (oracle):

 $ /usr/libexec/java_home -v 1.8 -e java ListEngines
Oracle Nashorn
[nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript]

Java 7 (oracle):

$ /usr/libexec/java_home -v 1.7 -e java ListEngines
Mozilla Rhino
[js, rhino, JavaScript, javascript, ECMAScript, ecmascript]

After reinstalling the apple JRE - from this apple URL

Java 6 (apple - you have to manually reinstall this ):

$ /usr/libexec/java_home -v 1.6 -e java ListEngines
Mozilla Rhino
[js, rhino, JavaScript, javascript, ECMAScript, ecmascript]
AppleScriptEngine
[AppleScriptEngine, AppleScript, OSA]

and now applescript support appears for the oracle VM.

$ /usr/libexec/java_home -v 1.8 -e java ListEngines
AppleScriptEngine
[AppleScriptEngine, AppleScript, OSA]
Oracle Nashorn
[nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript]

So it looks like you need to explicitly reinstall the apple JRE to get applescript support on El Cap (probably applies to older releases also).

The reason that AppleScript support appears, is because there are a bunch of extensions installed by OSX when you install the apple provided JRE - these libraries are in /System/Library/Java/Extensions . One of these libraries is AppleScriptEngine.jar (and it's corresponding libAppleScriptEngine.jnilib ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM