16

Has anyone found a way to automatically switch between "light" and "dark" modes yet, with regards to the "Window Theme" in Ubuntu's settings (Settings → AppearanceWindow ThemeLight / Standard / Dark)?

Digging into the settings every morning or evening is a real pain. It'd be great to be able to automatically switch to "dark" mode at sunset, and automatically switch to "light" mode at sunrise.

0
13

For GNOME, this shell extension exists: Night Theme Switcher

It has quite a lot of options and already works out of the box, without having to configure anything, but the configuration is straight forward as well!

Screenshot of the Schedule tab

Screenshot of the GTK theme tab

3
  • This is a great extension, thanks for it. Mar 26 '21 at 8:28
  • I installed it but don't understand how to open it. Apr 6 '21 at 19:59
  • 1
    @RaffiKhatchadourian that's a thing with gnome extensions - it's really not clear how to open the settings if you don't know. there's a few ways. if you installed the extension using the gnome extensions browser add-on, you can open the preferences from there. otherwise, you might want to use gnome-tweaks or using the command line: gnome-extensions prefs nightthemeswitcher@romainvigier.fr
    – sk22
    Apr 13 '21 at 13:15
6

The terminal command for changing theme is:

gsettings set org.gnome.desktop.interface gtk-theme Yaru-dark

for the Yaru-dark theme, and

gsettings set org.gnome.desktop.interface gtk-theme Yaru-light

for the Yaru-light theme.

Now, there's something called cron-job for scheduling jobs (basically executing something, repeatedly at specific time). So, you can write a cron-job to execute these commands at specified times (something like change to dark theme at 9 PM and light theme at 6 AM).

Add the following to a file named script.sh:

#!/bin/bash
echo export DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS > lightscript.sh
echo export DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS > darkscript.sh
echo "gsettings set org.gnome.desktop.interface gtk-theme Yaru-light" >> lightscript.sh
echo "gsettings set org.gnome.desktop.interface gtk-theme Yaru-dark" >> darkscript.sh
chmod 755 lightscript.sh
chmod 755 darkscript.sh

currenttime=$(date +%H:%M)
if [[ "$currenttime" > "21:00" ]] || [[ "$currenttime" < "06:00" ]]; then
  ./darkscript.sh
else
  ./lightscript.sh
fi

Make the file executable running:

chmod 755 /path/to/script.sh

or:

chmod +x /path/to/script.sh

Run gnome-session-properties in terminal. Add a new start up program by clicking add on right side and selecting the script.sh file by browsing and save it with some name and comment. This will tell GNOME to create lightscript.sh and darkscript.sh whenever you login through GUI.

Add your job (change theme) to crontab by using the command:

crontab -e

and choosing a suitable editor or you can go to /var/spool/cron/crontabs and edit the file with your username. Accessing the file this way requires sudo privileges. Add the following two lines (with /path/to/ replaced by actual path):

0 6 * * * /path/to/lightscript.sh
0 21 * * * /path/to/darkscript.sh

It will say:

crontab: installing new crontab

after exiting the command. You can also check with:

crontab -l

This should do it. The above two lines tell cron to execute lightscript.sh at 6:00 AM and darkscript.sh at 9:00 PM everyday.

We are taking this detour instead of just adding:

0 6 * * * gsettings set org.gnome.desktop.interface gtk-theme Yaru-light

to crontab because this requires the DBUS_SESSION_BUS_ADDRESS variable to be set correctly.

13
  • nice answer....
    – UnKNOWn
    May 3 '20 at 17:36
  • I followed the instructions... We'll see in about half an hour whether I have done it right and / or how well it works. But why do your instructions refer to two different scripts at the end (lightscript.sh and darkscript.sh), when you are indicate that I should create a single script at the start (called "script.sh")? For the last section (with the reference to the two scripts), I changed the name to "script.sh" for both of them, because I only created a single script at the start... I also change the times (from "21" to "18", for 6PM). May 4 '20 at 7:29
  • It didn't work... :-( May 4 '20 at 8:13
  • So, the file "script.sh" is executed when you login (provided you added it in gnome-session-properties). Its work is to create lightscript.sh and darkscript.sh with proper value for DBUS_SESSION_BUS_ADDRESS. Then the crontab executes these lightscript.sh and darkscript.sh. Just restart and login and see if lightscript.sh and darkscript.sh is created where script.sh is present. Also try service cron status to see if cron is running
    – Shubhzgang
    May 4 '20 at 8:25
  • This wouldn't work with changes you made to the script names @GregoryOpera
    – Shubhzgang
    May 4 '20 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.