How to Automatically Switch Between Light and Dark Themes?

I would like my system to automatically switch to dark mode after dusk. After some searching I came across this topic. However, I get the following when I attempt to run the commands it mentioned.
No such schema “org.mate.desktop.interface”

I figure a new thread is appropriate, since this is a different version of Ubuntu MATE and possibly a different way of accomplishing this task.

I have this script which sets it to dark mode:

dconf read /org/mate/desktop/interface/gtk-theme
dconf write /org/mate/marco/general/theme "'Ambiant-MATE-Dark'"
dconf write /org/mate/desktop/interface/gtk-theme "'Ambiant-MATE-Dark'"

The script works flawlessly when ran standalone, but fails when put into the crontab as follows:
* * * * * /home/mike/Documents/Scripts/ 1> /home/mike/log.txt 2> /home/mike/err.txt

It puts the following error in my log file:
error: Cannot autolaunch D-Bus without X11 $DISPLAY

Any thoughts?

After googling the error message. I modified the command to be
DISPLAY=:0.0 dconf write /org/mate/desktop/interface/gtk-theme "'Ambiant-MATE-Dark'"

No more errors logged, but it still doesn't work. Again, the command works perfectly in the terminal or as a standalone script but does nothing in cron.

export the .Xauthority file as well, like

XAUTHORITY=/home/mike/.Xauthority before or after the DISPLAY=:0.0

Remember, for X apps both the XAUTHORITY and DISPLAY env vars need to be set.


I'm still not having any luck.

The following command runs successfully for me in the terminal:
XAUTHORITY=/home/mike/.Xauthority DISPLAY=:0.0 dconf write /org/mate/desktop/interface/gtk-theme "'Ambiant-MATE-Dark'"

However, the following entry in my crontab doesn't appear to do anything when it runs:
* * * * * XAUTHORITY=/home/mike/.Xauthority DISPLAY=:0.0 dconf write /org/mate/desktop/interface/gtk-theme "'Ambiant-MATE-Dark'"

The following gets logged in /var/log/syslog every minute:
Apr 11 14:40:01 mike-H97-D3H CRON[10126]: (mike) CMD (XAUTHORITY=/home/mike/.Xauthority DISPLAY=:0.0 dconf write /org/mate/desktop/interface/gtk-theme "'Ambiant-MATE-Dark'")

The following also gets logged in /var/log/syslog at odd intervals; not sure if it's related or not:
Apr 11 14:40:11 mike-H97-D3H python3[1449]: Received a timestamp of 0; window activation may not function properly.

Mike, can you try this script ...

export XAUTHORITY=/home/mike/.Xauthority
export DISPLAY=:0.0
gsettings set org.mate.interface gtk-theme 'Ambiant-MATE-Dark'
1 Like

@mtnarwhal One more thing, gsettings needs a DBUS session connection to be set, add the line below to my script ... (you don't need to export XAUTH and DISPLAY, DBUS takes care of things)

export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/mike/1000/bus

Tested on UM20.04 --

  • on one terminal I do gsettings to Ambiant-MATE,
  • after 1 min the script kicks in and makes it Ambiant-MATE-Dark

Here's the final script:

export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/mike/1000/bus

gsettings set org.mate.interface gtk-theme 'Ambiant-MATE-Dark'
sleep 30s
gsettings set org.mate.interface gtk-theme 'Ambiant-MATE'

At min 1, theme turns dark, 30sec later turns normal, script exits.
At min 2, same thing.

1 Like

Hi @mtnarwhal :slight_smile:
Just adding a tiny thing to the perfect and professional answer of @pavlos_kairis

Additinally to your theme switcher, you can use redshift (pre-installed on your UM). This software make the screen colors more red to avoid blue light. It is changing the gamma automaticly depending on your localisation and the day time.
My config : .config/redshift.conf


Don't forget to put your own latitude and longitude.
Playing those numbers will mqke your screen looks more red :slight_smile:


Next just run redshift and enable it with the panel indicator.