I recently got a Framework laptop and installed Ubuntu on it to give Linux for laptops a chance after using Windows and Mac for work for years. One thing I wanted was to be able to switch between light mode and dark mode automatically depending on the time of day. GNOME had a blue-light filter mode that could automatically turn on, but it didn’t appear to have a way to switch between light mode and dark mode at the same time.
Research
Sort of Working
The following command seemed to switch the theme, but not all apps would follow. The terminal and GNOME would follow, but Firefox wouldn’t.
1
2
gsettings set org.gnome.desktop.interface gtk-theme Yaru-dark
gsettings set org.gnome.desktop.interface gtk-theme Yaru
#!/bin/bash
set_theme(){if[["$1"=="dark"]];thennew_gtk_theme="prefer-dark"elif[["$1"=="light"]];thennew_gtk_theme="prefer-light"elseecho"[!] Unsupported theme: $1"returnfiexportDBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESSecho"$DBUS_SESSION_BUS_ADDRESS"current_gtk_theme=$(gsettings get org.gnome.desktop.interface color-scheme)# echo "[.] Currently using ${current_gtk_theme}"if[["${current_gtk_theme}"=="'${new_gtk_theme}'"]];thenecho"[i] Already using gtk '${new_gtk_theme}' theme"elseecho"[-] Setting gtk theme to ${new_gtk_theme}" gsettings set org.gnome.desktop.interface color-scheme ${new_gtk_theme}echo"[✓] gtk theme changed to ${new_gtk_theme}"fi}# If script run without argumentif[[ -z "$1"]];thencurrenttime=$(date +%H:%M)if[["$currenttime" > "17:00"||"$currenttime" < "09:00"]];then set_theme dark
else set_theme light
fielse set_theme $1fi
Move the file into a safe location. I use /usr/bin/ so it’s not user-writable by default:
[Unit]Description=Auto adjusts GNOME theme between dark and light to match the timeAfter=suspend.target[Service]Type=oneshotExecStart=/usr/bin/gnome-theme-switcher.shTimeoutSec=30StandardOutput=journalPrivateTmp=trueProtectSystem=fullProtectHome=read-only[Install]WantedBy=multi-user.target
The timer defines when the command should run. I choose to run mine at 8pm and 9am. If you change it, make sure to update the script above.
[Unit]Description=Auto adjusts GNOME theme between dark and light to match the time[Timer]OnCalendar=*-*-* 20:00:00 # 8pmOnCalendar=*-*-* 09:00:00 # 9am[Install]WantedBy=timers.target