Search code examples
macoscocoauikitappkit

Is there predefined NSColor for the Dock and Menubar on OS X?


There are some NSColor initialization helpers like controlColor() which return various colors of system components. Is there any such thing for the dock and menubar? I couldn't see one by name that popped out. If there is no such helper, is there another way to get the color?

I want to use it as the default NSWindow background color for a utility application I am developing that will be displayed in a small, borderless NSWindow.

They are both controlled from the 'General' panel in 'System Preferences' shown in the figure below.

System Preferences Dialog

Thanks in Advance.


Solution

  • No, the menu bar and Dock background appearances aren't NSColor system colors. They do look a lot like the Dark Vibrant and Light Vibrant styles you can get from NSVisualEffectView, though.

    If you want to change your view's appearance to match the state of the "Use dark menu bar and Dock" preference... well, there's no API for that, but you can read that preference from where it's stored. (Since it's not API, caveat emptor: there's no guarantee the preference storage won't change in later OS X versions.)

    In Swift (and broken into several lines for clarity):

    let defaults = NSUserDefaults.standardUserDefaults()
    let globalPrefs = defaults.persistentDomainForName(NSGlobalDomain)!
    let interfaceStyle = globalPrefs["AppleInterfaceStyle"]
    if interfaceStyle as! String == "Dark" {
        // time to be all emo
    }