Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,35 @@

import Cocoa

// Function to spawn a child process that can be used to update
@discardableResult
func shell(_ args: [String]) -> Int32 {
let task = Process()
let task = Process() // The new child process
let isDark = UserDefaults.standard.string(forKey: "AppleInterfaceStyle") == "Dark"
var env = ProcessInfo.processInfo.environment
env["DARKMODE"] = isDark ? "1" : "0"
task.environment = env
var env = ProcessInfo.processInfo.environment // Get the current processes environment variables
env["DARKMODE"] = isDark ? "1" : "0" // Append the DARKMODE environment variable to env
task.environment = env // Set the child processes environment variables to the current processes environment variables plus DARKMODE
task.launchPath = "/usr/bin/env"
task.arguments = args
task.standardError = FileHandle.standardError
task.standardOutput = FileHandle.standardOutput
task.launch()
task.launch() // Spawn the child process with the new environment variables
task.waitUntilExit()
return task.terminationStatus
}

let args = Array(CommandLine.arguments.suffix(from: 1))
shell(args)

// Add an observer for dark mode or light mode being set
DistributedNotificationCenter.default.addObserver(
forName: Notification.Name("AppleInterfaceThemeChangedNotification"),
object: nil,
queue: nil) { (notification) in
shell(args)
shell(args)
}

// Add an observer to check whether dark/light mode has been updated since the device last went to sleep
NSWorkspace.shared.notificationCenter.addObserver(
forName: NSWorkspace.didWakeNotification,
object: nil,
Expand Down