karstenw

toggle_darkmode

Aug 3rd, 2025
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.79 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Set the DISPLAY variable (usually :0 for the primary display)
  4. export DISPLAY=:0
  5. logger -t toggle_darkmode "DISPLAY: $DISPLAY"
  6.  
  7. # Attempt to find the DBUS_SESSION_BUS_ADDRESS
  8. # Get all PIDs for gnome-session
  9. GNOME_PIDS=$(pgrep gnome-session)
  10.  
  11. # Check if any PIDs were found
  12. if [ -z "$GNOME_PIDS" ]; then
  13.     logger -t toggle_darkmode "Could not find any running gnome-session PIDs."
  14.     exit 1
  15. fi
  16.  
  17. DBUS_ADDRESS_FOUND=""
  18. for PID_SINGLE in $GNOME_PIDS; do
  19.     # Try to extract DBUS_SESSION_BUS_ADDRESS from this specific PID's environment
  20.     CURRENT_DBUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS "/proc/$PID_SINGLE/environ" | tr '\0' '\n' | cut -d= -f2-)
  21. #    CURRENT_DBUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS "/proc/$PID_SINGLE/environ" | cut -d= -f2-)
  22.    
  23.     if [ -n "$CURRENT_DBUS_ADDRESS" ]; then
  24.         DBUS_ADDRESS_FOUND="$CURRENT_DBUS_ADDRESS"
  25.     export DBUS_SESSION_BUS_ADDRESS="$DBUS_ADDRESS_FOUND"
  26.         break # Found it, so exit the loop
  27.     else
  28.         logger -t toggle_darkmode "PID $PID_SINGLE does not have DBUS_SESSION_BUS_ADDRESS."
  29.     fi
  30. done
  31. logger -t toggle_darkmode "DBUS_SESSION_BUS_ADDRESS: $DBUS_SESSION_BUS_ADDRESS"
  32.  
  33. # Funktion zum Umschalten auf Dark Mode
  34. set_dark_mode() {
  35.     # Gnome
  36.     gsettings set org.gnome.desktop.interface gtk-theme 'Adwaita-dark'
  37.     gsettings set org.gnome.shell.extensions.user-theme name 'Adwaita-dark'
  38.     gsettings set org.gnome.desktop.interface color-scheme 'prefer-dark'
  39.    
  40.     # wezterm, ensure that config.automatically_reload_config = true
  41.     sed -i 's/config.color_scheme = .*/config.color_scheme = "Catppuccin Mocha"/' ~/.wezterm.lua
  42.    
  43.     logger -t toggle_darkmode "entering dark mode"
  44. }
  45.  
  46. # Funktion zum Umschalten auf Light Mode
  47. set_light_mode() {
  48.     # Gnome
  49.     gsettings set org.gnome.desktop.interface gtk-theme 'Adwaita'
  50.     gsettings set org.gnome.shell.extensions.user-theme name 'Adwaita'
  51.     gsettings set org.gnome.desktop.interface color-scheme 'prefer-light'
  52.  
  53.     # wezterm, ensure that config.automatically_reload_config = true
  54.     sed -i 's/config.color_scheme = .*/config.color_scheme = "Catppuccin Latte"/' ~/.wezterm.lua
  55.    
  56.     logger -t toggle_darkmode "entering light mode"
  57. }
  58.  
  59. # Funktion für den Auto-Modus basierend auf Uhrzeit
  60. set_auto_mode() {
  61.     # Aktuelle Stunde im 24h-Format (z. B. 14 für 14:00 Uhr)
  62.     current_hour=$(date +%H)
  63.    
  64.     # Zwischen 6:00 und 20:00 Uhr Light Mode, sonst Dark Mode
  65.     if [ "$current_hour" -ge 6 ] && [ "$current_hour" -lt 20 ]; then
  66.         set_light_mode
  67.     else
  68.         set_dark_mode
  69.     fi
  70. }
  71.  
  72. if [ "$1" = "light" ]; then
  73.     set_light_mode
  74. elif [ "$1" = "dark" ]; then
  75.     set_dark_mode
  76. elif [ "$1" = "auto" ]; then
  77.     set_auto_mode
  78. else
  79.     echo "Unknown parameter, must be [light|dark|auto]."
  80.     exit 1
  81. fi
  82.  
  83.  
Advertisement
Add Comment
Please, Sign In to add comment