Files

29 lines
1.2 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
}
}