B4J Question Detect OSX Dark Mode?

Chris Lee

Licensed User
One of the new features in OSX Mojave is dark mode, which allows the user to pick either the traditional light theme, or select a dark theme system wide.

I know how to theme the interface, but i'm wondering if its possible to detect if the user has dark mode enabled?

From research I found this article


This suggests using inline java code might work. Although this appears to be executing an external process which might bring other issues.
I can't get this working, but i have only limited knowledge on running inline Java.
Anyone know how I might check if dark or light is selected?

private boolean isMacMenuBarDarkMode() {
    try {
        // check for exit status only. Once there are more modes than "dark" and "default", we might need to analyze string contents..
        final Process proc = Runtime.getRuntime().exec(new String[] {"defaults", "read", "-g", "AppleInterfaceStyle"});
        proc.waitFor(100, TimeUnit.MILLISECONDS);
        return proc.exitValue() == 0;
    } catch (IOException | InterruptedException | IllegalThreadStateException ex) {
        // IllegalThreadStateException thrown by proc.exitValue(), if process didn't terminate
        LOG.warn("Could not determine, whether 'dark mode' is being used. Falling back to default (light) mode.");
        return false;


Licensed User
Longtime User
Had a look at your code. You're probably missing some imports
import java.io.IOException;
import java.util.concurrent.TimeUnit;

but the post goes back to 2016 and I am not sure if that code would still work today.

Found this command:
defaults read -g AppleInterfaceStyle
If I run it from a terminal prompt, I get the result "Dark" which is correct since I am using dark mode.


Perhaps if you use the jShell library, you can run the command and get the result.
You could use the DetectOS snippet to determine the OS the app is running on so you can decide whether to run the command or not.
Last edited:
Upvote 0


B4X founder
Staff member
Licensed User
Longtime User
Don't use inline code for this. Use jShell. BTW, starting a process on the main thread is a bad practice.

Private Sub IsMacMenuBarDarkMode As ResumableSub
   Dim shl As Shell
   shl.Initialize("shl", "defaults", Array("read", "-g", "AppleInterfaceStyle"))
   Wait For shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
   Return ExitCode = 0
End Sub

Wait For (IsMacMenuBarDarkMode) Complete (DarkMode As Boolean)
Upvote 0