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 | |
| ## example usage | |
| # | |
| ## set_random_wallpaper.sh \ | |
| ## --url https://minimalistic-wallpaper.demolab.com/?random \ | |
| ## --dir ~/dotfiles/assets/wallpapers/ \ | |
| ## --fallback ~/dotfiles/assets/background.jpg \ | |
| ## --executor hyprpaper | |
| ## ---------------------------------------------------------------------------------- ## | |
| ## ------------------------- 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 debug() { echo "$1" > /dev/stderr; } | |
| function error() { debug "$1"; exit 1; } | |
| function usage() { | |
| echo "Usage: $0" | |
| } | |
| ## ---------------------------------------------------------------------------------- ## | |
| ## ------------------------- Main code ---------------------------------------------- ## | |
| ## ---------------------------------------------------------------------------------- ## | |
| SHORT=d:,e:,f:,u:,h | |
| LONG=dir:,executor:,fallback:,url:,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" | |
| } | |
| FOLDER= | |
| FALLBACK= | |
| URL= | |
| 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 | |
| ;; | |
| -h | --help) | |
| usage | |
| exit 2 | |
| ;; | |
| --) | |
| shift; | |
| break | |
| ;; | |
| *) | |
| echo "Unexpected option: $1" | |
| ;; | |
| esac | |
| done | |
| ## ---------------------------------------------------------------------------------- ## | |
| ## ------------------------- 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" | |
| notify "[$source] $wallpaper" | |
| # 4. set wallpaper using executor -- -e | --executor | |
| 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 & | |
| exit 0 | |
| fi | |
| if [[ $EXECUTOR == "feh" ]]; then | |
| feg --bg-scale "$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