Detect Dark Mode in software?

Is there a way in macOS 10.14 (or earlier) to find programatically if the user has selected Dark Mode in System Preferences?

Accepted Reply

Try this :


        let x = NSAppearance.current.name
        if x == .aqua {
            print("aqua")
        } else {
            print("dark")
        }
     let darkMode = x == .darkAqua

Replies

Try this :


        let x = NSAppearance.current.name
        if x == .aqua {
            print("aqua")
        } else {
            print("dark")
        }
     let darkMode = x == .darkAqua

There are a few problems with this approach:


- `NSAppearance.current` is not the system appearance; it is just the appearance marked as current for use in resolving colors, images, etc; kind of like `NSGraphicsContext.current`. So it can return a pretty arbitrary result when called in some places

- Getting the `name` of an appearance shouldn't be used to identify it, since sometimes appearances can be more complex than just dark vs light.

- If an appearance != aqua, it doesn't mean its == dark. Because there are many appearances, it could be some other match (like vibrantLight).



@granada29, what are you trying to do by detecting dark mode? A lot of things work without having to even know what the setting is, such as having custom colors or images, redrawing, etc.


For cases where you do need to explicitly check the appearance, you'll want to decide if you care about knowing if the a view is light or dark or if you really care about the whole system (e.g. selecting a default profile in Terminal).


let appearance = view.effectiveAppearance
// or appearance = NSApp.effectiveAppearance


Then you'll want to match that against the appearances you care about:


switch appearance.bestMatch(from: [.aqua, .darkAqua, .vibrantDark]) {
  case .aqua?:
    print("do light stuff")
  case .darkAqua?:
    print("do dark stuff")
  case .accessibilityHighContrastDarkAqua?:
    print("do high contrast dark stuff")
  default:
    print("some other appearance?")
}

How do you get into accessibilityHighContrastDarkAqua mode?


I haven't been able to get this value in the first mojave Betas when I switched to Dark Mode and enabled Increase Contrast in the Accessibility PrefPane?

Is there a notification provided to become informed when the current appearance has changed?

In my application I have an image (displayed in an NSView) that looks fine when dark mode is off. With dark mode enabled, it looks to my eyes to be too bright. I was hoping to be able to detect the current setting and tone down the brightness of that image if dark mode is enabled. I'm well aware of the standard colours etc used by controls.

At the view level, you have - [NSView viewDidChangeEffectiveAppearance];

If you are not at the view level, I believe you could also KVO the effectiveAppearance property.