macOS High Sierra / Mojave:以编程方式获取 "System Preferences -> Accessibility -> Display -> Increase contrast" 的值

macOS Hight Sierra / Mojave: Programmatically get value of "System Preferences -> Accessibility -> Display -> Increase contrast"

如何以编程方式获取 macOS Hight Sierra 上 增加对比度 设置的值?知道如何观察该值变化也很有趣。

系统偏好设置的完整路径是系统偏好设置 -> 辅助功能 -> 显示 -> 增加对比度

在 macOS Mojave 上,可以通过 effectiveAppearance.bestMatch API 如下调用来实现。

open override func layout() {
  super.layout()
  if #available(OSX 10.14, *) {
     if let value = effectiveAppearance.bestMatch(from: [.aqua, .darkAqua, .accessibilityHighContrastAqua, .accessibilityHighContrastDarkAqua]) {

        switch value {
        case .aqua:
           setupAppearance(.light)
        case .darkAqua:
           setupAppearance(.dark)
        case .accessibilityHighContrastAqua:
           setupAppearance(.highContrastLight)
        case .accessibilityHighContrastDarkAqua:
           setupAppearance(.highContrastDark)
        default:
           break
        }

     }
  }
}

从 macOS 10.10 开始,您可以使用这个 API 的 NSWorkspace。

https://developer.apple.com/documentation/appkit/nsworkspace/1526290-accessibilitydisplayshouldincrea