29 lines
1.2 KiB
Swift
29 lines
1.2 KiB
Swift
import Foundation
|
||
|
||
enum SystemTheme {
|
||
static func toggle() async throws {
|
||
try await Task.detached {
|
||
guard let script = NSAppleScript(source: """
|
||
tell application "System Events"
|
||
tell appearance preferences
|
||
set dark mode to not dark mode
|
||
end tell
|
||
end tell
|
||
""") else {
|
||
throw NSError(domain: "Conjure.SystemTheme", code: 1, userInfo: [
|
||
NSLocalizedDescriptionKey: "Couldn’t create the system appearance script.",
|
||
])
|
||
}
|
||
var error: NSDictionary?
|
||
script.executeAndReturnError(&error)
|
||
if let error {
|
||
let code = error[NSAppleScript.errorNumber] as? Int ?? 1
|
||
let message = code == -1743
|
||
? "Allow Conjure to control System Events in System Settings → Privacy & Security → Automation, then try again."
|
||
: error[NSAppleScript.errorMessage] as? String ?? "macOS couldn’t change the system appearance."
|
||
throw NSError(domain: "Conjure.SystemTheme", code: code, userInfo: [NSLocalizedDescriptionKey: message])
|
||
}
|
||
}.value
|
||
}
|
||
}
|