Skip to content

Instantly share code, notes, and snippets.

@elkcityhazard
Last active June 4, 2025 00:56
Show Gist options
  • Save elkcityhazard/d0af8a2bd0f080458d61b19539152e43 to your computer and use it in GitHub Desktop.
Save elkcityhazard/d0af8a2bd0f080458d61b19539152e43 to your computer and use it in GitHub Desktop.
hyprpaper script to execute with waybar

Random Wallpaper functionality with hyprpaper, waybar, and hyprland

This assumes you are using Hyprland and have Hyprpaper installed.

Don't forget to update your WALLPAPER_DIR path. Also, I put the script in my .config/hypr folder, you should put it where you keep your shell scripts.

hyprpaper_refresh.sh

#!/bin/bash
ATTEMPTS=0
MAX_NUM=10
FILES=()
WALLPAPER_DIR="/usr/share/backgrounds"
CURRENT_WALL=$(hyprctl --instance 0 hyprpaper listloaded)
WALLPAPER=""

while true; do
    ((ATTEMPTS++))
    echo "Attempt #$ATTEMPTS"

    if (( ATTEMPTS > MAX_NUM )); then
        notify-send "Error: we tried to set the following files: ${FILES[*]}"
        exit 1
    fi

    WALLPAPER=$(find "$WALLPAPER_DIR" -type f ! -name "$(basename "$CURRENT_WALL")" | shuf -n 1)
    if [[ -z "$WALLPAPER" ]]; then
        notify-send "Error: No suitable wallpapers found."
        exit 1
    fi

    MIME_TYPE=$(file --mime-type -b "$WALLPAPER")
    if [[ ! "$MIME_TYPE" =~ ^image/ ]]; then
        echo "$WALLPAPER - $MIME_TYPE"
        FILES+=("$WALLPAPER")
        notify-send "WARNING" "The selected file '$WALLPAPER' is not an image (MIME type: $MIME_TYPE)."
    else
        echo "Setting: $WALLPAPER"
        # Apply the selected wallpaper
        hyprctl --instance 0 hyprpaper reload ,"$WALLPAPER"
        break
    fi
done

Make It Executable

chmod +x hyprpaper_refresh.sh

Add To Waybar

  "custom/wallpaper": {
    "format": "",
    "on-click": "/bin/bash ~/.config/hypr/hypr_refresh.sh"
  },

Add To Crontab

crontab -e

*/5 * * * * ~/.config/hypr/hypr_refresh.sh >> ~/crontab_log.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment