Last active
July 12, 2025 18:08
-
-
Save brunomvsouza/21e5cbe75aefefb9d46a79bed7b6acc1 to your computer and use it in GitHub Desktop.
Ubuntu Theme Switcher Script for Cron (tested on Ubuntu 24.04)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Add to `crontab -e` | |
# */1 * * * * /path/to/ubuntu_theme_switcher.sh | |
# @reboot /path/to/ubuntu_theme_switcher.sh | |
# Modified version of https://askubuntu.com/a/1407874 | |
set_theme() { | |
if [[ "$1" == "dark" ]]; then | |
new_color_scheme="prefer-dark" | |
elif [[ "$1" == "light" ]]; then | |
new_color_scheme="prefer-light" | |
else | |
echo "[!] Unsupported theme: $1" | |
return | |
fi | |
# Must be set to work on cron. Get your value running `echo $DBUS_SESSION_BUS_ADDRESS`. | |
export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/bus" | |
# Get the current color-scheme setting | |
current_color_scheme=$(gsettings get org.gnome.desktop.interface color-scheme) | |
# Note the quotes around the value from gsettings | |
if [[ "${current_color_scheme}" == "'${new_color_scheme}'" ]]; then | |
echo "[i] Already using '${new_color_scheme}' color scheme" | |
else | |
echo "[-] Setting color scheme to ${new_color_scheme}" | |
# Set the new color-scheme | |
gsettings set org.gnome.desktop.interface color-scheme ${new_color_scheme} | |
# Optionally set theme to Yaru-dark anyways, in my experience things look better this way | |
#gsettings set org.gnome.desktop.interface gtk-theme Yaru-dark | |
notify-send "[✓] Color scheme changed to ${new_color_scheme}" | |
fi | |
} | |
# If no argument was provided | |
if [[ -z "$1" ]]; then | |
currenttime=$(date +%H:%M) | |
if [[ "$currenttime" > "17:30" || "$currenttime" < "06:00" ]]; then | |
set_theme dark | |
else | |
set_theme light | |
fi | |
else | |
set_theme $1 | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment