无法运行程序“osascript”:error = 2,没有这样的文件或目录

时间:2016-02-02 19:59:33

标签: java selenium applescript appium osascript

我正在尝试在远程网格上托管的selenium测试中运行以下的AppleScript文件。

protected void enableTouchIDLogin(){
   Runtime runtime = Runtime.getRuntime();
   String appleScriptCommand =   "tell application \"System Events\" to tell process \"Simulator\"\n" +
                   "click menu item \"Touch ID Enrolled\" of menu 1 of menu bar item \"Hardware\" of menu bar 1\n"+
                   "end tell";


   String[] args = { "osascript", "-e", appleScriptCommand};
   try
   {
     Process process = runtime.exec(args);
   }
   catch (Exception e)
   {
     e.printStackTrace();
   }
}

当我在本地运行测试时,它工作正常。但是在远程网格上我得到了

java.io.IOException: Cannot run program "osascript": error=2, No such file or directory at java.lang.ProcessBuilder.start(Unknown Source) at java.lang.Runtime.exec(Unknown Source) at java.lang.Runtime.exec(Unknown Source)

我不确定为什么会这样。在osascript'返回'/ usr / bin / osascript'的远程网格上。在本地运行时,我的osascript位置相同。

鉴于本地和远程网格上的路径是相同的,我不确定为什么-e标志不起作用。我不确定我的appleScriptCommand应该是什么样的......

修改

根据其中一个回复,我尝试了以下内容,它不会引发错误,但也不会在本地或远程执行此功能。

  protected void enableTouchIDLogin(){



   try
   {
       Runtime runtime = Runtime.getRuntime();
       String appleScriptCommand =   "tell application \"System Events\" to tell process \"Simulator\"\n" +
                       "click menu item \"Touch ID Enrolled\" of menu 1 of menu bar item \"Hardware\" of menu bar 1\n"+
                       "end tell";

       File executor = File.createTempFile("exec", ".sh");
       PrintWriter writer = new PrintWriter(executor, "UTF-8");
       writer.println("#!/bin/bash");
       writer.println();
       writer.println(String.format("osascript -e \"do shell script \\\"%s\\\" with administrator privileges\"",
                       appleScriptCommand));
       writer.close();
       executor.setExecutable(true);

       Process process = runtime.exec(String.format("%s",
                       executor.getPath()));
   }
   catch (Exception e)
   {
       e.printStackTrace();
   }

  }   

1 个答案:

答案 0 :(得分:0)

这对我有用 更新5:

String script = "tell application \\\"System Events\\\" to tell process \\\"Simulator\\\"\n" +
                   "click menu item \\\"Touch ID Enrolled\\\" of menu 1 of menu bar item \\\"Hardware\\\" of menu bar 1\n"+
                   "end tell";
File executor = File.createTempFile("exec", ".sh");
PrintWriter writer = new PrintWriter(executor, "UTF-8");
writer.println("#!/bin/bash");
writer.println();
writer.println(String.format("osascript -e \"%s\" with administrator privileges",
        script);
writer.close();
executor.setExecutable(true);

Runtime.getRuntime().exec(String.format("%s", 
        executor.getPath()));