Skip to content

Instantly share code, notes, and snippets.

@mojoaxel
Last active March 30, 2026 04:30
Show Gist options
  • Select an option

  • Save mojoaxel/2f0f745f654134e9e7efea286f652a55 to your computer and use it in GitHub Desktop.

Select an option

Save mojoaxel/2f0f745f654134e9e7efea286f652a55 to your computer and use it in GitHub Desktop.
Automatic light/dark theme switcher for ubuntu 24.04

Automatic light/dark theme switcher

A script that can switch between light and dark themes either automatically based on time or manually via command-line arguments. It switches to dark theme at night (between 21:00 and 6:00) and light/default by day. Works in ubuntu 24.04.03

Setup

nano ~/.local/bin/theme_switcher.sh
chmod +x ~/.local/bin/theme_switcher.sh
chrontab -e

* * * * * ~/.local/bin/theme_switcher.sh

Usage

./theme_switcher.sh --dark
./theme_switcher.sh --light

# Automatic mode
./theme_switcher.sh

License

This script was created by mojoaxel with the help of Claude Sonnet 3.5 and is licensed under CC0 - No Rights Reserved.

#!/bin/zsh
# Function to set theme
set_theme() {
current_theme=$(gsettings get org.gnome.desktop.interface color-scheme)
if [ $1 = "dark" ]; then
if [ "$current_theme" != "'prefer-dark'" ]; then
echo "Switching to dark theme"
gsettings set org.gnome.desktop.interface color-scheme prefer-dark
fi
else
if [ "$current_theme" != "'default'" ]; then
echo "Switching to light theme"
gsettings set org.gnome.desktop.interface color-scheme default
fi
fi
}
# Parse command line arguments
for arg in "$@"; do
case "$arg" in
-d|--dark)
set_theme "dark"
exit 0
;;
-l|--light)
set_theme "light"
exit 0
;;
-*)
echo "Invalid option: $arg" >&2
echo "Usage: $0 [-d|--dark] [-l|--light]" >&2
exit 1
;;
esac
done
# If no arguments provided, use time-based switching
# Get current hour (24-hour format)
current_hour=$(date +%H)
# Check if current time is between 21:00 and 06:00
if [ $current_hour -ge 21 ] || [ $current_hour -lt 6 ]; then
# Set dark theme
set_theme "dark"
else
# Set light theme
set_theme "light"
fi
@ianheggie-oaf
Copy link
Copy Markdown

Thanks - this helped!

I changed

  • default to prefer-light to be explicit
  • run each hour and on reboot for efficiency (Though at real time 0.007, its not really a worry to run each minute)
  • changed to bash, as I don't have zsh (quoted variables in tests)
#!/bin/bash
#
# Crotab settings
# Check each hour
# @reboot ~/bin/theme_switcher
# 0 * * * * ~/bin/theme_switcher

set_theme() {
        current_theme=$(gsettings get org.gnome.desktop.interface color-scheme)
        if [ "$1" = "dark" ]; then
                if [ "$current_theme" != "'prefer-dark'" ]; then
                        echo "Switching to dark theme"
                        gsettings set org.gnome.desktop.interface color-scheme prefer-dark
                fi
        else
                if [ "$current_theme" != "'prefer-light'" ]; then
                        echo "Switching to light theme"
                        gsettings set org.gnome.desktop.interface color-scheme prefer-light
                fi
        fi
}

for arg in "$@"; do
        case "$arg" in
                -d|--dark)  set_theme "dark";  exit 0 ;;
                -l|--light) set_theme "light"; exit 0 ;;
                -*)
                        echo "Invalid option: $arg" >&2
                        echo "Usage: $0 [-d|--dark] [-l|--light]" >&2
                        exit 1
                        ;;
        esac
done

current_hour=$(date +%H)

if [ "$current_hour" -ge 20 ] || [ "$current_hour" -lt 7 ]; then
        set_theme "dark"
else
        set_theme "light"
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment