【问题标题】:Having Java call an Applescript file with arguments让 Java 调用带有参数的 Applescript 文件
【发布时间】:2014-06-02 20:38:52
【问题描述】:

我有一个 Java 程序,它调用一个 Applescript 文件来运行,并将信息返回给 Java。但是,我还需要将一些参数 传递给 Applescript 文件。 Java文件的相关部分:

public static void scriptRunner(String[] args) {
    
    // Connect to the database.
    ConnectionManager.getInstance().setDBType(DBType.MYSQL);

    // Prepare the AppleScript file to be executed.
    String homeFolder = System.getenv("HOME");
    File scriptFile = new File(homeFolder + "/Documents/Output--Test.applescript");
    InputStream scriptStream = null;
    try {
        scriptStream = FileUtils.openInputStream(scriptFile);
    } catch (IOException e) {
        JOptionPane.showMessageDialog(null, "Could not find the Output AppleScript file.  Please notify Chris McGee", "File not found", JOptionPane.ERROR_MESSAGE);
        ConnectionManager.getInstance().close();
        System.exit(1);
    }
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(scriptStream));

    // These two lines prepare the scripting engine, ready to run the script.
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByName("AppleScript");

    // Add the parameters to the engine so they will be passed to the script.
    engine.put("javaOrderNum", args[0]);
    engine.put("javaShipDate", args[1]);
    engine.put("javaInitials", args[2]);
    engine.put("javaOverruns", args[3]);
    
    // Run the script and evaluate the result.
    log.trace("Run the script and evaluate the result.");
    Object result = null;
    try {
        result = engine.eval(bufferedReader); // Run the script and place the result into an abstract object.
    } catch (ScriptException e) {
        JOptionPane.showMessageDialog(null, "Either an error occurred with the Output script or the user cancelled it.", "Script error / cancel", JOptionPane.INFORMATION_MESSAGE);
        ConnectionManager.getInstance().close();
        System.exit(1);
    }
    log.debug(result); // Check that we received the correct information back from the script.
    log.debug("");
.
.
.

    

可悲的是,engine.put 行,正如我在搜索过程中阅读的论坛所建议的那样,以解决这个问题,似乎不起作用。 AppleScript 文件:

-- Get variables passed in
set jOrderNum to item 1 of arguments
set jShipDate to item 2 of arguments
set jInitials to item 3 of arguments
set jOverruns to item 4 of arguments

-- Set the correct folder variable
if (folderExists(POSIX path of "/Volumes/Users/Scripts/")) then
    set server_prefix to "/Volumes/Users/Scripts/"
else if (folderExists(POSIX path of "/centralserver/Users/Scripts/")) then
    set server_prefix to "/centralserver/Users/Scripts/"
else
    display alert "Please connect to the central server, then try again.
If you have already done so, please let Chris McGee know."
end if

with timeout of (30 * 60) seconds
    tell application "Adobe InDesign CS6"
        
        set myJavaScript to server_prefix & "sky-artdept/Test/Output.jsx"
        set myResult to do script myJavaScript with arguments {jOrderNum, jShipDate, jInitials, jOverruns} language javascript
        return myResult
        
    end tell
end timeout

on folderExists(posixPath)
    return ((do shell script "if test -e " & quoted form of posixPath & "; then
        echo 1;
        else
        echo 0;
        fi") as integer) as boolean
end folderExists

我收到一个错误,即变量arguments 未定义。接下来我可以尝试什么?

【问题讨论】:

    标签: java arguments applescript


    【解决方案1】:

    我无法帮助运行 applescript 的 javascript。但是,您的 applescript 代码缺少声明。您要求“参数的第 1 项”,但您从未定义变量参数。 当脚本不在任何处理程序内时,它隐含在 run() 处理程序内。而且,由于您需要在运行时传递参数,因此您应该尝试将脚本包装在包含参数声明的运行处理程序中,减去 on folderExists() 处理程序。

    on run(arguments)
    
        -- Get variables passed in
    set jOrderNum to item 1 of arguments
    set jShipDate to item 2 of arguments
    …
    end timeout
    end run
    on folderExists(posixPath)
    …
    end folderExists
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-01
      • 2014-12-31
      • 2017-06-25
      • 1970-01-01
      • 2013-07-17
      • 2011-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多