Last active
December 24, 2024 12:56
-
-
Save josemarcosrf/4de7a72c0c7a07afcb0b5f880dc9a2dc to your computer and use it in GitHub Desktop.
Bash script to toggle / change Gnome and Shell themes and Terminal profiles between dark and light modes
This file contains 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 | |
# Cron doesn't have access to env.vars as your user has, | |
# so we need to export these: | |
export TERM=xterm-256color | |
# Check current value with: 'echo $DBUS_SESSION_BUS_ADDRESS' and paste here | |
# see: https://stackoverflow.com/questions/53628122/git-libsecret-throws-cannot-autolaunch-d-bus-without-x11-display | |
export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus | |
# =========================================================== | |
# To be used with cron: | |
# Check every hour and change theme depending on the time. | |
# Create a crontab (i.e.: crontab -e) | |
# | |
# i.e: | |
# | |
# 0 * * * * /home/jose/bin/auto-toggle-theme.sh | |
# =========================================================== | |
# Import the theme setting functions | |
. /home/jose/bin/toggle-theme.sh --source-only | |
maybe-toggle-theme() { | |
light_time=$1 | |
current_gtk_theme=$(get-gtk-theme) | |
say @magenta "Current theme: @b@magenta $current_gtk_theme (light mode: $light_time)" | |
if [[ $current_gtk_theme != "'$GTK_DARK'" ]] && [[ $light_time == "nightlight" ]]; | |
then | |
say @blue "Switching to DARK mode:" | |
set-dark | |
elif [[ $current_gtk_theme != "'$GTK_LIGHT'" ]] && [[ $light_time == "daylight" ]]; | |
then | |
say @yellow "Switching to LIGHT mode:" | |
set-light | |
fi | |
} | |
# Get current time | |
current_time=$(date +'%T') | |
say @magenta "Current time: $current_time" | |
if [[ $current_time > '07:00' ]] && [[ $current_time < '20:00' ]]; | |
then | |
maybe-toggle-theme "daylight" | |
# elif [[ condition ]]; then | |
# #statements | |
else | |
maybe-toggle-theme "nightlight" | |
fi |
This file contains 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 | |
set -e | |
set -o pipefail | |
# Some useful commands to figure out what to tweak: | |
# https://askubuntu.com/questions/971067/how-can-i-script-the-settings-made-by-gnome-tweak-tool#971577 | |
# * watch dconf changes being made: | |
# 'dconf watch /' | |
# * List gsettings keys: | |
# 'gsettings list-keys org.gnome....' | |
# https://stackoverflow.com/questions/41156442/glib-gio-message-using-the-memory-gsettings-backend-happened-in-ubuntu-16-10 | |
export GIO_EXTRA_MODULES=/usr/lib/x86_64-linux-gnu/gio/modules/ | |
# Gnome Terminal themes | |
# Avaialable at: http://mayccoll.github.io/Gogh/ (https://github.com/Mayccoll/Gogh) | |
# After installing or creating the desired terminal themes; | |
# 1. Retrieve the ID: `dconf dump /org/gnome/terminal/legacy/profiles:/ | awk '/\[:/||/visible-name=/'` | |
# 2. Set which one to use for Dark and Light mode. | |
TERMINAL_LIGHT=b0c17716-3330-4773-a48e-6051c785e449 # Pencil Light | |
TERMINAL_DARK=7d341320-cabf-4ad3-91de-275e800647eb # Sweet Terminal | |
# GTK3 Themes | |
# Orchis: https://github.com/vinceliuice/Orchis-theme | |
# | |
# MacOS 11: https://www.pling.com/p/1220826 | |
# MacOS Sierra: https://www.pling.com/p/1013714 | |
# MacOS Mojave: https://www.pling.com/p/1275087 | |
# MacOS WhiteSur: https://github.com/vinceliuice/WhiteSur-gtk-theme | |
# | |
# For a complete article on the topic: https://itsfoss.com/make-ubuntu-look-like-macos/ | |
# | |
# NOTE: Themes should be installed first. i.e.: Copied to ~/.themes | |
GTK_LIGHT='Orchis-Light-Compact' # Or 'macOS11-GTK' | 'WhiteSur-light-blue' | |
GTK_DARK='Orchis-Dark-Compact' # Or 'Mojave-dark' | 'WhiteSur-dark-blue' | |
# Similar to the above but for the Gnome Shell (i.e.: top desktop bar, ...) | |
SHELL_LIGHT='Orchis-Light-Compact' # Or 'macOS11-Shell' | 'WhiteSur-light-blue' | |
SHELL_DARK='Orchis-Dark-Compact' # Or 'macOS11-Shell-Dark' | 'WhiteSur-dark-blue' | |
# Similar to the above but for OhMyBash themme (i.e.: bash prompt) | |
OSH_LIGHT='powerline-light' # Or 'powerline-jmrf' | |
OSH_DARK='powerline-light' # Or 'cupcake' | |
say() { | |
echo "$@" | sed \ | |
-e "s/\(\(@\(red\|green\|yellow\|blue\|magenta\|cyan\|white\|reset\|b\|u\)\)\+\)[[]\{2\}\(.*\)[]]\{2\}/\1\4@reset/g" \ | |
-e "s/@red/$(tput setaf 1)/g" \ | |
-e "s/@green/$(tput setaf 2)/g" \ | |
-e "s/@yellow/$(tput setaf 3)/g" \ | |
-e "s/@blue/$(tput setaf 4)/g" \ | |
-e "s/@magenta/$(tput setaf 5)/g" \ | |
-e "s/@cyan/$(tput setaf 6)/g" \ | |
-e "s/@white/$(tput setaf 7)/g" \ | |
-e "s/@reset/$(tput sgr0)/g" \ | |
-e "s/@b/$(tput bold)/g" \ | |
-e "s/@u/$(tput sgr 0 1)/g" | |
} | |
# ============================================= | |
# ================ Gnome Shell ================ | |
set-shell-theme() { | |
theme=$1 | |
say @blue "Setting SHELL theme to: @b$theme" | |
# gsettings doesn't have a scheme for this, so we use dconf directly | |
dconf write /org/gnome/shell/extensions/user-theme/name "'$theme'" | |
} | |
get-shell-theme() { | |
echo $(gsettings get org.gnome.shell.extensions.user-theme name) | |
} | |
# ============================================= | |
# ================ GTK Theme ================== | |
set-gtk-theme() { | |
theme=$1 | |
say @blue "Setting GTK theme to: @b $theme" | |
gsettings set org.gnome.desktop.interface gtk-theme $theme | |
} | |
get-gtk-theme() { | |
echo $(gsettings get org.gnome.desktop.interface gtk-theme) | |
} | |
# ============================================= | |
# ================ Terminal =================== | |
set-terminal-profile() { | |
theme=$1 | |
say @blue "Setting TERMINAL theme to: @b$theme" | |
gsettings set org.gnome.Terminal.ProfilesList default $theme | |
} | |
get-terminal-profile() { | |
# dconf dump /org/gnome/terminal/legacy/profiles:/ | awk '/\[:/||/visible-name=/' | |
echo $(gsettings get org.gnome.Terminal.ProfilesList default) | |
} | |
swap-osh-theme() { | |
current=$1 | |
next=$2 | |
sed -i "s/^OSH_THEME=\""$current\""/OSH_THEME=\""$next\""/g" /home/$USER/.bashrc | |
} | |
set-light() { | |
set-gtk-theme $GTK_LIGHT | |
set-shell-theme $SHELL_LIGHT | |
set-terminal-profile $TERMINAL_LIGHT | |
swap-osh-theme $OSH_DARK $OSH_LIGHT | |
} | |
set-dark() { | |
set-gtk-theme $GTK_DARK | |
set-shell-theme $SHELL_DARK | |
set-terminal-profile $TERMINAL_DARK | |
swap-osh-theme $OSH_LIGHT $OSH_DARK | |
} | |
toggle-theme() { | |
current_gtk_theme=$(get-gtk-theme) | |
say @blue "Current theme: @b@magenta $current_gtk_theme" | |
if [ $current_gtk_theme = "'$GTK_LIGHT'" ]; | |
then | |
say @blue "Switching to DARK mode:" | |
set-dark | |
else | |
say @blue "Switching to LIGHT mode:" | |
set-light | |
fi | |
} | |
# Toggle between Light & Dark | |
if [ "${1}" != "--source-only" ]; then | |
# main "${@}" | |
toggle-theme | |
fi |
This file contains 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 | |
set -e | |
set -o pipefail | |
# List the installed themes with: 'lookandfeeltool -l org.kde.breezedark.desktop' | |
KDE_LIGHT_THEME="org.kde.breeze.desktop" | |
KDE_DARK_THEME="org.kde.breezedark.desktop" | |
# List of currently existing profiles with: 'konsole --list-profiles' | |
KONSOLE_LIGHT_THEME="Light" | |
KONSOLE_DARK_THEME="Dark" | |
cprint() { | |
echo "$@" | sed \ | |
-e "s/\(\(@\(red\|green\|yellow\|blue\|magenta\|cyan\|white\|reset\|b\|u\)\)\+\)[[]\{2\}\(.*\)[]]\{2\}/\1\4@reset/g" \ | |
-e "s/@red/$(tput setaf 1)/g" \ | |
-e "s/@green/$(tput setaf 2)/g" \ | |
-e "s/@yellow/$(tput setaf 3)/g" \ | |
-e "s/@blue/$(tput setaf 4)/g" \ | |
-e "s/@magenta/$(tput setaf 5)/g" \ | |
-e "s/@cyan/$(tput setaf 6)/g" \ | |
-e "s/@white/$(tput setaf 7)/g" \ | |
-e "s/@reset/$(tput sgr0)/g" \ | |
-e "s/@b/$(tput bold)/g" \ | |
-e "s/@u/$(tput sgr 0 1)/g" | |
} | |
# ============================================= | |
# ================ KDE Theme ================== | |
get-kde-theme() { | |
curremt_theme=$(grep 'LookAndFeelPackage=' ~/.config/kdeglobals | awk -F'=' '{print $2}') | |
echo $curremt_theme | |
} | |
set-kde-theme() { | |
theme=$1 | |
cprint @blue "Setting KDE theme to: @b $theme" | |
lookandfeeltool -a $theme | |
} | |
# ============================================= | |
# ============== Konsole Theme ================ | |
switch-konsole-profile() { | |
# NOTE: This requires the Konsole profile to exist and configured as a LIght/Dark theme. | |
# NOTE: Requires qdbus: 'sudo dnf install qt5-qttools' | |
profile=$1 | |
cprint @blue "Setting Konsole profile to: @b $profile" | |
# This changes the default profile for every Konsole window from here onward | |
qdbus org.kde.konsole $KONSOLE_DBUS_SESSION org.kde.konsole.Session.setProfile $profile | |
# This iterates over the open Konsole instances and changes to the desired profile | |
for instance in $(qdbus-qt6 | grep org.kde.konsole); do | |
# Iterate over all sessions within each instance | |
for session in $(qdbus-qt6 "$instance" | grep -E '^/Sessions/'); do | |
# Set the profile for each session | |
qdbus-qt6 "$instance" "$session" org.kde.konsole.Session.setProfile "$profile" | |
done | |
done | |
} | |
toggle-theme() { | |
current_gtk_theme=$(get-kde-theme) | |
cprint @blue "Current theme: @b@magenta $current_gtk_theme" | |
if [[ $current_gtk_theme == $KDE_LIGHT_THEME ]]; | |
then | |
cprint @blue "Switching to DARK mode" | |
set-kde-theme $KDE_DARK_THEME | |
switch-konsole-profile $KONSOLE_DARK_THEME | |
else | |
cprint @blue "Switching to LIGHT mode" | |
set-kde-theme $KDE_LIGHT_THEME | |
switch-konsole-profile $KONSOLE_LIGHT_THEME | |
fi | |
} |
To run the auto-toggle-theme.sh
at login
Add in ~/.profile
:
bash $HOME/bin/auto-toggle-theme.sh > /dev/null 2>&1
To run the auto-toggle-theme.sh
on resume
:
⚠️ : This might not work asdconf
might not be connected
Create a new file @ /lib/systemd/system-sleep/resume-theme.sh
, with:
#!/bin/sh
case $1/$2 in
pre/*)
echo "Going to $2..."
# Place your pre suspend commands here, or `exit 0`
# if no pre suspend action required
exit 0
;;
post/*)
echo "Waking up from $2..."
# Place your post suspend (resume) commands here, or `exit 0`
# if no post suspend action required
/bin/bash /home/jose/bin/auto-toggle-theme.sh > /dev/null 2>&1
;;
esac
On Fedora with KDE the auto-toggle based on datetime can be achieved with: Koi
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some useful resources