Skip to content

Instantly share code, notes, and snippets.

@mojoaxel
Last active August 29, 2025 07:34
Show Gist options
  • Save mojoaxel/2f0f745f654134e9e7efea286f652a55 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment