Last active
July 27, 2023 11:20
-
-
Save onjin/411d7f9c6ebcaf66aa75abe6941bea55 to your computer and use it in GitHub Desktop.
set wallpaper by url | directory | file, using hyprpaper | feh | betterlockcreen | print - random or last used
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| ## ---------------------------------------------------------------------------------- ## | |
| ## Sets random image as wallpaper, trying remote url, local folder and fallback ## | |
| ## image. Then using executor like hyprland, feh, betterlockscreen or just prints ## | |
| ## found image to the output. ## | |
| ## ---------------------------------------------------------------------------------- ## | |
| ## https://gist.github.com/onjin/411d7f9c6ebcaf66aa75abe6941bea55 | |
| ## ---------------------------------------------------------------------------------- ## | |
| ## Example usage | |
| # | |
| ## set_random_wallpaper.sh \ | |
| ## --url https://minimalistic-wallpaper.demolab.com/?random \ | |
| ## --dir ~/dotfiles/assets/wallpapers/ \ | |
| ## --fallback ~/dotfiles/assets/background.jpg \ | |
| ## --executor hyprpaper | |
| ## ❯ set_random_wallpaper.sh -h | |
| ## Usage: /home/onjin/.local/bin/set_random_wallpaper.sh [-u | --url] [-d | --dir] [-f | --fallback] [-e | --executor] | |
| ## -u | --url -- url to download wallpaper from | |
| ## -d | --dir -- path to directory to choose random wallpaper from | |
| ## -f | --fallback -- path to fallback image if no url/dir wallpaper is available | |
| ## -e | --executor -- executor to set image; default hyprland or feh depending on XDG_SESSION_TYPE | |
| ## -v | --verbose -- prints debug info to /dev/stderr - will not broke '--executor output' | |
| ## | |
| ## Supported executors: | |
| ## - hyprland -- creates ~/.config/hypr/hyprpaper.conf and restarts hyprland | |
| ## - feh -- runs feh --bg-scale | |
| ## - betterlockscreen -- refreshes cache and runs betterlockscreen -w | |
| ## - output -- prints wallpaper path to output | |
| ## ---------------------------------------------------------------------------------- ## | |
| ## ------------------------- Helper functions --------------------------------------- ## | |
| ## ---------------------------------------------------------------------------------- ## | |
| function set_from_remote() { | |
| URL=$1 | |
| JPG_FILE=/tmp/download_online_wallpaper.jpg | |
| PNG_FILE=/tmp/download_online_wallpaper.png | |
| curl -s "$URL" > $JPG_FILE | |
| if [[ $(file $JPG_FILE | grep -q "ASCII text") ]]; then | |
| echo "" | |
| exit 0 | |
| fi | |
| wallpaper=$JPG_FILE | |
| if [[ $(file "$wallpaper" | grep PNG) ]]; then | |
| mv $JPG_FILE $PNG_FILE | |
| wallpaper=$PNG_FILE | |
| fi | |
| echo "$wallpaper" | |
| } | |
| function set_from_folder() { | |
| FOLDER=$1 | |
| wallpaper=$(find "$FOLDER" -type f | shuf --random-source=/dev/urandom -n 1) | |
| if [[ -z $wallpaper ]]; then | |
| echo "" | |
| exit 0 | |
| fi | |
| echo "$wallpaper" | |
| } | |
| function notify() { | |
| MSG=$1 | |
| if command -v notify-send &> /dev/null | |
| then | |
| notify-send "Wallpaper set" "$MSG" | |
| fi | |
| } | |
| function usage() { | |
| echo "Usage: $0" | |
| } | |
| ## ---------------------------------------------------------------------------------- ## | |
| ## ------------------------- Main code ---------------------------------------------- ## | |
| ## ---------------------------------------------------------------------------------- ## | |
| SHORT=d:,e:,f:,u:,v,h | |
| LONG=dir:,executor:,fallback:,url:,verbose,help | |
| OPTS=$(getopt --alternative --name "$0" --options $SHORT --longoptions $LONG -- "$@") | |
| eval set -- "$OPTS" | |
| function usage() { | |
| echo "Usage: $0 [-u | --url] [-d | --dir] [-f | --fallback] [-e | --executor]" | |
| echo " -u | --url -- url to download wallpaper from" | |
| echo " -d | --dir -- path to directory to choose random wallpaper from" | |
| echo " -f | --fallback -- path to fallback image if no url/dir wallpaper is available" | |
| echo " -e | --executor -- executor to set image; default hyprland or feh depending on XDG_SESSION_TYPE" | |
| echo " -v | --verbose -- prints debug info to /dev/stderr - will not broke '--executor output'" | |
| echo "" | |
| echo "Supported executors:" | |
| echo " - hyprland -- creates ~/.config/hypr/hyprpaper.conf and restarts hyprland" | |
| echo " - feh -- runs feh --bg-scale" | |
| echo " - betterlockscreen -- refreshes cache and runs betterlockscreen -w" | |
| echo " - output -- prints wallpaper path to output" | |
| } | |
| FOLDER= | |
| FALLBACK= | |
| URL= | |
| VERBOSE= | |
| if [[ $XDG_SESSION_TYPE == "wayland" ]]; then | |
| EXECUTOR=hyprpaper | |
| else | |
| EXECUTOR=feh | |
| fi | |
| while : | |
| do | |
| case "$1" in | |
| -d | --dir ) | |
| FOLDER="$2" | |
| shift 2 | |
| ;; | |
| -e | --executor ) | |
| EXECUTOR="$2" | |
| shift 2 | |
| ;; | |
| -f | --fallback ) | |
| FALLBACK="$2" | |
| shift 2 | |
| ;; | |
| -u | --url ) | |
| URL="$2" | |
| shift 2 | |
| ;; | |
| -v | --verbose ) | |
| VERBOSE=1 | |
| shift 1 | |
| ;; | |
| -h | --help) | |
| usage | |
| exit 2 | |
| ;; | |
| --) | |
| shift; | |
| break | |
| ;; | |
| *) | |
| echo "Unexpected option: $1" | |
| ;; | |
| esac | |
| done | |
| function debug() { if [[ $VERBOSE == "1" ]]; then echo "$1" > /dev/stderr; fi; } | |
| function error() { debug "$1"; exit 1; } | |
| ## ---------------------------------------------------------------------------------- ## | |
| ## ------------------------- Find wallpaper to set ---------------------------------- ## | |
| ## ---------------------------------------------------------------------------------- ## | |
| source="" | |
| wallpaper="" | |
| # 1. try to download wallpaper -- -u | --url | |
| if [[ -n $URL ]]; then | |
| source=$URL | |
| debug "[find] using $URL" | |
| wallpaper=$(set_from_remote $URL) | |
| else | |
| debug "[skip] use -u | --url to download wallpaper from url" | |
| fi | |
| # 2. try to get random image from folder -- -d | --url | |
| if [[ -z $wallpaper ]]; then | |
| if [[ -n $FOLDER ]]; then | |
| source=$FOLDER | |
| debug "[find] using folder $FOLDER" | |
| wallpaper=$(set_from_folder "$FOLDER") | |
| else | |
| debug "[skip] use -d | --dir to choose wallpaper from folder" | |
| fi | |
| fi | |
| # 3. try to set fallback image -- -f | --fallback | |
| if [[ -z $wallpaper ]]; then | |
| if [[ -n $FALLBACK ]]; then | |
| debug "[find] using fallback $FALLBACK" | |
| source=$FALLBACK | |
| wallpaper=$FALLBACK | |
| else | |
| debug "[skip] use -f | --fallback to set given image as wallpaper" | |
| fi | |
| fi | |
| if [[ -z $wallpaper ]]; then | |
| debug "[find] ¯\(ツ)/¯ cannot set any wallpaper" | |
| notify "¯\(ツ)/¯ cannot set any wallpaper" | |
| exit 1 | |
| fi | |
| debug "[found] $wallpaper" | |
| # 4. set wallpaper using executor -- -e | --executor | |
| if [[ $EXECUTOR == "output" ]]; then | |
| echo $wallpaper | |
| exit 0 | |
| fi | |
| debug "[set] using $EXECUTOR" | |
| if ! command -v "$EXECUTOR" &> /dev/null | |
| then | |
| error "[set] executor $EXECUTOR not found :/" | |
| fi | |
| if [[ $EXECUTOR == "hyprpaper" ]]; then | |
| echo -ne "preload = $wallpaper \nwallpaper = ,$wallpaper" > ~/.config/hypr/hyprpaper.conf | |
| killall hyprpaper | |
| sleep 2 | |
| hyprpaper > /dev/null 2>&1 & | |
| notify "[$source] $wallpaper" | |
| exit 0 | |
| fi | |
| if [[ $EXECUTOR == "feh" ]]; then | |
| feh --bg-scale "$wallpaper" | |
| notify "[$source] $wallpaper" | |
| exit 0 | |
| fi | |
| if [[ $EXECUTOR == "betterlockscreen" ]]; then | |
| debug "[betterlockscreen] updating cache ..." | |
| betterlockscreen -u "$wallpaper" | |
| debug "[betterlockscreen] setting wallpaper" | |
| betterlockscreen -w | |
| notify "[$source] $wallpaper" | |
| exit 0 | |
| fi | |
| notify "executor $EXECUTOR not supported :/" | |
| error "[set] executor $EXECUTOR not supported :/" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment