Skip to content

Instantly share code, notes, and snippets.

@ImranR98
Last active January 2, 2025 20:13

Revisions

  1. ImranR98 revised this gist Aug 28, 2021. 1 changed file with 4 additions and 7 deletions.
    11 changes: 4 additions & 7 deletions eOSUDS.sh
    Original file line number Diff line number Diff line change
    @@ -10,18 +10,15 @@
    # It also optionally changes the wallpaper based on the theme
    # It also supports changing themes for the Mailspring email app (both .deb and Snap versions)

    # Use cron jobs to run this script automatically on a desired schedule; learn about cron by running `man crontab` in a terminal
    # Note that system updates can reset the forced Flatpak theming, so running the script frequently (every few minutes) is recommended


    # =======================================
    # Instructions
    # =======================================

    # 1. Change the configuration variables below as needed
    # 2. Save this script, make it executable, and set it up to run in the background on startup
    # This can be done on elementaryOS by adding the command to Applications -> Startup in the system settings

    # 1. Save this script and make it executable
    # 2. Change the configuration variables below as needed
    # 3. Use a cron job to run the script automatically on a desired schedule; you can learn about cron by running `man crontab` in a terminal
    # Note that system updates can reset the forced Flatpak theming, so running the script frequently (every few minutes) is recommended

    # =======================================
    # Configuration
  2. ImranR98 revised this gist Aug 28, 2021. No changes.
  3. ImranR98 revised this gist Aug 28, 2021. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions eOSUDS.sh
    Original file line number Diff line number Diff line change
    @@ -10,17 +10,17 @@
    # It also optionally changes the wallpaper based on the theme
    # It also supports changing themes for the Mailspring email app (both .deb and Snap versions)

    # Use cron jobs to run this script automatically on a desired schedule. Learn about cron by running `man crontab` in a terminal.
    # Note that system updates can reset the forced Flatpak theming, so running the script frequently (every few minutes) is recommended.
    # Use cron jobs to run this script automatically on a desired schedule; learn about cron by running `man crontab` in a terminal
    # Note that system updates can reset the forced Flatpak theming, so running the script frequently (every few minutes) is recommended


    # =======================================
    # Instructions
    # =======================================

    # 1. Change the configuration variables below as needed.
    # 2. Save this script, make it executable, and set it up to run in the background on startup.
    # This can be done on elementaryOS by adding the command to Applications -> Startup in the system settings.
    # 1. Change the configuration variables below as needed
    # 2. Save this script, make it executable, and set it up to run in the background on startup
    # This can be done on elementaryOS by adding the command to Applications -> Startup in the system settings


    # =======================================
  4. ImranR98 revised this gist Aug 28, 2021. 1 changed file with 24 additions and 46 deletions.
    70 changes: 24 additions & 46 deletions eOSUDS.sh
    Original file line number Diff line number Diff line change
    @@ -6,10 +6,13 @@
    # =======================================

    # elementaryOS 6 has great dark themes, but they only apply to "curated" appcenter apps
    # This script monitors changes to the system theme and forces it to work with regular Debian packages as well as non-curated Flatpak apps
    # This script forces the system theme to work with regular Debian packages as well as non-curated Flatpak apps
    # It also optionally changes the wallpaper based on the theme
    # It also supports changing themes for the Mailspring email app (both .deb and Snap versions)

    # Use cron jobs to run this script automatically on a desired schedule. Learn about cron by running `man crontab` in a terminal.
    # Note that system updates can reset the forced Flatpak theming, so running the script frequently (every few minutes) is recommended.


    # =======================================
    # Instructions
    @@ -30,35 +33,25 @@ MAILSPRING_LIGHT_THEME='ui-light' # Leave empty to disable Mailspring theme swit
    MAILSPRING_DARK_THEME='ui-dark' # Leave empty to disable Mailspring theme switching on dark theme
    SHOW_DEBUG_MESSAGES=0 # If debug logs should be printed, set to 1

    # Timing variables - how often to check whether a specific change needs to be made (and make it) - all in seconds, whole numbers only
    GTK_UNIVERSAL_DARK_PREF_CHECK_INTERVAL=30 # Ensure the GTK universal dark theme is in line with the system dark style
    WALLPAPER_CHECK_INTERVAL=30 # Ensure the current wallpaper is in line with the system dark style based on the variables above
    MAILSPRING_THEME_CHECK_INTERVAL=30 # Ensure the current Mailspring theme is in line with the system dark style based on the variables above
    NON_ELEMENTARY_FLATPAK_THEME_CHECK_INTERVAL=60 # Ensure non-Elementary Flatpak apps use the current system theme - more CPU intensive, better to do less frequently


    # =======================================
    # Script functions
    # =======================================

    # Echoes provided string with a date prefix. If $2 is 1, redirects to stderr with ERROR prefix. If $3 is 1, echoes a crash message to stderr and exits with code 1
    # If $4 is 1, the message is treated as a debug log (only shown if the relevant configuration variable above is set)
    TERM_RED="\e[0;31m"
    TERM_BLUE="\e[0;34m"
    TERM_BOLD="\e[1m"
    TERM_RESET="\e[0m"
    log() {
    if [ "$2" == 1 ]; then
    echo -e "$TERM_RED""ERROR: $(date): $1""$TERM_RESET" >&2
    echo -e "ERROR: $(date): $1" >&2
    elif [ "$4" == 1 ]; then
    if [ "$SHOW_DEBUG_MESSAGES" == 1 ]; then
    echo -e "$TERM_BLUE""DEBUG: $(date): $1""$TERM_RESET"
    echo -e "DEBUG: $(date): $1"
    fi
    else
    echo -e "-INFO: $(date): $1""$TERM_RESET"
    echo -e "MESSG: $(date): $1"
    fi
    if [ "$3" == 1 ]; then
    echo -e "$TERM_RED""$TERM_BOLD""CRASHED.""$TERM_RESET" >&2
    echo -e "CRASHED." >&2
    exit 1
    fi

    @@ -85,7 +78,7 @@ changeGTKUniversalDarkPrefIfNeeded() {
    [ "$GTK_UNIVERSAL_PREF_IS_DARK" == 0 ] || [ "$GTK_UNIVERSAL_PREF_IS_DARK" == 1 ] || log "$GTK_UNIVERSAL_PREF_FILE is not valid" 1 1
    # If needed, make the change
    if [ "$GTK_UNIVERSAL_PREF_IS_DARK" != "$1" ]; then
    echo "$(cat "$GTK_UNIVERSAL_PREF_FILE" | head -c -2)""$1" > "$GTK_UNIVERSAL_PREF_FILE"
    sed -i "s/gtk-application-prefer-dark-theme=.*/gtk-application-prefer-dark-theme=$1/" "$GTK_UNIVERSAL_PREF_FILE"
    log "GTK universal dark preference has been set to $1"
    else
    log "GTK universal dark preference is already $1, no changes made" 0 0 1
    @@ -98,6 +91,11 @@ changeWallpaperIfNeeded() {
    if [ ! -z "$1" ]; then
    CURRENT_WALLPAPER="$(gsettings get org.gnome.desktop.background picture-uri | head -c -2 | tail -c +9)"
    if [ "$CURRENT_WALLPAPER" != "$1" ]; then
    # This if statement allows the wallpaper change to work even when running from a cron job
    if [ -z "$DBUS_SESSION_BUS_ADDRESS" ]; then
    PID=$(pgrep gnome-session)
    export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ | tr '\0' '\n' | cut -d= -f2-)
    fi
    gsettings set org.gnome.desktop.background picture-uri file://"$1"
    log "Wallpaper has been set to $1"
    else
    @@ -206,33 +204,13 @@ changeNonElementaryFlatpakThemesIfNeeded() {
    # Actual execution
    # =======================================

    # In a continuous loop:
    # 1. Every GTK_UNIVERSAL_DARK_PREF_CHECK_INTERVAL seconds, changes the universal GTK dark theme preference if needed
    # 2. Every WALLPAPER_CHECK_INTERVAL seconds, changes the wallpaper if needed
    # 3. Every MAILSPRING_THEME_CHECK_INTERVAL seconds, changes the Mailspring theme if needed
    # 4. Every NON_ELEMENTARY_FLATPAK_THEME_CHECK_INTERVAL seconds, changes the theme of all non-elementary Flatpak apps if needed

    log "Loop started" 0 0 1
    GTK_UNIVERSAL_DARK_PREF_CHECK_TIMER="$GTK_UNIVERSAL_DARK_PREF_CHECK_INTERVAL"
    WALLPAPER_CHECK_TIMER="$WALLPAPER_CHECK_INTERVAL"
    MAILSPRING_THEME_CHECK_TIMER="$MAILSPRING_THEME_CHECK_INTERVAL"
    NON_ELEMENTARY_FLATPAK_THEME_CHECK_TIMER="$NON_ELEMENTARY_FLATPAK_THEME_CHECK_INTERVAL"
    while [ true ]; do
    if [ "$GTK_UNIVERSAL_DARK_PREF_CHECK_TIMER" == "$GTK_UNIVERSAL_DARK_PREF_CHECK_INTERVAL" ]; then
    changeGTKUniversalDarkPrefIfNeeded "$(getSystemDarkStylePref)"
    GTK_UNIVERSAL_DARK_PREF_CHECK_TIMER=0
    fi
    if [ "$WALLPAPER_CHECK_TIMER" == "$WALLPAPER_CHECK_INTERVAL" ]; then
    changeWallpaperIfNeeded "$([ "$(getSystemDarkStylePref)" == 1 ] && echo "$DARK_THEME_WALLPAPER" || echo "$LIGHT_THEME_WALLPAPER")"
    WALLPAPER_CHECK_TIMER=0
    fi
    if [ "$MAILSPRING_THEME_CHECK_TIMER" == "$MAILSPRING_THEME_CHECK_INTERVAL" ]; then
    changeMailSpringThemeIfNeeded "$([ "$(getSystemDarkStylePref)" == 1 ] && echo "$MAILSPRING_DARK_THEME" || echo "$MAILSPRING_LIGHT_THEME")"
    MAILSPRING_THEME_CHECK_TIMER=0
    fi
    if [ "$NON_ELEMENTARY_FLATPAK_THEME_CHECK_TIMER" == "$NON_ELEMENTARY_FLATPAK_THEME_CHECK_INTERVAL" ]; then
    changeNonElementaryFlatpakThemesIfNeeded "$(getSystemDarkStylePref)"
    NON_ELEMENTARY_FLATPAK_THEME_CHECK_TIMER=0
    fi
    sleep 1
    done
    # 1. Change the universal GTK dark theme preference if needed
    # 2. Change the wallpaper if needed
    # 3. Change the Mailspring theme if needed
    # 4. Change the theme of all non-elementary Flatpak apps if needed

    SYSTEM_DARK_PREF="$(getSystemDarkStylePref)"
    changeGTKUniversalDarkPrefIfNeeded "$SYSTEM_DARK_PREF"
    changeWallpaperIfNeeded "$([ "$SYSTEM_DARK_PREF" == 1 ] && echo "$DARK_THEME_WALLPAPER" || echo "$LIGHT_THEME_WALLPAPER")"
    changeMailSpringThemeIfNeeded "$([ "$SYSTEM_DARK_PREF" == 1 ] && echo "$MAILSPRING_DARK_THEME" || echo "$MAILSPRING_LIGHT_THEME")"
    changeNonElementaryFlatpakThemesIfNeeded "$SYSTEM_DARK_PREF"
  5. ImranR98 revised this gist Aug 24, 2021. 1 changed file with 52 additions and 44 deletions.
    96 changes: 52 additions & 44 deletions eOSUDS.sh
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,20 @@
    #!/usr/bin/bash


    # =======================================
    # eOS-Universal-Dark-Style
    # =======================================

    # elementaryOS 6 has great dark themes, but they only apply to "curated" appcenter apps
    # This script monitors changes to the system theme and forces it to work with regular Debian packages as well as non-curated Flatpak apps
    # It also optionally changes the wallpaper based on the theme
    # It also supports changing themes for the Mailspring email app (both .deb and Snap versions)


    # =======================================
    # Instructions
    # =======================================

    # 1. Change the configuration variables below as needed.
    # 2. Save this script, make it executable, and set it up to run in the background on startup.
    # This can be done on elementaryOS by adding the command to Applications -> Startup in the system settings.
    @@ -19,6 +23,7 @@
    # =======================================
    # Configuration
    # =======================================

    LIGHT_THEME_WALLPAPER='/usr/share/backgrounds/odin.jpg' # Leave empty to disable wallpaper switching on light theme
    DARK_THEME_WALLPAPER='/usr/share/backgrounds/odin-dark.jpg' # Leave empty to disable wallpaper switching on dark theme
    MAILSPRING_LIGHT_THEME='ui-light' # Leave empty to disable Mailspring theme switching on light theme
    @@ -31,6 +36,7 @@ WALLPAPER_CHECK_INTERVAL=30 # Ensure the current wallpaper is in line with the s
    MAILSPRING_THEME_CHECK_INTERVAL=30 # Ensure the current Mailspring theme is in line with the system dark style based on the variables above
    NON_ELEMENTARY_FLATPAK_THEME_CHECK_INTERVAL=60 # Ensure non-Elementary Flatpak apps use the current system theme - more CPU intensive, better to do less frequently


    # =======================================
    # Script functions
    # =======================================
    @@ -86,6 +92,49 @@ changeGTKUniversalDarkPrefIfNeeded() {
    fi
    }

    # Changes the wallpaper to the one given in the argument (if not already set)
    # If the argument is empty, does nothing (does NOT throw an error)
    changeWallpaperIfNeeded() {
    if [ ! -z "$1" ]; then
    CURRENT_WALLPAPER="$(gsettings get org.gnome.desktop.background picture-uri | head -c -2 | tail -c +9)"
    if [ "$CURRENT_WALLPAPER" != "$1" ]; then
    gsettings set org.gnome.desktop.background picture-uri file://"$1"
    log "Wallpaper has been set to $1"
    else
    log "Wallpaper is already $1, no changes made" 0 0 1
    fi
    else
    log "Wallpaper argument is empty, no changes made" 0 0 1
    fi
    }

    # Changes the Mailspring theme to the provided string if a Mailspring config can be found (indicating it is installed)
    # If not installed or if no theme provided, does nothing (does NOT throw an error)
    # Warning: This abruptly kills the Mailspring process and restarts it in the background
    changeMailSpringThemeIfNeeded() {
    if [ ! -z "$1" ]; then
    MAILSPRING_CONFIG_FILE=~/.config/Mailspring/config.json
    if [ ! -f "$MAILSPRING_CONFIG_FILE" ]; then MAILSPRING_CONFIG_FILE=~/snap/mailspring/common/config.json; fi
    if [ -f "$MAILSPRING_CONFIG_FILE" ] && [ ! -z "$1" ]; then
    CURRENT_MAILSPRING_THEME="$(cat "$MAILSPRING_CONFIG_FILE" | grep \"theme\": | tr -d ' ' | tail -c +10 | head -c -3)"
    if [ "$CURRENT_MAILSPRING_THEME" != "$1" ]; then
    sed -i "s/\"theme\":.*/\"theme\": \"$1\",/g" "$MAILSPRING_CONFIG_FILE"
    log "Mailspring theme has been set to $1"
    pkill -f mailspring > /dev/null 2>&1
    log "Mailspring process has been killed"
    mailspring -b </dev/null &>/dev/null &
    log "Mailspring process has been started in background"
    else
    log "Mailspring theme is already $1, no changes made" 0 0 1
    fi
    else
    log "Mailspring not installed, no changes made" 0 0 1
    fi
    else
    log "Mailspring theme argument is empty, no changes made" 0 0 1
    fi
    }

    # First, installs the current system theme into the Flatpak runtimes used by all non-curated (non-appcenter) apps (if not already installed)
    # If the current theme is an elementary theme (io.elementary.stylesheet.*) and if the $1 argument is 1, it makes these changes before installing the theme:
    # - Renames it by adding a -dark suffix
    @@ -153,57 +202,16 @@ changeNonElementaryFlatpakThemesIfNeeded() {
    }


    # Changes the wallpaper to the one given in the argument (if not already set)
    # If the argument is empty, does nothing (does NOT throw an error)
    changeWallpaperIfNeeded() {
    if [ ! -z "$1" ]; then
    CURRENT_WALLPAPER="$(gsettings get org.gnome.desktop.background picture-uri | head -c -2 | tail -c +9)"
    if [ "$CURRENT_WALLPAPER" != "$1" ]; then
    gsettings set org.gnome.desktop.background picture-uri file://"$1"
    log "Wallpaper has been set to $1"
    else
    log "Wallpaper is already $1, no changes made" 0 0 1
    fi
    else
    log "Wallpaper argument is empty, no changes made" 0 0 1
    fi
    }


    # Changes the Mailspring theme to the provided string if a Mailspring config can be found (indicating it is installed)
    # If not installed or if no theme provided, does nothing; does NOT throw an error
    # Warning: This abruptly kills the Mailspring process and restarts it in the background
    changeMailSpringThemeIfNeeded() {
    if [ ! -z "$1" ]; then
    MAILSPRING_CONFIG_FILE=~/.config/Mailspring/config.json
    if [ ! -f "$MAILSPRING_CONFIG_FILE" ]; then MAILSPRING_CONFIG_FILE=~/snap/mailspring/common/config.json; fi
    if [ -f "$MAILSPRING_CONFIG_FILE" ] && [ ! -z "$1" ]; then
    CURRENT_MAILSPRING_THEME="$(cat "$MAILSPRING_CONFIG_FILE" | grep \"theme\": | tr -d ' ' | tail -c +10 | head -c -3)"
    if [ "$CURRENT_MAILSPRING_THEME" != "$1" ]; then
    sed -i "s/\"theme\":.*/\"theme\": \"$1\",/g" "$MAILSPRING_CONFIG_FILE"
    log "Mailspring theme has been set to $1"
    pkill -f mailspring > /dev/null 2>&1
    log "Mailspring process has been killed"
    mailspring -b </dev/null &>/dev/null &
    log "Mailspring process has been started in background"
    else
    log "Mailspring theme is already $1, no changes made" 0 0 1
    fi
    else
    log "Mailspring not installed, no changes made" 0 0 1
    fi
    else
    log "Mailspring theme argument is empty, no changes made" 0 0 1
    fi
    }

    # =======================================
    # Actual execution
    # =======================================

    # In a continuous loop:
    # 1. Every GTK_UNIVERSAL_DARK_PREF_CHECK_INTERVAL seconds, changes the universal GTK dark theme preference if needed
    # 2. Every WALLPAPER_CHECK_INTERVAL seconds, changes the wallpaper if needed
    # 3. Every MAILSPRING_THEME_CHECK_INTERVAL seconds, changes the Mailspring theme if needed
    # 4. Every NON_ELEMENTARY_FLATPAK_THEME_CHECK_INTERVAL seconds, changes the theme of all non-elementary Flatpak apps if needed

    log "Loop started" 0 0 1
    GTK_UNIVERSAL_DARK_PREF_CHECK_TIMER="$GTK_UNIVERSAL_DARK_PREF_CHECK_INTERVAL"
    WALLPAPER_CHECK_TIMER="$WALLPAPER_CHECK_INTERVAL"
  6. ImranR98 revised this gist Aug 24, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions eOSUDS.sh
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    #!/usr/bin/bash

    # =======================================
    # eOS-Universal-Dark-Style
    # =======================================
  7. ImranR98 revised this gist Aug 24, 2021. 1 changed file with 92 additions and 121 deletions.
    213 changes: 92 additions & 121 deletions eOSUDS.sh
    Original file line number Diff line number Diff line change
    @@ -22,13 +22,12 @@ DARK_THEME_WALLPAPER='/usr/share/backgrounds/odin-dark.jpg' # Leave empty to dis
    MAILSPRING_LIGHT_THEME='ui-light' # Leave empty to disable Mailspring theme switching on light theme
    MAILSPRING_DARK_THEME='ui-dark' # Leave empty to disable Mailspring theme switching on dark theme
    SHOW_DEBUG_MESSAGES=0 # If debug logs should be printed, set to 1
    CHECK_THEME_CHANGE_INTERVAL=7 # How long, in seconds (whole number), to wait between each check of the system theme
    ENSURE_FLATPAK_THEMES_INSTALLED_INTERVAL=35 # How long, in seconds (whole number), to wait between each check of the themes installed in Flatpak runtimes

    # Why have two separate intervals instead of one? Two reasons:
    # 1. Checking the theme difference is not nearly as CPU intensive as ensuring Flatpak themes are installed, so the former can be done more frequently
    # 2. Flatpak runtime themes need to be re-installed anytime the runtime is updated, not just when there is a theme change, so that must be checked independently

    # Timing variables - how often to check whether a specific change needs to be made (and make it) - all in seconds, whole numbers only
    GTK_UNIVERSAL_DARK_PREF_CHECK_INTERVAL=30 # Ensure the GTK universal dark theme is in line with the system dark style
    WALLPAPER_CHECK_INTERVAL=30 # Ensure the current wallpaper is in line with the system dark style based on the variables above
    MAILSPRING_THEME_CHECK_INTERVAL=30 # Ensure the current Mailspring theme is in line with the system dark style based on the variables above
    NON_ELEMENTARY_FLATPAK_THEME_CHECK_INTERVAL=60 # Ensure non-Elementary Flatpak apps use the current system theme - more CPU intensive, better to do less frequently

    # =======================================
    # Script functions
    @@ -48,21 +47,24 @@ log() {
    echo -e "$TERM_BLUE""DEBUG: $(date): $1""$TERM_RESET"
    fi
    else
    echo -e "-INFO: $(date): $1"
    echo -e "-INFO: $(date): $1""$TERM_RESET"
    fi
    if [ "$3" == 1 ]; then
    echo -e "$TERM_RED""$TERM_BOLD""CRASHED.""$TERM_RESET" >&2
    exit 1
    fi

    }

    # Echoes 1 if the system dark style is set, 0 otherwise
    getSystemDarkStylePref() {
    [ "$(gsettings get org.freedesktop prefers-color-scheme)" = "'dark'" ] && echo 1 || echo 0
    }

    getGTKUniversalDarkPref() {
    # Changes the universal GTK dark theme preference based on the provided argument (if they are different)
    changeGTKUniversalDarkPrefIfNeeded() {
    # Ensure the provided argument is valid before making the change
    [ "$1" == 0 ] || [ "$1" == 1 ] || log "GTK universal dark preference can only be set to 0 or 1" 1 1
    # The location of the universal GTK dark preference config file
    GTK_UNIVERSAL_PREF_FILE=~/.config/gtk-3.0/settings.ini
    # Ensure the file exists (if not, create it)
    @@ -71,41 +73,23 @@ getGTKUniversalDarkPref() {
    fi
    # Grab the pref
    GTK_UNIVERSAL_PREF_IS_DARK="$(cat "$GTK_UNIVERSAL_PREF_FILE" | grep -o [0-1])"
    # Ensure the latter was 0 or 1; if so, echo it and if not, log the error and exit
    # Ensure it is 0 or 1; if not, log the error and exit
    [ "$GTK_UNIVERSAL_PREF_IS_DARK" == 0 ] || [ "$GTK_UNIVERSAL_PREF_IS_DARK" == 1 ] || log "$GTK_UNIVERSAL_PREF_FILE is not valid" 1 1
    echo "$GTK_UNIVERSAL_PREF_IS_DARK"
    }

    # Changes the universal GTK dark theme preference based on the provided argument
    changeGTKUniversalDarkPref() {
    # The location of the universal GTK dark preference config file
    GTK_UNIVERSAL_PREF_FILE=~/.config/gtk-3.0/settings.ini
    # Ensure the provided argument is valid before making the change
    [ "$1" == 0 ] || [ "$1" == 1 ] || log "GTK universal dark preference must be 0 nor 1" 1 1
    echo "$(cat "$GTK_UNIVERSAL_PREF_FILE" | head -c -2)""$1" > "$GTK_UNIVERSAL_PREF_FILE"
    log "GTK universal dark preference has been set to $1"
    }

    # Compares the elementary dark style preference with the GTK universal dark theme preference to see if they differ (indicating changes need to be made)
    # Echoes 2 if they match; if they don't, echoes 1 to indicate the elementary pref is dark (and GTK pref is light), or echoes 0 to to indicate the opposite
    getThemeDifference() {
    # Grab the system dark style preference and the GTK universal dark theme preference
    ELEMENTARY_STYLE_IS_DARK="$(getSystemDarkStylePref)"
    GTK_UNIVERSAL_PREF_IS_DARK="$(getGTKUniversalDarkPref)"
    if [ "$ELEMENTARY_STYLE_IS_DARK" -eq "$GTK_UNIVERSAL_PREF_IS_DARK" ]; then echo 2
    # If needed, make the change
    if [ "$GTK_UNIVERSAL_PREF_IS_DARK" != "$1" ]; then
    echo "$(cat "$GTK_UNIVERSAL_PREF_FILE" | head -c -2)""$1" > "$GTK_UNIVERSAL_PREF_FILE"
    log "GTK universal dark preference has been set to $1"
    else
    [ "$ELEMENTARY_STYLE_IS_DARK" -eq 1 ] && echo 1 || echo 0
    log "GTK universal dark preference is already $1, no changes made" 0 0 1
    fi
    }

    # Installs the current system theme into the Flatpak runtimes used by all non-curated (non-appcenter) apps (if not already installed)
    # First, installs the current system theme into the Flatpak runtimes used by all non-curated (non-appcenter) apps (if not already installed)
    # If the current theme is an elementary theme (io.elementary.stylesheet.*) and if the $1 argument is 1, it makes these changes before installing the theme:
    # - Renames it by adding a -dark suffix
    # - Modifies it so the default stylesheet is dark themed; this is needed as elementary themes do not have traditional separate dark variants
    # If $2 exists, it is treated as a variable name, and if at least one theme was installed into a runtime, the variable named in $2 (if any) is set to 1
    ensureNonElementaryFlatpakRuntimesHaveCurrentThemeInstalled() {
    # Set the variable named in $2 to 0, if it was provided
    if [ ! -z "$2" ]; then eval ""$2"=0"; fi
    # Then, forces all the apps to use the current theme by adding an environment variable override (if not already set)
    changeNonElementaryFlatpakThemesIfNeeded() {
    # The location of Flatpak runtimes
    FLATPAK_RUNTIME_DIR=~/.local/share/flatpak/runtime
    # Where themes are stored in a Flatpak runtime
    @@ -151,107 +135,94 @@ ensureNonElementaryFlatpakRuntimesHaveCurrentThemeInstalled() {
    rm -r "$TEMP_DIR"
    log "$THEME theme has been modified to $THEME-dark and installed into $RUNTIME Flatpak runtime"
    fi
    # Set the variable named in $2 to 1, if it was provided
    if [ ! -z "$2" ]; then eval ""$2"=1"; fi
    else
    log "$THEME theme is already installed in the $RUNTIME Flatpak runtime, no changes made" 0 0 1
    fi
    done
    }


    # Forces all non-curated (non-appcenter) Flatpak apps, to use the current theme by adding an environment variable override (assumes the theme is already installed in the appropriate Flatpak runtimes)
    # If the current theme is an elementary theme (io.elementary.stylesheet.*) and if the $1 argument is 1, it adds a '-dark' suffix to the theme name
    changeNonElementaryFlatpakThemesToCurrentTheme() {
    # Grab the current system theme
    THEME="$(gsettings get org.gnome.desktop.interface gtk-theme | head -c -2 | tail -c +2)"
    # Set the variable that indicates whether to add the '-dark' prefix to the theme based on the $1 argument and whether the current theme is an elementary one
    USE_CUSTOM_ELEMENTARY_DARK="$([ "$1" == 1 ] && [ ! -z "$(echo "$THEME" | grep -e "^io\.elementary\.stylesheet\..*")" ] && echo 1 || echo 0)"
    # Grab the list of app IDs for all non-appcenter installed Flatpaks
    TARGET_APP_IDS=($(flatpak list --columns=application,origin,runtime | awk -F "\t" '{if ($2 != "appcenter" && $3 != "") print $1}'))
    # For each target app ID...
    for APP_ID in "${TARGET_APP_IDS[@]}"; do
    # Do the override
    # Now that the theme is installed, do the override if needed
    CURRENT_APP_THEME="$(flatpak override --user --show "$APP_ID" | grep GTK_THEME= | tail -c +11)"
    FINAL_OVERRIDE_THEME_NAME="$([ "$USE_CUSTOM_ELEMENTARY_DARK" == 1 ] && echo "$(echo "$THEME" | head -c -1 | tail -c +1)-dark" || echo "$THEME")"
    flatpak override --user --env=GTK_THEME="$FINAL_OVERRIDE_THEME_NAME" "$APP_ID"
    log "Flatpak environment variable override 'GTK_THEME="$FINAL_OVERRIDE_THEME_NAME"' has been added for "$APP_ID""
    if [ "$CURRENT_APP_THEME" != "$FINAL_OVERRIDE_THEME_NAME" ]; then
    flatpak override --user --env=GTK_THEME="$FINAL_OVERRIDE_THEME_NAME" "$APP_ID"
    log "Flatpak environment variable override 'GTK_THEME=$FINAL_OVERRIDE_THEME_NAME' has been added for $APP_ID"
    else
    log "Flatpak environment variable override 'GTK_THEME=$FINAL_OVERRIDE_THEME_NAME' is already set for $APP_ID, no changes made" 0 0 1
    fi
    done
    }


    # Changes the wallpaper to the one given in the argument
    changeWallpaper() {
    gsettings set org.gnome.desktop.background picture-uri file://"$1"
    log "Wallpaper has been set to $1"
    # Changes the wallpaper to the one given in the argument (if not already set)
    # If the argument is empty, does nothing (does NOT throw an error)
    changeWallpaperIfNeeded() {
    if [ ! -z "$1" ]; then
    CURRENT_WALLPAPER="$(gsettings get org.gnome.desktop.background picture-uri | head -c -2 | tail -c +9)"
    if [ "$CURRENT_WALLPAPER" != "$1" ]; then
    gsettings set org.gnome.desktop.background picture-uri file://"$1"
    log "Wallpaper has been set to $1"
    else
    log "Wallpaper is already $1, no changes made" 0 0 1
    fi
    else
    log "Wallpaper argument is empty, no changes made" 0 0 1
    fi
    }


    # Changes the Mailspring theme to the provided string if a Mailspring config can be found (indicating it is installed)
    # If not installed or if no theme provided, does nothing; does NOT throw an error
    # Warning: This abruptly kills the Mailspring process and restarts it in the background
    changeMailSpringThemeIfPossible() {
    MAILSPRING_CONFIG_FILE=~/.config/Mailspring/config.json
    if [ ! -f "$MAILSPRING_CONFIG_FILE" ]; then MAILSPRING_CONFIG_FILE=~/snap/mailspring/common/config.json; fi
    if [ -f "$MAILSPRING_CONFIG_FILE" ] && [ ! -z "$1" ]; then
    sed -i "s/\"theme\":.*/\"theme\": \"$1\",/g" "$MAILSPRING_CONFIG_FILE"
    log "Mailspring theme has been set to $1"
    pkill -f mailspring > /dev/null 2>&1
    log "Mailspring process has been killed"
    mailspring -b </dev/null &>/dev/null &
    log "Mailspring process has been started in background"
    fi
    }

    # Brings together all above functions. Runs the following in a loop:
    # 1. Every CHECK_THEME_CHANGE_INTERVAL seconds, checks to see if there is a difference between the system dark style and the GTK universal dark theme preference
    # 2. Every ENSURE_FLATPAK_THEMES_INSTALLED_INTERVAL seconds, checks to see if the current theme is missing from any Flatpak runtime where it's needed (and installs it if so)
    # 3. Based on the result of step 1:
    # - Changes the GTK universal dark style preference
    # - Changes the wallpaper if possible
    # - Changes the Mailspring theme if possible
    # 4. Based on either the result of step 1 or 2:
    # - Changes the theme override environment variable for all non-curated (non-elementary) Flatpak apps
    runMainLoop() {
    FIST_RUN=1
    log "Loop started" 0 0 1
    THEME_CHANGE_TIMER="$CHECK_THEME_CHANGE_INTERVAL"
    FLATPAK_THEME_INSTALL_TIMER="$ENSURE_FLATPAK_THEMES_INSTALLED_INTERVAL"
    while [ true ]; do
    if [ "$THEME_CHANGE_TIMER" == "$CHECK_THEME_CHANGE_INTERVAL" ]; then
    THEME_DIFF="$(getThemeDifference)"
    log "THEME_DIFF is found to be $THEME_DIFF" 0 0 1
    if [ "$THEME_DIFF" != 2 ]; then
    changeGTKUniversalDarkPref "$THEME_DIFF"
    ensureNonElementaryFlatpakRuntimesHaveCurrentThemeInstalled "$THEME_DIFF"
    if [ "$THEME_DIFF" -eq 1 ]; then
    if [ ! -z "$DARK_THEME_WALLPAPER" ]; then changeWallpaper "$DARK_THEME_WALLPAPER"; fi
    if [ ! -z "$MAILSPRING_DARK_THEME" ]; then changeMailSpringThemeIfPossible "$MAILSPRING_DARK_THEME"; fi
    else
    if [ ! -z "$LIGHT_THEME_WALLPAPER" ]; then changeWallpaper "$LIGHT_THEME_WALLPAPER"; fi
    if [ ! -z "$MAILSPRING_LIGHT_THEME" ]; then changeMailSpringThemeIfPossible "$MAILSPRING_LIGHT_THEME"; fi
    fi
    changeNonElementaryFlatpakThemesToCurrentTheme "$THEME_DIFF"
    fi
    THEME_CHANGE_TIMER=0
    fi
    if [ "$FLATPAK_THEME_INSTALL_TIMER" == "$ENSURE_FLATPAK_THEMES_INSTALLED_INTERVAL" ] || [ "$FIST_RUN" == 1 ]; then
    ELEMENTARY_STYLE_IS_DARK="$(getSystemDarkStylePref)"
    ensureNonElementaryFlatpakRuntimesHaveCurrentThemeInstalled "$ELEMENTARY_STYLE_IS_DARK" 'MISSING_THEME_INSTALLED_INTO_FLATPAK_RUNTIME'
    log "MISSING_THEME_INSTALLED_INTO_FLATPAK_RUNTIME is found to be $MISSING_THEME_INSTALLED_INTO_FLATPAK_RUNTIME" 0 0 1
    if [ "$MISSING_THEME_INSTALLED_INTO_FLATPAK_RUNTIME" == 1 ]; then
    changeNonElementaryFlatpakThemesToCurrentTheme "$ELEMENTARY_STYLE_IS_DARK"
    MISSING_THEME_INSTALLED_INTO_FLATPAK_RUNTIME=0
    THEME_DIFF=2
    changeMailSpringThemeIfNeeded() {
    if [ ! -z "$1" ]; then
    MAILSPRING_CONFIG_FILE=~/.config/Mailspring/config.json
    if [ ! -f "$MAILSPRING_CONFIG_FILE" ]; then MAILSPRING_CONFIG_FILE=~/snap/mailspring/common/config.json; fi
    if [ -f "$MAILSPRING_CONFIG_FILE" ] && [ ! -z "$1" ]; then
    CURRENT_MAILSPRING_THEME="$(cat "$MAILSPRING_CONFIG_FILE" | grep \"theme\": | tr -d ' ' | tail -c +10 | head -c -3)"
    if [ "$CURRENT_MAILSPRING_THEME" != "$1" ]; then
    sed -i "s/\"theme\":.*/\"theme\": \"$1\",/g" "$MAILSPRING_CONFIG_FILE"
    log "Mailspring theme has been set to $1"
    pkill -f mailspring > /dev/null 2>&1
    log "Mailspring process has been killed"
    mailspring -b </dev/null &>/dev/null &
    log "Mailspring process has been started in background"
    else
    log "Mailspring theme is already $1, no changes made" 0 0 1
    fi
    FLATPAK_THEME_INSTALL_TIMER=0
    else
    log "Mailspring not installed, no changes made" 0 0 1
    fi
    THEME_CHANGE_TIMER="$(expr "$THEME_CHANGE_TIMER" + 1)"
    FLATPAK_THEME_INSTALL_TIMER="$(expr "$FLATPAK_THEME_INSTALL_TIMER" + 1)"

    FIST_RUN=0
    sleep 1
    done
    else
    log "Mailspring theme argument is empty, no changes made" 0 0 1
    fi
    }

    # =======================================
    # Actual execution
    # =======================================
    runMainLoop
    # 1. Every GTK_UNIVERSAL_DARK_PREF_CHECK_INTERVAL seconds, changes the universal GTK dark theme preference if needed
    # 2. Every WALLPAPER_CHECK_INTERVAL seconds, changes the wallpaper if needed
    # 3. Every MAILSPRING_THEME_CHECK_INTERVAL seconds, changes the Mailspring theme if needed
    # 4. Every NON_ELEMENTARY_FLATPAK_THEME_CHECK_INTERVAL seconds, changes the theme of all non-elementary Flatpak apps if needed
    log "Loop started" 0 0 1
    GTK_UNIVERSAL_DARK_PREF_CHECK_TIMER="$GTK_UNIVERSAL_DARK_PREF_CHECK_INTERVAL"
    WALLPAPER_CHECK_TIMER="$WALLPAPER_CHECK_INTERVAL"
    MAILSPRING_THEME_CHECK_TIMER="$MAILSPRING_THEME_CHECK_INTERVAL"
    NON_ELEMENTARY_FLATPAK_THEME_CHECK_TIMER="$NON_ELEMENTARY_FLATPAK_THEME_CHECK_INTERVAL"
    while [ true ]; do
    if [ "$GTK_UNIVERSAL_DARK_PREF_CHECK_TIMER" == "$GTK_UNIVERSAL_DARK_PREF_CHECK_INTERVAL" ]; then
    changeGTKUniversalDarkPrefIfNeeded "$(getSystemDarkStylePref)"
    GTK_UNIVERSAL_DARK_PREF_CHECK_TIMER=0
    fi
    if [ "$WALLPAPER_CHECK_TIMER" == "$WALLPAPER_CHECK_INTERVAL" ]; then
    changeWallpaperIfNeeded "$([ "$(getSystemDarkStylePref)" == 1 ] && echo "$DARK_THEME_WALLPAPER" || echo "$LIGHT_THEME_WALLPAPER")"
    WALLPAPER_CHECK_TIMER=0
    fi
    if [ "$MAILSPRING_THEME_CHECK_TIMER" == "$MAILSPRING_THEME_CHECK_INTERVAL" ]; then
    changeMailSpringThemeIfNeeded "$([ "$(getSystemDarkStylePref)" == 1 ] && echo "$MAILSPRING_DARK_THEME" || echo "$MAILSPRING_LIGHT_THEME")"
    MAILSPRING_THEME_CHECK_TIMER=0
    fi
    if [ "$NON_ELEMENTARY_FLATPAK_THEME_CHECK_TIMER" == "$NON_ELEMENTARY_FLATPAK_THEME_CHECK_INTERVAL" ]; then
    changeNonElementaryFlatpakThemesIfNeeded "$(getSystemDarkStylePref)"
    NON_ELEMENTARY_FLATPAK_THEME_CHECK_TIMER=0
    fi
    sleep 1
    done
  8. ImranR98 revised this gist Aug 23, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion eOSUDS.sh
    Original file line number Diff line number Diff line change
    @@ -48,7 +48,7 @@ log() {
    echo -e "$TERM_BLUE""DEBUG: $(date): $1""$TERM_RESET"
    fi
    else
    echo -e "-INFO: $(date): $1""$TERM_RESET"
    echo -e "-INFO: $(date): $1"
    fi
    if [ "$3" == 1 ]; then
    echo -e "$TERM_RED""$TERM_BOLD""CRASHED.""$TERM_RESET" >&2
  9. ImranR98 revised this gist Aug 22, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions eOSUDS.sh
    Original file line number Diff line number Diff line change
    @@ -215,7 +215,7 @@ runMainLoop() {
    THEME_CHANGE_TIMER="$CHECK_THEME_CHANGE_INTERVAL"
    FLATPAK_THEME_INSTALL_TIMER="$ENSURE_FLATPAK_THEMES_INSTALLED_INTERVAL"
    while [ true ]; do
    if [ "$THEME_CHANGE_TIMER" == "$CHECK_THEME_CHANGE_INTERVAL" ] || [ "$FIST_RUN" == 1 ]; then
    if [ "$THEME_CHANGE_TIMER" == "$CHECK_THEME_CHANGE_INTERVAL" ]; then
    THEME_DIFF="$(getThemeDifference)"
    log "THEME_DIFF is found to be $THEME_DIFF" 0 0 1
    if [ "$THEME_DIFF" != 2 ]; then
    @@ -232,7 +232,7 @@ runMainLoop() {
    fi
    THEME_CHANGE_TIMER=0
    fi
    if [ "$FLATPAK_THEME_INSTALL_TIMER" == "$ENSURE_FLATPAK_THEMES_INSTALLED_INTERVAL" ] && [ "$FIST_RUN" != 1 ]; then
    if [ "$FLATPAK_THEME_INSTALL_TIMER" == "$ENSURE_FLATPAK_THEMES_INSTALLED_INTERVAL" ] || [ "$FIST_RUN" == 1 ]; then
    ELEMENTARY_STYLE_IS_DARK="$(getSystemDarkStylePref)"
    ensureNonElementaryFlatpakRuntimesHaveCurrentThemeInstalled "$ELEMENTARY_STYLE_IS_DARK" 'MISSING_THEME_INSTALLED_INTO_FLATPAK_RUNTIME'
    log "MISSING_THEME_INSTALLED_INTO_FLATPAK_RUNTIME is found to be $MISSING_THEME_INSTALLED_INTO_FLATPAK_RUNTIME" 0 0 1
  10. ImranR98 revised this gist Aug 22, 2021. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions eOSUDS.sh
    Original file line number Diff line number Diff line change
    @@ -210,11 +210,12 @@ changeMailSpringThemeIfPossible() {
    # 4. Based on either the result of step 1 or 2:
    # - Changes the theme override environment variable for all non-curated (non-elementary) Flatpak apps
    runMainLoop() {
    FIST_RUN=1
    log "Loop started" 0 0 1
    THEME_CHANGE_TIMER="$CHECK_THEME_CHANGE_INTERVAL"
    FLATPAK_THEME_INSTALL_TIMER="$ENSURE_FLATPAK_THEMES_INSTALLED_INTERVAL"
    while [ true ]; do
    if [ "$THEME_CHANGE_TIMER" == "$CHECK_THEME_CHANGE_INTERVAL" ]; then
    if [ "$THEME_CHANGE_TIMER" == "$CHECK_THEME_CHANGE_INTERVAL" ] || [ "$FIST_RUN" == 1 ]; then
    THEME_DIFF="$(getThemeDifference)"
    log "THEME_DIFF is found to be $THEME_DIFF" 0 0 1
    if [ "$THEME_DIFF" != 2 ]; then
    @@ -231,7 +232,7 @@ runMainLoop() {
    fi
    THEME_CHANGE_TIMER=0
    fi
    if [ "$FLATPAK_THEME_INSTALL_TIMER" == "$ENSURE_FLATPAK_THEMES_INSTALLED_INTERVAL" ]; then
    if [ "$FLATPAK_THEME_INSTALL_TIMER" == "$ENSURE_FLATPAK_THEMES_INSTALLED_INTERVAL" ] && [ "$FIST_RUN" != 1 ]; then
    ELEMENTARY_STYLE_IS_DARK="$(getSystemDarkStylePref)"
    ensureNonElementaryFlatpakRuntimesHaveCurrentThemeInstalled "$ELEMENTARY_STYLE_IS_DARK" 'MISSING_THEME_INSTALLED_INTO_FLATPAK_RUNTIME'
    log "MISSING_THEME_INSTALLED_INTO_FLATPAK_RUNTIME is found to be $MISSING_THEME_INSTALLED_INTO_FLATPAK_RUNTIME" 0 0 1
    @@ -245,6 +246,7 @@ runMainLoop() {
    THEME_CHANGE_TIMER="$(expr "$THEME_CHANGE_TIMER" + 1)"
    FLATPAK_THEME_INSTALL_TIMER="$(expr "$FLATPAK_THEME_INSTALL_TIMER" + 1)"

    FIST_RUN=0
    sleep 1
    done
    }
  11. ImranR98 revised this gist Aug 21, 2021. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions eOSUDS.sh
    Original file line number Diff line number Diff line change
    @@ -21,9 +21,9 @@ LIGHT_THEME_WALLPAPER='/usr/share/backgrounds/odin.jpg' # Leave empty to disable
    DARK_THEME_WALLPAPER='/usr/share/backgrounds/odin-dark.jpg' # Leave empty to disable wallpaper switching on dark theme
    MAILSPRING_LIGHT_THEME='ui-light' # Leave empty to disable Mailspring theme switching on light theme
    MAILSPRING_DARK_THEME='ui-dark' # Leave empty to disable Mailspring theme switching on dark theme
    SHOW_DEBUG_MESSAGES=1 # If debug logs should be printed, set to 1
    CHECK_THEME_CHANGE_INTERVAL=1 # How long, in seconds (whole number), to wait between each check of the system theme
    ENSURE_FLATPAK_THEMES_INSTALLED_INTERVAL=1 # How long, in seconds (whole number), to wait between each check of the themes installed in Flatpak runtimes
    SHOW_DEBUG_MESSAGES=0 # If debug logs should be printed, set to 1
    CHECK_THEME_CHANGE_INTERVAL=7 # How long, in seconds (whole number), to wait between each check of the system theme
    ENSURE_FLATPAK_THEMES_INSTALLED_INTERVAL=35 # How long, in seconds (whole number), to wait between each check of the themes installed in Flatpak runtimes

    # Why have two separate intervals instead of one? Two reasons:
    # 1. Checking the theme difference is not nearly as CPU intensive as ensuring Flatpak themes are installed, so the former can be done more frequently
  12. ImranR98 revised this gist Aug 21, 2021. 1 changed file with 27 additions and 19 deletions.
    46 changes: 27 additions & 19 deletions eOSUDS.sh
    Original file line number Diff line number Diff line change
    @@ -21,9 +21,9 @@ LIGHT_THEME_WALLPAPER='/usr/share/backgrounds/odin.jpg' # Leave empty to disable
    DARK_THEME_WALLPAPER='/usr/share/backgrounds/odin-dark.jpg' # Leave empty to disable wallpaper switching on dark theme
    MAILSPRING_LIGHT_THEME='ui-light' # Leave empty to disable Mailspring theme switching on light theme
    MAILSPRING_DARK_THEME='ui-dark' # Leave empty to disable Mailspring theme switching on dark theme
    SHOW_DEBUG_MESSAGES=0 # If debug logs should be printed, set to 1
    CHECK_THEME_CHANGE_INTERVAL=7 # How long, in seconds (whole number), to wait between each check of the system theme
    ENSURE_FLATPAK_THEMES_INSTALLED_INTERVAL=35 # How long, in seconds (whole number), to wait between each check of the themes installed in Flatpak runtimes
    SHOW_DEBUG_MESSAGES=1 # If debug logs should be printed, set to 1
    CHECK_THEME_CHANGE_INTERVAL=1 # How long, in seconds (whole number), to wait between each check of the system theme
    ENSURE_FLATPAK_THEMES_INSTALLED_INTERVAL=1 # How long, in seconds (whole number), to wait between each check of the themes installed in Flatpak runtimes

    # Why have two separate intervals instead of one? Two reasons:
    # 1. Checking the theme difference is not nearly as CPU intensive as ensuring Flatpak themes are installed, so the former can be done more frequently
    @@ -62,39 +62,50 @@ getSystemDarkStylePref() {
    [ "$(gsettings get org.freedesktop prefers-color-scheme)" = "'dark'" ] && echo 1 || echo 0
    }

    # Compares the elementary dark style preference with the GTK universal dark theme preference to see if they differ (indicating changes need to be made)
    # Echoes 2 if they match; if they don't, echoes 1 to indicate the elementary pref is dark (and GTK pref is light), or echoes 0 to to indicate the opposite
    getThemeDifference() {
    getGTKUniversalDarkPref() {
    # The location of the universal GTK dark preference config file
    GTK_UNIVERSAL_PREF_FILE=~/.config/gtk-3.0/settings.ini
    # Grab the system dark style preference and the GTK universal dark theme preference
    ELEMENTARY_STYLE_IS_DARK="$(getSystemDarkStylePref)"
    GTK_UNIVERSAL_PREF_IS_DARK="$(cat "$GTK_UNIVERSAL_PREF_FILE" | grep -o [0-1])"
    # Ensure the latter was 0 or 1; if not, log the error and exit
    [ "$GTK_UNIVERSAL_PREF_IS_DARK" == 0 ] || [ "$GTK_UNIVERSAL_PREF_IS_DARK" == 1 ] || log "$GTK_UNIVERSAL_PREF_FILE is missing or not valid" 1 1
    if [ "$ELEMENTARY_STYLE_IS_DARK" -eq "$GTK_UNIVERSAL_PREF_IS_DARK" ]; then echo 2
    else
    [ "$ELEMENTARY_STYLE_IS_DARK" -eq 1 ] && echo 1 || echo 0
    # Ensure the file exists (if not, create it)
    if [ ! -f "$GTK_UNIVERSAL_PREF_FILE" ]; then
    echo -e '[Settings]\ngtk-application-prefer-dark-theme=1\n' > "$GTK_UNIVERSAL_PREF_FILE"
    fi
    # Grab the pref
    GTK_UNIVERSAL_PREF_IS_DARK="$(cat "$GTK_UNIVERSAL_PREF_FILE" | grep -o [0-1])"
    # Ensure the latter was 0 or 1; if so, echo it and if not, log the error and exit
    [ "$GTK_UNIVERSAL_PREF_IS_DARK" == 0 ] || [ "$GTK_UNIVERSAL_PREF_IS_DARK" == 1 ] || log "$GTK_UNIVERSAL_PREF_FILE is not valid" 1 1
    echo "$GTK_UNIVERSAL_PREF_IS_DARK"
    }


    # Changes the universal GTK dark theme preference based on the provided argument
    changeGTKUniversalDarkPref() {
    # The location of the universal GTK dark preference config file
    GTK_UNIVERSAL_PREF_FILE=~/.config/gtk-3.0/settings.ini
    # Ensure the provided argument is valid before making the change
    [ "$1" == 0 ] || [ "$1" == 1 ] || log "GTK universal dark preference is neither 0 nor 1" 1 1
    [ "$1" == 0 ] || [ "$1" == 1 ] || log "GTK universal dark preference must be 0 nor 1" 1 1
    echo "$(cat "$GTK_UNIVERSAL_PREF_FILE" | head -c -2)""$1" > "$GTK_UNIVERSAL_PREF_FILE"
    log "GTK universal dark preference has been set to $1"
    }

    # Compares the elementary dark style preference with the GTK universal dark theme preference to see if they differ (indicating changes need to be made)
    # Echoes 2 if they match; if they don't, echoes 1 to indicate the elementary pref is dark (and GTK pref is light), or echoes 0 to to indicate the opposite
    getThemeDifference() {
    # Grab the system dark style preference and the GTK universal dark theme preference
    ELEMENTARY_STYLE_IS_DARK="$(getSystemDarkStylePref)"
    GTK_UNIVERSAL_PREF_IS_DARK="$(getGTKUniversalDarkPref)"
    if [ "$ELEMENTARY_STYLE_IS_DARK" -eq "$GTK_UNIVERSAL_PREF_IS_DARK" ]; then echo 2
    else
    [ "$ELEMENTARY_STYLE_IS_DARK" -eq 1 ] && echo 1 || echo 0
    fi
    }

    # Installs the current system theme into the Flatpak runtimes used by all non-curated (non-appcenter) apps (if not already installed)
    # If the current theme is an elementary theme (io.elementary.stylesheet.*) and if the $1 argument is 1, it makes these changes before installing the theme:
    # - Renames it by adding a -dark suffix
    # - Modifies it so the default stylesheet is dark themed; this is needed as elementary themes do not have traditional separate dark variants
    # If $2 exists, it is treated as a variable name, and if at least one theme was installed into a runtime, the variable named in $2 (if any) is set to 1
    ensureNonElementaryFlatpakRuntimesHaveCurrentThemeInstalled() {
    # Set the variable named in $2 to 0, if it was provided
    if [ ! -z "$2" ]; then eval ""$2"=0"; fi
    # The location of Flatpak runtimes
    FLATPAK_RUNTIME_DIR=~/.local/share/flatpak/runtime
    # Where themes are stored in a Flatpak runtime
    @@ -142,9 +153,6 @@ ensureNonElementaryFlatpakRuntimesHaveCurrentThemeInstalled() {
    fi
    # Set the variable named in $2 to 1, if it was provided
    if [ ! -z "$2" ]; then eval ""$2"=1"; fi
    else
    # Set the variable named in $2 to 0, if it was provided
    if [ ! -z "$2" ]; then eval ""$2"=0"; fi
    fi
    done
    }
  13. ImranR98 revised this gist Aug 20, 2021. 1 changed file with 91 additions and 40 deletions.
    131 changes: 91 additions & 40 deletions eOSUDS.sh
    Original file line number Diff line number Diff line change
    @@ -21,8 +21,13 @@ LIGHT_THEME_WALLPAPER='/usr/share/backgrounds/odin.jpg' # Leave empty to disable
    DARK_THEME_WALLPAPER='/usr/share/backgrounds/odin-dark.jpg' # Leave empty to disable wallpaper switching on dark theme
    MAILSPRING_LIGHT_THEME='ui-light' # Leave empty to disable Mailspring theme switching on light theme
    MAILSPRING_DARK_THEME='ui-dark' # Leave empty to disable Mailspring theme switching on dark theme
    SLEEP_INTERVAL_SECONDS=1 # How long, in seconds, to wait between each check of the system theme
    SHOW_DEBUG_MESSAGES=0 # If debug logs should be printed
    SHOW_DEBUG_MESSAGES=0 # If debug logs should be printed, set to 1
    CHECK_THEME_CHANGE_INTERVAL=7 # How long, in seconds (whole number), to wait between each check of the system theme
    ENSURE_FLATPAK_THEMES_INSTALLED_INTERVAL=35 # How long, in seconds (whole number), to wait between each check of the themes installed in Flatpak runtimes

    # Why have two separate intervals instead of one? Two reasons:
    # 1. Checking the theme difference is not nearly as CPU intensive as ensuring Flatpak themes are installed, so the former can be done more frequently
    # 2. Flatpak runtime themes need to be re-installed anytime the runtime is updated, not just when there is a theme change, so that must be checked independently


    # =======================================
    @@ -52,13 +57,18 @@ log() {

    }

    # Echoes 1 if the system dark style is set, 0 otherwise
    getSystemDarkStylePref() {
    [ "$(gsettings get org.freedesktop prefers-color-scheme)" = "'dark'" ] && echo 1 || echo 0
    }

    # Compares the elementary dark style preference with the GTK universal dark theme preference to see if they differ (indicating changes need to be made)
    # Echoes 2 if they match; if they don't, echoes 1 to indicate the elementary pref is dark (and GTK pref is light), or echoes 0 to to indicate the opposite
    getThemeDifference() {
    # The location of the universal GTK dark preference config file
    GTK_UNIVERSAL_PREF_FILE=~/.config/gtk-3.0/settings.ini
    # Grab the system dark style preference and the GTK universal dark theme preference
    ELEMENTARY_STYLE_IS_DARK="$([ "$(gsettings get org.freedesktop prefers-color-scheme)" = "'dark'" ] && echo 1 || echo 0)"
    ELEMENTARY_STYLE_IS_DARK="$(getSystemDarkStylePref)"
    GTK_UNIVERSAL_PREF_IS_DARK="$(cat "$GTK_UNIVERSAL_PREF_FILE" | grep -o [0-1])"
    # Ensure the latter was 0 or 1; if not, log the error and exit
    [ "$GTK_UNIVERSAL_PREF_IS_DARK" == 0 ] || [ "$GTK_UNIVERSAL_PREF_IS_DARK" == 1 ] || log "$GTK_UNIVERSAL_PREF_FILE is missing or not valid" 1 1
    @@ -74,17 +84,17 @@ changeGTKUniversalDarkPref() {
    # The location of the universal GTK dark preference config file
    GTK_UNIVERSAL_PREF_FILE=~/.config/gtk-3.0/settings.ini
    # Ensure the provided argument is valid before making the change
    [ "$1" == 0 ] || [ "$1" == 1 ] || log "GTK universal dark preference must be 0 or 1" 1 1
    [ "$1" == 0 ] || [ "$1" == 1 ] || log "GTK universal dark preference is neither 0 nor 1" 1 1
    echo "$(cat "$GTK_UNIVERSAL_PREF_FILE" | head -c -2)""$1" > "$GTK_UNIVERSAL_PREF_FILE"
    log "The GTK universal dark preference set to $1"
    log "GTK universal dark preference has been set to $1"
    }


    # Installs the current theme into the Flatpak runtimes used by all non-curated (non-appcenter) apps, then forces the apps to use it by adding an environment variable override
    # Installs the current system theme into the Flatpak runtimes used by all non-curated (non-appcenter) apps (if not already installed)
    # If the current theme is an elementary theme (io.elementary.stylesheet.*) and if the $1 argument is 1, it makes these changes before installing the theme:
    # - Renames it by adding a -dark suffix
    # - Modifies it so the default stylesheet is dark themed; this is needed as elementary themes do not have traditional separate dark variants
    changeNonElementaryFlatpakThemesToCurrentTheme() {
    # If $2 exists, it is treated as a variable name, and if at least one theme was installed into a runtime, the variable named in $2 (if any) is set to 1
    ensureNonElementaryFlatpakRuntimesHaveCurrentThemeInstalled() {
    # The location of Flatpak runtimes
    FLATPAK_RUNTIME_DIR=~/.local/share/flatpak/runtime
    # Where themes are stored in a Flatpak runtime
    @@ -113,11 +123,11 @@ changeNonElementaryFlatpakThemesToCurrentTheme() {
    for LOCATION in "${POSSIBLE_THEME_LOCATIONS[@]}"; do
    if [ ! -z "$(ls "$LOCATION" | grep -e ^"$THEME"$)" ]; then TARGET_THEME_LOCATION="$LOCATION"/"$THEME"; fi
    done
    [ ! -z "$TARGET_THEME_LOCATION" ] || log "The location of the currently set theme - $THEME - could not be found" 1 1
    [ ! -z "$TARGET_THEME_LOCATION" ] || log "Location of the currently set theme - $THEME - could not be found" 1 1
    # If there is not need to make the '-dark' modifications, copy the theme into the Flatpak runtime; else, modify it in a temporary directory first, then copy it
    if [ ! "$USE_CUSTOM_ELEMENTARY_DARK" -eq 1 ]; then
    cp -r "$TARGET_THEME_LOCATION" "$RUNTIME_THEME_DIR"
    log "$THEME theme installed into $RUNTIME Flatpak runtime"
    log "$THEME theme has been installed into $RUNTIME Flatpak runtime"
    else
    TEMP_DIR="$(mktemp -d)"
    cp -r "$TARGET_THEME_LOCATION" "$TEMP_DIR"
    @@ -128,21 +138,41 @@ changeNonElementaryFlatpakThemesToCurrentTheme() {
    mv "$TEMP_DIR"/"$THEME"-dark/gtk-3.0/gtk-dark.css "$TEMP_DIR"/"$THEME"-dark/gtk-3.0/gtk.css
    cp -r "$TEMP_DIR"/"$THEME"-dark "$RUNTIME_THEME_DIR"
    rm -r "$TEMP_DIR"
    log "$THEME theme modified to $THEME-dark and installed into $RUNTIME Flatpak runtime"
    log "$THEME theme has been modified to $THEME-dark and installed into $RUNTIME Flatpak runtime"
    fi
    # Set the variable named in $2 to 1, if it was provided
    if [ ! -z "$2" ]; then eval ""$2"=1"; fi
    else
    # Set the variable named in $2 to 0, if it was provided
    if [ ! -z "$2" ]; then eval ""$2"=0"; fi
    fi
    done
    }


    # Forces all non-curated (non-appcenter) Flatpak apps, to use the current theme by adding an environment variable override (assumes the theme is already installed in the appropriate Flatpak runtimes)
    # If the current theme is an elementary theme (io.elementary.stylesheet.*) and if the $1 argument is 1, it adds a '-dark' suffix to the theme name
    changeNonElementaryFlatpakThemesToCurrentTheme() {
    # Grab the current system theme
    THEME="$(gsettings get org.gnome.desktop.interface gtk-theme | head -c -2 | tail -c +2)"
    # Set the variable that indicates whether to add the '-dark' prefix to the theme based on the $1 argument and whether the current theme is an elementary one
    USE_CUSTOM_ELEMENTARY_DARK="$([ "$1" == 1 ] && [ ! -z "$(echo "$THEME" | grep -e "^io\.elementary\.stylesheet\..*")" ] && echo 1 || echo 0)"
    # Grab the list of app IDs for all non-appcenter installed Flatpaks
    TARGET_APP_IDS=($(flatpak list --columns=application,origin,runtime | awk -F "\t" '{if ($2 != "appcenter" && $3 != "") print $1}'))
    # For each target app ID...
    for APP_ID in "${TARGET_APP_IDS[@]}"; do
    # Do the override
    FINAL_OVERRIDE_THEME_NAME="$([ "$USE_CUSTOM_ELEMENTARY_DARK" == 1 ] && echo "$(echo "$THEME" | head -c -1 | tail -c +1)-dark" || echo "$THEME")"
    flatpak override --user --env=GTK_THEME="$FINAL_OVERRIDE_THEME_NAME" "$APP_ID"
    log "Flatpak environment variable override 'GTK_THEME="$FINAL_OVERRIDE_THEME_NAME"' added for "$APP_ID""
    log "Flatpak environment variable override 'GTK_THEME="$FINAL_OVERRIDE_THEME_NAME"' has been added for "$APP_ID""
    done
    }


    # Changes the wallpaper to the one given in the argument
    changeWallpaper() {
    gsettings set org.gnome.desktop.background picture-uri file://"$1"
    log "Wallpaper set to $1"
    log "Wallpaper has been set to $1"
    }


    @@ -154,43 +184,64 @@ changeMailSpringThemeIfPossible() {
    if [ ! -f "$MAILSPRING_CONFIG_FILE" ]; then MAILSPRING_CONFIG_FILE=~/snap/mailspring/common/config.json; fi
    if [ -f "$MAILSPRING_CONFIG_FILE" ] && [ ! -z "$1" ]; then
    sed -i "s/\"theme\":.*/\"theme\": \"$1\",/g" "$MAILSPRING_CONFIG_FILE"
    log "Set Mailspring theme to $1"
    log "Mailspring theme has been set to $1"
    pkill -f mailspring > /dev/null 2>&1
    log "Mailspring process killed"
    log "Mailspring process has been killed"
    mailspring -b </dev/null &>/dev/null &
    log "Mailspring process started in background"
    log "Mailspring process has been started in background"
    fi
    }


    # Brings together all above functions:
    # 1. Checks to see if there is a difference between the system dark style and the GTK universal dark theme preference
    # 2. Based on that:
    # Brings together all above functions. Runs the following in a loop:
    # 1. Every CHECK_THEME_CHANGE_INTERVAL seconds, checks to see if there is a difference between the system dark style and the GTK universal dark theme preference
    # 2. Every ENSURE_FLATPAK_THEMES_INSTALLED_INTERVAL seconds, checks to see if the current theme is missing from any Flatpak runtime where it's needed (and installs it if so)
    # 3. Based on the result of step 1:
    # - Changes the GTK universal dark style preference
    # - Changes the theme override environment variable for all non-curated (non-elementary) Flatpak apps
    # - Changes the wallpaper if possible
    # - Changes the Mailspring theme if possible
    runScript() {
    THEME_DIFF="$(getThemeDifference)"
    log "The theme difference is $THEME_DIFF" 0 0 1
    if [ ! "$THEME_DIFF" -eq 2 ]; then
    changeGTKUniversalDarkPref "$THEME_DIFF"
    if [ "$THEME_DIFF" -eq 1 ]; then
    if [ ! -z "$DARK_THEME_WALLPAPER" ]; then changeWallpaper "$DARK_THEME_WALLPAPER"; fi
    if [ ! -z "$MAILSPRING_DARK_THEME" ]; then changeMailSpringThemeIfPossible "$MAILSPRING_DARK_THEME"; fi
    else
    if [ ! -z "$LIGHT_THEME_WALLPAPER" ]; then changeWallpaper "$LIGHT_THEME_WALLPAPER"; fi
    if [ ! -z "$MAILSPRING_LIGHT_THEME" ]; then changeMailSpringThemeIfPossible "$MAILSPRING_LIGHT_THEME"; fi
    # 4. Based on either the result of step 1 or 2:
    # - Changes the theme override environment variable for all non-curated (non-elementary) Flatpak apps
    runMainLoop() {
    log "Loop started" 0 0 1
    THEME_CHANGE_TIMER="$CHECK_THEME_CHANGE_INTERVAL"
    FLATPAK_THEME_INSTALL_TIMER="$ENSURE_FLATPAK_THEMES_INSTALLED_INTERVAL"
    while [ true ]; do
    if [ "$THEME_CHANGE_TIMER" == "$CHECK_THEME_CHANGE_INTERVAL" ]; then
    THEME_DIFF="$(getThemeDifference)"
    log "THEME_DIFF is found to be $THEME_DIFF" 0 0 1
    if [ "$THEME_DIFF" != 2 ]; then
    changeGTKUniversalDarkPref "$THEME_DIFF"
    ensureNonElementaryFlatpakRuntimesHaveCurrentThemeInstalled "$THEME_DIFF"
    if [ "$THEME_DIFF" -eq 1 ]; then
    if [ ! -z "$DARK_THEME_WALLPAPER" ]; then changeWallpaper "$DARK_THEME_WALLPAPER"; fi
    if [ ! -z "$MAILSPRING_DARK_THEME" ]; then changeMailSpringThemeIfPossible "$MAILSPRING_DARK_THEME"; fi
    else
    if [ ! -z "$LIGHT_THEME_WALLPAPER" ]; then changeWallpaper "$LIGHT_THEME_WALLPAPER"; fi
    if [ ! -z "$MAILSPRING_LIGHT_THEME" ]; then changeMailSpringThemeIfPossible "$MAILSPRING_LIGHT_THEME"; fi
    fi
    changeNonElementaryFlatpakThemesToCurrentTheme "$THEME_DIFF"
    fi
    THEME_CHANGE_TIMER=0
    fi
    changeNonElementaryFlatpakThemesToCurrentTheme "$THEME_DIFF"
    fi
    if [ "$FLATPAK_THEME_INSTALL_TIMER" == "$ENSURE_FLATPAK_THEMES_INSTALLED_INTERVAL" ]; then
    ELEMENTARY_STYLE_IS_DARK="$(getSystemDarkStylePref)"
    ensureNonElementaryFlatpakRuntimesHaveCurrentThemeInstalled "$ELEMENTARY_STYLE_IS_DARK" 'MISSING_THEME_INSTALLED_INTO_FLATPAK_RUNTIME'
    log "MISSING_THEME_INSTALLED_INTO_FLATPAK_RUNTIME is found to be $MISSING_THEME_INSTALLED_INTO_FLATPAK_RUNTIME" 0 0 1
    if [ "$MISSING_THEME_INSTALLED_INTO_FLATPAK_RUNTIME" == 1 ]; then
    changeNonElementaryFlatpakThemesToCurrentTheme "$ELEMENTARY_STYLE_IS_DARK"
    MISSING_THEME_INSTALLED_INTO_FLATPAK_RUNTIME=0
    THEME_DIFF=2
    fi
    FLATPAK_THEME_INSTALL_TIMER=0
    fi
    THEME_CHANGE_TIMER="$(expr "$THEME_CHANGE_TIMER" + 1)"
    FLATPAK_THEME_INSTALL_TIMER="$(expr "$FLATPAK_THEME_INSTALL_TIMER" + 1)"

    sleep 1
    done
    }


    # =======================================
    # Looped execution every X seconds
    # Actual execution
    # =======================================
    while [ true ]; do
    runScript
    sleep "$SLEEP_INTERVAL_SECONDS"
    done
    runMainLoop
  14. ImranR98 revised this gist Aug 19, 2021. 1 changed file with 16 additions and 6 deletions.
    22 changes: 16 additions & 6 deletions eOSUDS.sh
    Original file line number Diff line number Diff line change
    @@ -22,26 +22,36 @@ DARK_THEME_WALLPAPER='/usr/share/backgrounds/odin-dark.jpg' # Leave empty to dis
    MAILSPRING_LIGHT_THEME='ui-light' # Leave empty to disable Mailspring theme switching on light theme
    MAILSPRING_DARK_THEME='ui-dark' # Leave empty to disable Mailspring theme switching on dark theme
    SLEEP_INTERVAL_SECONDS=1 # How long, in seconds, to wait between each check of the system theme
    SHOW_DEBUG_MESSAGES=0 # If debug logs should be printed


    # =======================================
    # Script functions
    # =======================================

    # Echoes provided string with a date prefix. If $2 is 1, redirects to stderr with ERROR prefix. If $3 is 1, echoes a crash message to stderr and exits with code 1
    # If $4 is 1, the message is treated as a debug log (only shown if the relevant configuration variable above is set)
    TERM_RED="\e[0;31m"
    TERM_BLUE="\e[0;34m"
    TERM_BOLD="\e[1m"
    TERM_RESET="\e[0m"
    log() {
    if [ "$2" == 1 ]; then
    echo "$(date): ERROR: $1" >&2
    echo -e "$TERM_RED""ERROR: $(date): $1""$TERM_RESET" >&2
    elif [ "$4" == 1 ]; then
    if [ "$SHOW_DEBUG_MESSAGES" == 1 ]; then
    echo -e "$TERM_BLUE""DEBUG: $(date): $1""$TERM_RESET"
    fi
    else
    echo "$(date): $1"
    echo -e "-INFO: $(date): $1""$TERM_RESET"
    fi
    if [ "$3" == 1 ]; then
    echo "CRASHED." >&2
    echo -e "$TERM_RED""$TERM_BOLD""CRASHED.""$TERM_RESET" >&2
    exit 1
    fi

    }


    # Compares the elementary dark style preference with the GTK universal dark theme preference to see if they differ (indicating changes need to be made)
    # Echoes 2 if they match; if they don't, echoes 1 to indicate the elementary pref is dark (and GTK pref is light), or echoes 0 to to indicate the opposite
    getThemeDifference() {
    @@ -52,7 +62,6 @@ getThemeDifference() {
    GTK_UNIVERSAL_PREF_IS_DARK="$(cat "$GTK_UNIVERSAL_PREF_FILE" | grep -o [0-1])"
    # Ensure the latter was 0 or 1; if not, log the error and exit
    [ "$GTK_UNIVERSAL_PREF_IS_DARK" == 0 ] || [ "$GTK_UNIVERSAL_PREF_IS_DARK" == 1 ] || log "$GTK_UNIVERSAL_PREF_FILE is missing or not valid" 1 1
    RESULT=0
    if [ "$ELEMENTARY_STYLE_IS_DARK" -eq "$GTK_UNIVERSAL_PREF_IS_DARK" ]; then echo 2
    else
    [ "$ELEMENTARY_STYLE_IS_DARK" -eq 1 ] && echo 1 || echo 0
    @@ -163,16 +172,17 @@ changeMailSpringThemeIfPossible() {
    # - Changes the Mailspring theme if possible
    runScript() {
    THEME_DIFF="$(getThemeDifference)"
    log "The theme difference is $THEME_DIFF" 0 0 1
    if [ ! "$THEME_DIFF" -eq 2 ]; then
    changeGTKUniversalDarkPref "$THEME_DIFF"
    changeNonElementaryFlatpakThemesToCurrentTheme "$THEME_DIFF"
    if [ "$THEME_DIFF" -eq 1 ]; then
    if [ ! -z "$DARK_THEME_WALLPAPER" ]; then changeWallpaper "$DARK_THEME_WALLPAPER"; fi
    if [ ! -z "$MAILSPRING_DARK_THEME" ]; then changeMailSpringThemeIfPossible "$MAILSPRING_DARK_THEME"; fi
    else
    if [ ! -z "$LIGHT_THEME_WALLPAPER" ]; then changeWallpaper "$LIGHT_THEME_WALLPAPER"; fi
    if [ ! -z "$MAILSPRING_LIGHT_THEME" ]; then changeMailSpringThemeIfPossible "$MAILSPRING_LIGHT_THEME"; fi
    fi
    changeNonElementaryFlatpakThemesToCurrentTheme "$THEME_DIFF"
    fi
    }

  15. ImranR98 revised this gist Aug 18, 2021. No changes.
  16. ImranR98 revised this gist Aug 18, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions eOSUDS.sh
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    # eOS-Universal-Dark-Style
    # =======================================
    # elementaryOS 6 has great dark themes, but they only apply to "curated" appcenter apps
    # This script forces the dark theme to work on regular Debian packages, as well as non-curated Flatpak apps
    # This script monitors changes to the system theme and forces it to work with regular Debian packages as well as non-curated Flatpak apps
    # It also optionally changes the wallpaper based on the theme
    # It also supports changing themes for the Mailspring email app (both .deb and Snap versions)

    @@ -49,7 +49,7 @@ getThemeDifference() {
    GTK_UNIVERSAL_PREF_FILE=~/.config/gtk-3.0/settings.ini
    # Grab the system dark style preference and the GTK universal dark theme preference
    ELEMENTARY_STYLE_IS_DARK="$([ "$(gsettings get org.freedesktop prefers-color-scheme)" = "'dark'" ] && echo 1 || echo 0)"
    GTK_UNIVERSAL_PREF_IS_DARK="$(cat "$GTK_UNIVERSAL_PREF_FILE" | grep gtk-application-prefer-dark-theme= | tail -c 2)"
    GTK_UNIVERSAL_PREF_IS_DARK="$(cat "$GTK_UNIVERSAL_PREF_FILE" | grep -o [0-1])"
    # Ensure the latter was 0 or 1; if not, log the error and exit
    [ "$GTK_UNIVERSAL_PREF_IS_DARK" == 0 ] || [ "$GTK_UNIVERSAL_PREF_IS_DARK" == 1 ] || log "$GTK_UNIVERSAL_PREF_FILE is missing or not valid" 1 1
    RESULT=0
  17. ImranR98 revised this gist Aug 17, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions eOSUDS.sh
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@
    # =======================================
    # 1. Change the configuration variables below as needed.
    # 2. Save this script, make it executable, and set it up to run in the background on startup.
    # This can be done in elementaryOS by adding the command to Applications -> Startup in the system settings.
    # This can be done on elementaryOS by adding the command to Applications -> Startup in the system settings.


    # =======================================
    @@ -81,7 +81,7 @@ changeNonElementaryFlatpakThemesToCurrentTheme() {
    # Where themes are stored in a Flatpak runtime
    RUNTIME_THEME_SUBDIR='active/files/share/themes'
    # Grab the current system theme
    THEME=$(gsettings get org.gnome.desktop.interface gtk-theme | head -c -2 | tail -c +2)
    THEME="$(gsettings get org.gnome.desktop.interface gtk-theme | head -c -2 | tail -c +2)"
    # Set the variable that indicates whether to add the '-dark' prefix to the theme based on the $1 argument and whether the current theme is an elementary one
    USE_CUSTOM_ELEMENTARY_DARK="$([ "$1" == 1 ] && [ ! -z "$(echo "$THEME" | grep -e "^io\.elementary\.stylesheet\..*")" ] && echo 1 || echo 0)"
    # Grab the list of app IDs for all non-appcenter installed Flatpaks
  18. ImranR98 revised this gist Aug 17, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion eOSUDS.sh
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    # elementaryOS 6 has great dark themes, but they only apply to "curated" appcenter apps
    # This script forces the dark theme to work on regular Debian packages, as well as non-curated Flatpak apps
    # It also optionally changes the wallpaper based on the theme
    # It also optionally supports changing themes for the Mailspring email app (both .deb and Snap versions)
    # It also supports changing themes for the Mailspring email app (both .deb and Snap versions)

    # =======================================
    # Instructions
  19. ImranR98 created this gist Aug 17, 2021.
    186 changes: 186 additions & 0 deletions eOSUDS.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,186 @@
    # =======================================
    # eOS-Universal-Dark-Style
    # =======================================
    # elementaryOS 6 has great dark themes, but they only apply to "curated" appcenter apps
    # This script forces the dark theme to work on regular Debian packages, as well as non-curated Flatpak apps
    # It also optionally changes the wallpaper based on the theme
    # It also optionally supports changing themes for the Mailspring email app (both .deb and Snap versions)

    # =======================================
    # Instructions
    # =======================================
    # 1. Change the configuration variables below as needed.
    # 2. Save this script, make it executable, and set it up to run in the background on startup.
    # This can be done in elementaryOS by adding the command to Applications -> Startup in the system settings.


    # =======================================
    # Configuration
    # =======================================
    LIGHT_THEME_WALLPAPER='/usr/share/backgrounds/odin.jpg' # Leave empty to disable wallpaper switching on light theme
    DARK_THEME_WALLPAPER='/usr/share/backgrounds/odin-dark.jpg' # Leave empty to disable wallpaper switching on dark theme
    MAILSPRING_LIGHT_THEME='ui-light' # Leave empty to disable Mailspring theme switching on light theme
    MAILSPRING_DARK_THEME='ui-dark' # Leave empty to disable Mailspring theme switching on dark theme
    SLEEP_INTERVAL_SECONDS=1 # How long, in seconds, to wait between each check of the system theme


    # =======================================
    # Script functions
    # =======================================

    # Echoes provided string with a date prefix. If $2 is 1, redirects to stderr with ERROR prefix. If $3 is 1, echoes a crash message to stderr and exits with code 1
    log() {
    if [ "$2" == 1 ]; then
    echo "$(date): ERROR: $1" >&2
    else
    echo "$(date): $1"
    fi
    if [ "$3" == 1 ]; then
    echo "CRASHED." >&2
    exit 1
    fi
    }


    # Compares the elementary dark style preference with the GTK universal dark theme preference to see if they differ (indicating changes need to be made)
    # Echoes 2 if they match; if they don't, echoes 1 to indicate the elementary pref is dark (and GTK pref is light), or echoes 0 to to indicate the opposite
    getThemeDifference() {
    # The location of the universal GTK dark preference config file
    GTK_UNIVERSAL_PREF_FILE=~/.config/gtk-3.0/settings.ini
    # Grab the system dark style preference and the GTK universal dark theme preference
    ELEMENTARY_STYLE_IS_DARK="$([ "$(gsettings get org.freedesktop prefers-color-scheme)" = "'dark'" ] && echo 1 || echo 0)"
    GTK_UNIVERSAL_PREF_IS_DARK="$(cat "$GTK_UNIVERSAL_PREF_FILE" | grep gtk-application-prefer-dark-theme= | tail -c 2)"
    # Ensure the latter was 0 or 1; if not, log the error and exit
    [ "$GTK_UNIVERSAL_PREF_IS_DARK" == 0 ] || [ "$GTK_UNIVERSAL_PREF_IS_DARK" == 1 ] || log "$GTK_UNIVERSAL_PREF_FILE is missing or not valid" 1 1
    RESULT=0
    if [ "$ELEMENTARY_STYLE_IS_DARK" -eq "$GTK_UNIVERSAL_PREF_IS_DARK" ]; then echo 2
    else
    [ "$ELEMENTARY_STYLE_IS_DARK" -eq 1 ] && echo 1 || echo 0
    fi
    }


    # Changes the universal GTK dark theme preference based on the provided argument
    changeGTKUniversalDarkPref() {
    # The location of the universal GTK dark preference config file
    GTK_UNIVERSAL_PREF_FILE=~/.config/gtk-3.0/settings.ini
    # Ensure the provided argument is valid before making the change
    [ "$1" == 0 ] || [ "$1" == 1 ] || log "GTK universal dark preference must be 0 or 1" 1 1
    echo "$(cat "$GTK_UNIVERSAL_PREF_FILE" | head -c -2)""$1" > "$GTK_UNIVERSAL_PREF_FILE"
    log "The GTK universal dark preference set to $1"
    }


    # Installs the current theme into the Flatpak runtimes used by all non-curated (non-appcenter) apps, then forces the apps to use it by adding an environment variable override
    # If the current theme is an elementary theme (io.elementary.stylesheet.*) and if the $1 argument is 1, it makes these changes before installing the theme:
    # - Renames it by adding a -dark suffix
    # - Modifies it so the default stylesheet is dark themed; this is needed as elementary themes do not have traditional separate dark variants
    changeNonElementaryFlatpakThemesToCurrentTheme() {
    # The location of Flatpak runtimes
    FLATPAK_RUNTIME_DIR=~/.local/share/flatpak/runtime
    # Where themes are stored in a Flatpak runtime
    RUNTIME_THEME_SUBDIR='active/files/share/themes'
    # Grab the current system theme
    THEME=$(gsettings get org.gnome.desktop.interface gtk-theme | head -c -2 | tail -c +2)
    # Set the variable that indicates whether to add the '-dark' prefix to the theme based on the $1 argument and whether the current theme is an elementary one
    USE_CUSTOM_ELEMENTARY_DARK="$([ "$1" == 1 ] && [ ! -z "$(echo "$THEME" | grep -e "^io\.elementary\.stylesheet\..*")" ] && echo 1 || echo 0)"
    # Grab the list of app IDs for all non-appcenter installed Flatpaks
    TARGET_APP_IDS=($(flatpak list --columns=application,origin,runtime | awk -F "\t" '{if ($2 != "appcenter" && $3 != "") print $1}'))
    # For each target app ID...
    for APP_ID in "${TARGET_APP_IDS[@]}"; do
    # Grab the appropriate Flatpak runtime
    RUNTIME="$(flatpak info --show-runtime "$APP_ID")"
    # Ensure the runtime has a theme directory
    RUNTIME_THEME_DIR="$FLATPAK_RUNTIME_DIR"/"$RUNTIME"/"$RUNTIME_THEME_SUBDIR"
    if [ ! -d "$RUNTIME_THEME_DIR" ]; then mkdir -p "$RUNTIME_THEME_DIR"; fi
    # Check to see if the current theme (or the current theme plus the '-dark' suffix if needed) is already installed in the runtime
    THEME_IN_RUNTIME_SEARCH_RESULT="$([ "$USE_CUSTOM_ELEMENTARY_DARK" -eq 1 ] && echo "$(ls "$RUNTIME_THEME_DIR" | grep "$THEME"-dark)" || echo "$(ls "$RUNTIME_THEME_DIR" | grep "$THEME")")"
    # If not, find where the theme is stored (in the system or user directory) and copy it into the runtime theme folder
    # Add '-dark' suffix to it if needed and make the necessary changes
    if [ -z "$THEME_IN_RUNTIME_SEARCH_RESULT" ]; then
    # Find where the theme is stored
    TARGET_THEME_LOCATION=''
    POSSIBLE_THEME_LOCATIONS=(/usr/share/themes ~/.local/share/themes)
    for LOCATION in "${POSSIBLE_THEME_LOCATIONS[@]}"; do
    if [ ! -z "$(ls "$LOCATION" | grep -e ^"$THEME"$)" ]; then TARGET_THEME_LOCATION="$LOCATION"/"$THEME"; fi
    done
    [ ! -z "$TARGET_THEME_LOCATION" ] || log "The location of the currently set theme - $THEME - could not be found" 1 1
    # If there is not need to make the '-dark' modifications, copy the theme into the Flatpak runtime; else, modify it in a temporary directory first, then copy it
    if [ ! "$USE_CUSTOM_ELEMENTARY_DARK" -eq 1 ]; then
    cp -r "$TARGET_THEME_LOCATION" "$RUNTIME_THEME_DIR"
    log "$THEME theme installed into $RUNTIME Flatpak runtime"
    else
    TEMP_DIR="$(mktemp -d)"
    cp -r "$TARGET_THEME_LOCATION" "$TEMP_DIR"
    cp -r "$TEMP_DIR"/"$THEME" "$TEMP_DIR"/"$THEME"-dark
    mv "$TEMP_DIR"/"$THEME"-dark/plank "$TEMP_DIR"/"$THEME"-dark/plank-light
    mv "$TEMP_DIR"/"$THEME"-dark/plank-dark "$TEMP_DIR"/"$THEME"-dark/plank
    mv "$TEMP_DIR"/"$THEME"-dark/gtk-3.0/gtk.css "$TEMP_DIR"/"$THEME"-dark/gtk-3.0/gtk-light.css
    mv "$TEMP_DIR"/"$THEME"-dark/gtk-3.0/gtk-dark.css "$TEMP_DIR"/"$THEME"-dark/gtk-3.0/gtk.css
    cp -r "$TEMP_DIR"/"$THEME"-dark "$RUNTIME_THEME_DIR"
    rm -r "$TEMP_DIR"
    log "$THEME theme modified to $THEME-dark and installed into $RUNTIME Flatpak runtime"
    fi
    fi
    # Do the override
    FINAL_OVERRIDE_THEME_NAME="$([ "$USE_CUSTOM_ELEMENTARY_DARK" == 1 ] && echo "$(echo "$THEME" | head -c -1 | tail -c +1)-dark" || echo "$THEME")"
    flatpak override --user --env=GTK_THEME="$FINAL_OVERRIDE_THEME_NAME" "$APP_ID"
    log "Flatpak environment variable override 'GTK_THEME="$FINAL_OVERRIDE_THEME_NAME"' added for "$APP_ID""
    done
    }


    # Changes the wallpaper to the one given in the argument
    changeWallpaper() {
    gsettings set org.gnome.desktop.background picture-uri file://"$1"
    log "Wallpaper set to $1"
    }


    # Changes the Mailspring theme to the provided string if a Mailspring config can be found (indicating it is installed)
    # If not installed or if no theme provided, does nothing; does NOT throw an error
    # Warning: This abruptly kills the Mailspring process and restarts it in the background
    changeMailSpringThemeIfPossible() {
    MAILSPRING_CONFIG_FILE=~/.config/Mailspring/config.json
    if [ ! -f "$MAILSPRING_CONFIG_FILE" ]; then MAILSPRING_CONFIG_FILE=~/snap/mailspring/common/config.json; fi
    if [ -f "$MAILSPRING_CONFIG_FILE" ] && [ ! -z "$1" ]; then
    sed -i "s/\"theme\":.*/\"theme\": \"$1\",/g" "$MAILSPRING_CONFIG_FILE"
    log "Set Mailspring theme to $1"
    pkill -f mailspring > /dev/null 2>&1
    log "Mailspring process killed"
    mailspring -b </dev/null &>/dev/null &
    log "Mailspring process started in background"
    fi
    }


    # Brings together all above functions:
    # 1. Checks to see if there is a difference between the system dark style and the GTK universal dark theme preference
    # 2. Based on that:
    # - Changes the GTK universal dark style preference
    # - Changes the theme override environment variable for all non-curated (non-elementary) Flatpak apps
    # - Changes the wallpaper if possible
    # - Changes the Mailspring theme if possible
    runScript() {
    THEME_DIFF="$(getThemeDifference)"
    if [ ! "$THEME_DIFF" -eq 2 ]; then
    changeGTKUniversalDarkPref "$THEME_DIFF"
    changeNonElementaryFlatpakThemesToCurrentTheme "$THEME_DIFF"
    if [ "$THEME_DIFF" -eq 1 ]; then
    if [ ! -z "$DARK_THEME_WALLPAPER" ]; then changeWallpaper "$DARK_THEME_WALLPAPER"; fi
    if [ ! -z "$MAILSPRING_DARK_THEME" ]; then changeMailSpringThemeIfPossible "$MAILSPRING_DARK_THEME"; fi
    else
    if [ ! -z "$LIGHT_THEME_WALLPAPER" ]; then changeWallpaper "$LIGHT_THEME_WALLPAPER"; fi
    if [ ! -z "$MAILSPRING_LIGHT_THEME" ]; then changeMailSpringThemeIfPossible "$MAILSPRING_LIGHT_THEME"; fi
    fi
    fi
    }


    # =======================================
    # Looped execution every X seconds
    # =======================================
    while [ true ]; do
    runScript
    sleep "$SLEEP_INTERVAL_SECONDS"
    done