Skip to content

Instantly share code, notes, and snippets.

@pakLebah
Last active Oct 18, 2018
Embed
What would you like to do?
Toggle Mojave's dark mode automatically based on given time.
# NOTES:
# • Don't forget to change every <HOME> in the scripts to your home folder name.
# • Copy .plist and .scpt files into your Library/LaunchAgents folder.
# • First time execution would require permission.
# • Toggle time is hard coded in the script as:
# – light mode: 07:00-17:00 (day time)
# – dark mode: 17:00-07:00 (night time)
# • Execution interval: every 10 minutes (600 seconds).
# load task
$ launchctl load -w /Users/<HOME>/Library/LaunchAgents/local.darkmode.toggle.plist
# unload task
$ launchctl unload /Users/<HOME>/Library/LaunchAgents/local.darkmode.toggle.plist
# check task
$ launchctl list | grep local
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>local.darkmode.toggle</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/osascript</string>
<string>/Users/<HOME>/Library/LaunchAgents/local.darkmode.toggle.scpt</string>
</array>
<key>StartInterval</key>
<integer>600</integer>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
-- get seconds since midnight
set ticks to (time of (current date))
tell application "System Events"
tell appearance preferences
set dark to dark mode
-- time > 17:00 or time < 07:00
if ticks 61200 or ticks 25200 then
if not dark then
set dark mode to true
set appearance to graphite
set highlight color to graphite
end if
else
if dark then
set dark mode to false
set appearance to blue
set highlight color to blue
end if
end if
end tell
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment