2

I am a user of Linux Mint. One feature I often wish I had in Linux is the ability to switch between light and dark modes depending on the time of the day, as in macOS and mobile devices.

I have put together the following script for switching from light to dark mode:

#!/bin/bash
notify-send "Switching to dark mode"
gsettings set org.cinnamon.desktop.wm.preferences theme Mint-Y-Dark
gsettings set org.cinnamon.desktop.interface gtk-theme Mint-Y-Dark
gsettings set org.cinnamon.theme name Mint-Y-Dark

and I have another one for the opposite direction

#!/bin/bash
gsettings set org.cinnamon.desktop.wm.preferences theme Mint-Y
gsettings set org.cinnamon.desktop.interface gtk-theme Mint-Y
gsettings set org.cinnamon.theme name Mint-Y

Currently, I run these manually when I want to switch between the two themes.

Most programs respect the OS-wide theme meaning this method works surprisingly well! However, there are three problems that I would like to solve with this approach:

  1. Automated switching between light/dark modes depending on the time of day (as in macOS). I tried using cron and anacron but they don't work well for this purpose. When my computer is turned on after a period of being off, I would like it to realise that the time to switch has passed and perform the mode switch (as in macOS and various mobile devices);
  2. I would like my terminal (GNOME Terminal, Cinnamon default) to switch from a light theme to a dark theme (and vice versa); and
  3. I would like my text editor (Emacs) to load a light/dark theme.

Even getting just (1) to work would be quite nice!

affibern
  • 121
  • 2
  • 1
    what do you mean `...but they don't work well for this purpose.`? easiest way is to script them in a file and use crontab to run it – αғsнιη Nov 18 '20 at 21:21
  • `cron` simply doesn't work when my computer is off. I tried using `anacron` but that didn't work either and I'm not sure why. If you have a specific suggestion for using `anacron` for this purpose, can you please add it as an answer? – affibern Nov 19 '20 at 15:45
  • `anacron` has a minimum daily period. That means, you should use two different commands for two different times. But when you restart or cold start your laptop many times per day, no additional executions would happen for this day. – thanasisp Nov 19 '20 at 15:55

1 Answers1

1

It's better to have one script with all your commands to force the light or dark mode. Here is an example:

#!/bin/bash

function set_light_mode () {
    echo "setting light mode"
}

function set_dark_mode () {
    echo "setting dark mode"
}

case "$1" in
    light)
        set_light_mode
        ;;
    dark)
        set_dark_mode
        ;;
    *)
        light_time="0800"
        dark_time="1700"
        d="$(date +"%H%M")"

        if [ "$d" -ge "$light_time" ] && [ "$d" -lt "$dark_time" ]; then
            set_light_mode
        else
            set_dark_mode
        fi
        ;;
esac

Now you can:

  • execute it whenever you want with one command. Either provide the first argument, dark or light to set your X regardless of time. Or provide no arguments and current time will decide.
  • add 2 entries in the crontab for the above times (without arguments) or for any times. When your laptop is on, they will force the X settings depending on current time.
  • add a command to execute the script (without arguments) into the "Autostart Applications" of your favourite DE, using the GUI like this, the process is similar for Xfce, Kde, Gnome, etc, they all follow some standard specifications. You can do it via console also.

Note: We don't check for if X is running in the above, I guess your commands don't need that (except maybe send-notify that I guess could just fail in sush a case)

You can find and add more commands into the above functions to switch themes, modes etc. for your DE or some of your programms, whatever can be done via console is well documented.

thanasisp
  • 7,802
  • 2
  • 26
  • 39