因此,我已经尝试了很长时间了,但是还没有收到错误消息。我正在尝试使用以下命令在MacOS上切换暗模式:/usr/local/bin/dark-mode on
如果我在标准终端上执行它,它将起作用,但是执行下面的代码不会执行任何操作。日志文件为空。为了确保我的代码正确,我使用了另一条命令(注释掉了),该命令为我提供了正确的输出到whoami
并返回了当前用户。
我认为这可能与MacOS上的应用程序安全性有关?我不知道从哪里开始。
private void switchDark() {
try {
//Activate the dark mode on MacOS
String command = new String[] { "/usr/local/bin/dark-mode", "on" };
//String command = new String[] { "whoami" }; //This works and gives me the current user
ProcessBuilder pb =
new ProcessBuilder(command[0], command[1]);
File log = new File("LOG.txt");
pb.redirectErrorStream(true);
pb.redirectOutput(Redirect.appendTo(log));
Process proc = pb.start();
proc.waitFor();
if(proc.exitValue() != 0) {
throw new IllegalThreadStateException();
}
} catch (IOException | InterruptedException | IllegalThreadStateException ex) {
System.out.println("Setting dark mode failed!");
if(debug)System.out.println(ex.getMessage());
}
}
答案 0 :(得分:2)
我查看了代码,以查看“暗模式”命令的作用。 (https://github.com/sindresorhus/dark-mode-谢谢@MadProgrammer)
我不是Swift或MacOS专家,但是该命令的实现似乎无法测试AppleScript命令是否成功。此外,如果发生故障,似乎没有任何设置非零退出状态的信息。
所以我的结论是,底层AppleScript命令不起作用……由于未报告的原因……并且信息没有通过退出状态传递回Java。在Java级别上您无能为力。
我的猜测是,由于权限相关的原因,“暗模式”请求实际上失败了。