gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:58:27 26 4
gpt4 key购买 nike

我正在尝试在远程网格上托管的 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:无法运行程序“osascript”:错误= 2,没有这样的文件或目录
在 java.lang.ProcessBuilder.start(来源未知)
在 java.lang.Runtime.exec(来源未知)
在 java.lang.Runtime.exec(来源未知)

我不知道为什么会出现这种情况。在远程网格上,“which 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();
}

}

最佳答案

这对我来说很有用
更新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()));

关于java - 无法运行程序 "osascript": error=2, 没有这样的文件或目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35162731/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com