Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Set of commands and files to enable timed theme (dark/light) change
kretyn@meland:~/$ mkdir -p ~/.config/systemd/user
kretyn@meland:~/$ mkdir -p ~/.scripts
kretyn@meland:~/$ cd ~/.config/systemd/user
kretyn@meland:~/.config/systemd/user$ cat light.service
[Unit]
Description=Automatically change the "Window Theme" to "light" in the morning.
[Service]
Environment=DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
ExecStart=/home/kretyn/.scripts/profile_changer.sh light
kretyn@meland:~/.config/systemd/user$ cat light.timer
[Unit]
Description=Automatically change the "Window Theme" to "light" in the morning.
[Timer]
OnCalendar=*-*-* 06:00:00
Persistent=true
[Install]
WantedBy=default.target
kretyn@meland:~/.config/systemd/user$ cat dark.service
[Unit]
Description=Automatically change the "Window Theme" to "dark" in the evening.
[Service]
Environment=DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
ExecStart=/home/kretyn/.scripts/profile_changer.sh dark
kretyn@meland:~/.config/systemd/user$ cat dark.timer
[Unit]
Description=Automatically change the "Window Theme" to "dark" in the evening.
[Timer]
OnCalendar=*-*-* 19:00:00
Persistent=true
[Install]
WantedBy=default.target
kretyn@meland:~/$ cat ~/.scripts/profile_changer.sh
#!/bin/bash
get_uuid() {
# Print the UUID linked to the profile name sent in parameter
local profile_name=$1
profiles=($(gsettings get org.gnome.Terminal.ProfilesList list | tr -d "[]\',"))
for i in ${!profiles[*]}
do
local uuid="$(dconf read /org/gnome/terminal/legacy/profiles:/:${profiles[i]}/visible-name)"
if [[ "${uuid,,}" = "'${profile_name,,}'" ]]
then echo "${profiles[i]}"
return 0
fi
done
echo "$profile_name"
}
if [ $1 == "dark" ]; then
THEME='Yaru-dark'
elif [ $1 == "light" ]; then
THEME='Yaru-light'
fi
UUID=$(get_uuid $1)
/usr/bin/gsettings set org.gnome.desktop.interface gtk-theme $THEME
/usr/bin/gsettings set org.gnome.Terminal.ProfilesList default $UUID
kretyn@meland:~$ systemctl --user daemon-reload
kretyn@meland:~$ systemctl --user enable dark.timer light.timer
kretyn@meland:~$ systemctl --user start dark.timer light.timer
@W-Floyd

This comment has been minimized.

Copy link

@W-Floyd W-Floyd commented Oct 4, 2020

It took me a while to figure it out, but you can add this to change all currently running terminal windows to the correct theme also:

gdbus introspect -e -x -d org.gnome.Terminal -o /org/gnome/Terminal/window |
  xmllint --xpath '/node/node/@name' - |
  sed -e 's/name="\([0-9]*\)"/\1\n/g' -e 's/ //g' |
  while read -r __node; do
    gdbus call -e -d org.gnome.Terminal -o /org/gnome/Terminal/window/${__node} -m org.gtk.Actions.SetState profile "<'${UUID}'>" "{}" >/dev/null
  done

Note that this only changes the active terminal tab.

@laszukdawid

This comment has been minimized.

Copy link
Owner Author

@laszukdawid laszukdawid commented Oct 16, 2020

Neat @W-Floyd, Thanks! :)

I had problem to execute this but it turned out that the xmllint isn't installed by default. Needed to install libxml2-utils (sudo apt install libxml2-utils).

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