├── .gitignore ├── Package.swift ├── README.md └── Sources └── main.swift /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.build 3 | /Packages 4 | /*.xcodeproj 5 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:4.2 2 | // The swift-tools-version declares the minimum version of Swift required to build this package. 3 | 4 | import PackageDescription 5 | 6 | let package = Package( 7 | name: "DarkModeListener", 8 | products: [ 9 | .executable(name: "dark-mode-listener", targets: ["dark-mode-listener"]), 10 | ], 11 | targets: [ 12 | .target(name: "dark-mode-listener", path: "Sources"), 13 | ] 14 | ) 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DarkModeListener 2 | 3 | A small cli tool to listen to dark mode changes on macOS Mojave. 4 | 5 | ## Installation 6 | 7 | Installation via [Mint](https://github.com/yonaskolb/Mint): 8 | 9 | ```sh 10 | mint install LinusU/DarkModeListener 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```sh 16 | $ dark-mode-listener 17 | light 18 | dark 19 | light 20 | ... 21 | ``` 22 | 23 | The program will start by printing a line with either `light` or `dark` depending on the current active mode. It will then print a new line whenever the value changes. 24 | -------------------------------------------------------------------------------- /Sources/main.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | class Main: NSObject { 4 | override init() { 5 | super.init() 6 | UserDefaults.standard.addObserver(self, forKeyPath: "AppleInterfaceStyle", options: [.initial, .new], context: nil) 7 | } 8 | 9 | override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { 10 | let value = (change![.newKey] as? String)?.lowercased() ?? "light" 11 | print(value) 12 | } 13 | } 14 | 15 | // Disable buffering of stdout 16 | setbuf(__stdoutp, nil) 17 | 18 | let main = Main() 19 | RunLoop.main.run() 20 | --------------------------------------------------------------------------------