Skip to content

Instantly share code, notes, and snippets.

@nilesh-akhade
Last active May 11, 2022 04:41
Show Gist options
  • Save nilesh-akhade/c1ba284461c75fbd7c887cf6f59aeccf to your computer and use it in GitHub Desktop.
Save nilesh-akhade/c1ba284461c75fbd7c887cf6f59aeccf to your computer and use it in GitHub Desktop.
Script that auto switches VSCode and Ubuntu themes to Dark or Light mode
#!/bin/bash
# Script that auto switches VSCode and Ubuntu themes to Dark or Light
# depending on time of day
# Copy this file to /usr/local/bin/my-auto-theme
# Add following lines to crontab -e
# 0 9 * * * bash /usr/bin/local/my-auto-theme light
# 0 17 * * * bash /usr/bin/local/my-auto-theme dark
# @reboot bash /usr/bin/local/my-auto-theme
set_theme() {
if [[ "$1" == "dark" ]]; then
new_gtk_theme="Yaru-dark"
new_vscode_theme="Visual Studio Dark"
elif [[ "$1" == "light" ]]; then
new_gtk_theme="Yaru-light"
new_vscode_theme="Visual Studio Light"
else
echo "[!] Unsupported theme: $1"
return
fi
vscode_settings_file=${HOME}/.config/Code/User/settings.json
current_vscode_theme=$(jq -r -M '."workbench.colorTheme"' ${vscode_settings_file})
if [[ ${current_vscode_theme} == ${new_vscode_theme} ]]; then
echo "[i] Already using vscode '${new_vscode_theme}' theme"
else
echo "[-] Setting vscode theme to ${new_vscode_theme}"
tmp_file=$(mktemp)
jq -r -M ".\"workbench.colorTheme\" = \"${new_vscode_theme}\"" ${vscode_settings_file} > ${tmp_file}
mv ${tmp_file} ${vscode_settings_file}
echo "[✓] vscode theme changed to ${new_vscode_theme}"
fi
# https://askubuntu.com/a/1234819/895417
export DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS
current_gtk_theme=$(gsettings get org.gnome.desktop.interface gtk-theme)
# echo "[.] Currently using ${current_gtk_theme}"
if [[ "${current_gtk_theme}" == "'${new_gtk_theme}'" ]]; then
echo "[i] Already using gtk '${new_gtk_theme}' theme"
else
echo "[-] Setting gtk theme to ${new_gtk_theme}"
gsettings set org.gnome.desktop.interface gtk-theme ${new_gtk_theme}
echo "[✓] gtk theme changed to ${new_gtk_theme}"
fi
}
if [[ -z "$1" ]]; then
currenttime=$(date +%H:%M)
if [[ "$currenttime" > "17:00" || "$currenttime" < "09:00" ]]; then
set_theme dark
else
set_theme light
fi
else
set_theme $1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment