Back
Featured image of post Automate macOS Mojaves dark mode

Automate macOS Mojaves dark mode

In my opinion, the macOS Mojaves dark mode is one of the best features that Apple introduced for me. I work after hours a lot and the dark mode is awesome for that! But I think Apple forgot to implement a automation or scheduler for that!

There is a freeware tool called NightOwl that can do the job. I just tested it and it works fine. But in my workflow I need to do a bit more then just toggle the dark mode of macOS Mojave. Some Apps have the same (and many of then are still not so deeply integrated into macOS Mojave that they use the toggle). So I came up with an poor mens approach that uses the macOS internal functionality (StartCalendarInterval with LaunchAgents)! to call a script that does all the thinks I want.

Create the PLIST File for the Light Mode:

vi ~/Library/LaunchAgents/Set-LightMode.plist

Paste the following:

<?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>net.enatec.Set-LightMode</string>
        <key>EnvironmentVariables</key>
        <dict>
                <key>PATH</key>
                <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin</string>
        </dict>
        <key>Label</key>
        <string>Set Light Mode</string>
        <key>ProgramArguments</key>
        <array>
                <string>/Users/josh/bin/Set-LightMode.sh</string>
        </array>
        <key>RunAtLoad</key>
        <false/>
        <key>StartCalendarInterval</key>
        <array>
                <dict>
                        <key>Hour</key>
                        <integer>6</integer>
                        <key>Minute</key>
                        <integer>30</integer>
                </dict>
        </array>
</dict>
</plist>

You may want to tweak the Time (StartCalendarInterval)

Create the PLIST File for the Dark Mode:

vi ~/Library/LaunchAgents/Set-DarkMode.plist

Paste the following:

<?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>net.enatec.Set-DarkMode</string>
        <key>EnvironmentVariables</key>
        <dict>
                <key>PATH</key>
                <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin</string>
        </dict>
        <key>Label</key>
        <string>Set Dark Mode</string>
        <key>ProgramArguments</key>
        <array>
                <string>/Users/josh/bin/Set-DarkMode.sh</string>
        </array>
        <key>RunAtLoad</key>
        <false/>
        <key>StartCalendarInterval</key>
        <array>
                <dict>
                        <key>Hour</key>
                        <integer>23</integer>
                        <key>Minute</key>
                        <integer></integer>
                </dict>
        </array>
</dict>
</plist>

You may want to tweak the Time (StartCalendarInterval) Create the ~/bin/Set-LightMode.sh Script:

vi ~/bin/Set-LightMode.sh

Paste this:

#!/usr/bin/env bash

# Load some defaults
. /etc/bashrc

PATH=/usr/local/bin:/usr/local/sbin/:/var/root/bin/:$PATH
export PATH

osascript ~/bin/Set-LightMode.scpt > /dev/null 2>&1

exit 0

Create the ~/bin/Set-DarkMode.sh Script:

vi ~/bin/Set-DarkMode.sh

Paste this:

#!/usr/bin/env bash

# Load some defaults
. /etc/bashrc

PATH=/usr/local/bin:/usr/local/sbin/:/var/root/bin/:$PATH
export PATH

osascript ~/bin/Set-DarkMode.scpt > /dev/null 2>&1

exit 0

Create the AppleScript ~/bin/Set-LightMode.scpt with the Script Editor.app (Found in /Applications/Utilities/):

-- turn on Dark Mode on macOS
tell application "System Events"
    tell appearance preferences
        set dark mode to true
    end tell
end tell

Do the same for ~/bin/Set-DarkMode.scpt:

-- turn on Dark Mode on macOS
tell application "System Events"
    tell appearance preferences
        set dark mode to false
    end tell
end tell

Now load both Launch Agent Files:

launchctl load ~/Library/LaunchAgents/Set-DarkMode.plist
launchctl load ~/Library/LaunchAgents/Set-LightMode.plist

I uploaded everything to a GitHub Gist