-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmain.swift
executable file
·42 lines (36 loc) · 1.21 KB
/
main.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// The program will have the DARKMODE env flag set to 1 or 0
// Compile with:
// swift build
// And run the binary directly
// Most credit goes to https://github.com/mnewt/dotemacs/blob/master/bin/dark-mode-notifier.swift
import Cocoa
@discardableResult
func shell(_ args: [String]) -> Int32 {
let task = Process()
let isDark = UserDefaults.standard.string(forKey: "AppleInterfaceStyle") == "Dark"
var env = ProcessInfo.processInfo.environment
env["DARKMODE"] = isDark ? "1" : "0"
task.environment = env
task.launchPath = "/usr/bin/env"
task.arguments = args
task.standardError = FileHandle.standardError
task.standardOutput = FileHandle.standardOutput
task.launch()
task.waitUntilExit()
return task.terminationStatus
}
let args = Array(CommandLine.arguments.suffix(from: 1))
shell(args)
DistributedNotificationCenter.default.addObserver(
forName: Notification.Name("AppleInterfaceThemeChangedNotification"),
object: nil,
queue: nil) { (notification) in
shell(args)
}
NSWorkspace.shared.notificationCenter.addObserver(
forName: NSWorkspace.didWakeNotification,
object: nil,
queue: nil) { (notification) in
shell(args)
}
NSApplication.shared.run()