簡體   English   中英

無法運行程序“ osascript”:error = 2,沒有這樣的文件或目錄

[英]Cannot run program “osascript”: error=2, No such file or directory

我正在嘗試在遠程網格上托管的硒測試中運行以下小程序。

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();
   }

  }   

這對我有用
UPDATE5:

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()));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM