Reputation: 61
So I have been trying to find a solution to this problem for quite a while now but haven't been able to even get an error message. I am trying to toggle the dark mode on MacOS with this command: /usr/local/bin/dark-mode on
It works if I execute it with the standard terminal, but executing the code below doesn't do anything. The log file is empty. To make sure my code is correct I used a different command (commented out) and this one gave me the correct output to whoami
and returned my current user.
I thought this may have to do with application security on MacOS? I have no idea where to start though.
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());
}
}
Upvotes: 1
Views: 647
Reputation: 718826
I looked at the code to see what that "dark-mode" command does. (https://github.com/sindresorhus/dark-mode - thanks @MadProgrammer)
I'm no Swift or MacOS expert, but that command's implementation doesn't look like it tests if the AppleScript command succeeded. Furthermore, there doesn't seem to be anything to set non-zero exit status in the event of a failure.
So my conclusion is that the underlying AppleScript command is not working ... for reasons that are not reported ... and that information is not passed back to Java via the exit status. There is nothing you can do about it at the Java level.
My guess is that the "dark mode" request is actually failing for permissions-related reasons.
Upvotes: 2