[macOS] Create Automatic Light/Dark Mode ...

[macOS] Create Automatic Light/Dark Mode Using MacOS Built-in Apps

Apr 11, 2023

I've always wanted to have an automatic desktop background switch based on sunset and sunrise that goes along with my f.lux setting of Menu Bar and Dock on 10.13 OSX.

Setting automatic dark mode in older versions (10.14-) of macOS is tricky. Today, I'm going to show you how to achieve automatic light/dark mode that works with the sunrise/sunset using built-in macOS apps such as TextEdit and Terminal.

Before copying and pasting anything, make sure you replace *your-name* with your username.

Let's start!

1. Pick your files first, name them: "light.jpg" for daytime desktop background and "dark.jpg" for the nighttime one. I’m using "light.jpg" and "dark.jpg" names to easily change the wallpapers rather than changing paths to other files in the final script. You can use what I'm using: DOWNLOAD MACOS VENTURA WALLPAPERS

2. Put them in the folder. You can go along with me and my settings: Pictures > Wallpapers > Auto Theme so your switchable desktop backgrounds will be in the same place always

3. Create New Document in TextEdit or any code editor and paste the code:

#!/bin/bash

# Get your current latitude and longitude
location=$(curl -s https://ipinfo.io/loc)

# Use the latitude and longitude to fetch the sunrise/sunset times
sunrise=$(curl -s https://api.sunrise-sunset.org/json\?lat\=${location%,*}\&lng\=${location#*,}\&formatted\=0 | jq -r '.results.sunrise')
sunset=$(curl -s https://api.sunrise-sunset.org/json\?lat\=${location%,*}\&lng\=${location#*,}\&formatted\=0 | jq -r '.results.sunset')

# Convert the sunrise/sunset times to seconds since midnight
sunrise_secs=$(date -jf "%Y-%m-%dT%H:%M:%S%z" "$sunrise+0000" "+%s")
sunset_secs=$(date -jf "%Y-%m-%dT%H:%M:%S%z" "$sunset+0000" "+%s")
now_secs=$(date "+%s")

# Set the desktop background based on the time of day
if [[ $now_secs -lt $sunrise_secs || $now_secs -ge $sunset_secs ]]; then
 osascript -e 'tell application "Finder" to set desktop picture to POSIX file "/Users/*your-name*/Pictures/Wallpapers/Auto Theme/dark.jpg"'
else
 osascript -e 'tell application "Finder" to set desktop picture to POSIX file "/Users/*your-name*/Pictures/Wallpapers/Auto Theme/light.jpg"'
fi


4. Hit ⇧⌘T to Make Plain Text and Save as "Auto Theme.sh" or whatever name you like. I like to keep my files in order, hence the path /Users/*your-name*/Documents/Scripts/Auto Theme.sh

5. Open Terminal app and make the file executable by pasting the command and hitting ↵ Enter:

chmod +x /Users/*your-name*/Documents/Scripts/Auto\ Theme.sh

6. Type:

crontab -e

to open the crontab editor. Once you’re on the vim screen, hit i to put the editor into INSERT mode and then paste the following line to the crontab file to run the script every five minutes:

*/5 * * * * /Users/*your-name*/Documents/Scripts/Auto\ Theme.sh >/dev/null 2>&1

7. After typing out the cron expression, hit esc and then type:

:wq

to save and exit vim. You probably won’t see what you’re typing, so watch out. If you did everything properly, you should see the terminal tell you that it installed a new crontab. In case it tells you that the setup failed, double-check your cron expression. To see your active cron jobs, you can use the command:

crontab -l

That's it!

This script uses the ipinfo.io API to get your current latitude and longitude, and then uses the api.sunrise-sunset.org API to fetch the sunrise and sunset times for your location. The script then converts these times to seconds since midnight and compares them to the current time to determine whether it's daytime or nighttime.

If it's nighttime, the script sets the desktop background to the dark image. If it's daytime, the script sets the desktop background to the light image. The script runs every five minutes using a crontab job to automatically update the desktop background based on the current time.

This script should allow your computer to go to sleep as it does not continuously run in the background. Instead, it will be executed by the cron daemon at the specified intervals.

Enjoy! 🤗☕🥐

Enjoy this post?

Buy hempbiscuit a coffee

More from hempbiscuit