Skip to content

Instantly share code, notes, and snippets.

@onjin
Last active July 27, 2023 11:20
Show Gist options
  • Select an option

  • Save onjin/411d7f9c6ebcaf66aa75abe6941bea55 to your computer and use it in GitHub Desktop.

Select an option

Save onjin/411d7f9c6ebcaf66aa75abe6941bea55 to your computer and use it in GitHub Desktop.

Revisions

  1. onjin revised this gist Jul 27, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion set_wallpaper.sh
    Original file line number Diff line number Diff line change
    @@ -256,7 +256,7 @@ debug "[found] $wallpaper"
    if [[ $source != "last" ]]; then
    extension="${wallpaper##*.}"
    saved="${SAVE_PATH}/last.${extension}"
    rm -f "${SAVE_PATH}/last.*" ## clean old wallpapers
    rm -f "${SAVE_PATH}"/last.* ## clean old wallpapers
    cp -f "$wallpaper" "${saved}"
    debug "[save] saved image to ${saved}"
    else
  2. onjin revised this gist Jul 27, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion set_wallpaper.sh
    Original file line number Diff line number Diff line change
    @@ -221,7 +221,7 @@ if [[ -z $wallpaper ]]; then
    fi
    if command -v "sxiv" &> /dev/null
    then
    wallpaper=$(sxiv -t -o -r $FOLDER/*/*)
    wallpaper=$(sxiv -t -o -r $FOLDER)
    else
    wallpaper=$(find $FOLDER | rofi -dmenu -show-icons)
    fi
  3. onjin renamed this gist Jul 27, 2023. 1 changed file with 73 additions and 34 deletions.
    107 changes: 73 additions & 34 deletions set_random_wallpaper.sh → set_wallpaper.sh
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@

    ## Example usage
    #
    ## ❯ set_random_wallpaper.sh \
    ## ❯ set_wallpaper.sh \
    ## --url https://minimalistic-wallpaper.demolab.com/?random \
    ## --dir ~/dotfiles/assets/wallpapers/ \
    ## --fallback ~/dotfiles/assets/background.jpg \
    @@ -21,20 +21,22 @@
    ## [found] /tmp/download_online_wallpaper.png
    ## [set] using 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 hyprpaper or feh depending on XDG_SESSION_TYPE
    ## -v | --verbose -- prints debug info to /dev/stderr - will not broke '--executor output'
    ## -b | --browse -- use with --dir to browse and set image; if 'sxiv' is available press 'm' to mark image and 'q' to quit preview and set"
    ##
    ## ❯ set_wallpaper.sh -h
    ## Usage: set_wallpaper.sh [-u | --url] [-d | --dir] [-f | --fallback] [-e | --executor]
    ## -u | --url [url] -- url to download wallpaper from
    ## -d | --dir [path] -- path to directory to choose random wallpaper from
    ## -f | --fallback [path] -- path to fallback image if no url/dir wallpaper is available
    ## -e | --executor [name] -- executor to set image; default hyprpaper or feh depending on XDG_SESSION_TYPE
    ## -v | --verbose -- prints debug info to /dev/stderr - will not broke '--executor output'
    ## -b | --browse -- use with --dir to browse and set image; if 'sxiv' is available press 'm' to mark image and 'q' to quit preview and set
    ## -l | --last -- use last used wallpaper; saved at /home/onjin/.cache/set_wallpaper/last.jpg|png
    ## -s | --save [path] -- change save path from /home/onjin/.cache/set_wallpaper/ to another one; used with '--last-saved'
    ##
    ## Supported executors:
    ## - hyprpaper -- creates ~/.config/hypr/hyprpaper.conf and restarts hyprpaper
    ## - feh -- runs feh --bg-scale
    ## - betterlockscreen -- refreshes cache and runs betterlockscreen -w
    ## - output -- prints wallpaper path to output
    ## - hyprpaper -- creates ~/.config/hypr/hyprpaper.conf and restarts hyprpaper
    ## - feh -- runs feh --bg-scale
    ## - betterlockscreen -- refreshes cache and runs betterlockscreen -w
    ## - output -- prints wallpaper path to output

    ## ---------------------------------------------------------------------------------- ##
    ## ------------------------- Helper functions --------------------------------------- ##
    @@ -85,33 +87,39 @@ function usage() {
    ## ---------------------------------------------------------------------------------- ##
    ## ------------------------- Main code ---------------------------------------------- ##
    ## ---------------------------------------------------------------------------------- ##
    SHORT=d:,e:,f:,u:,v,b,h
    LONG=dir:,executor:,fallback:,url:,verbose,browse,help
    SHORT=d:,e:,f:,u:,s:,v,b,l,h
    LONG=dir:,executor:,fallback:,url:,save:,verbose,browse,last,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 hyprpaper or feh depending on XDG_SESSION_TYPE"
    echo " -v | --verbose -- prints debug info to /dev/stderr - will not broke '--executor output'"
    echo " -b | --browse -- use with --dir to browse and set image; if 'sxiv' is available press 'm' to mark image and 'q' to quit preview and set"
    echo " -u | --url [url] -- url to download wallpaper from"
    echo " -d | --dir [path] -- path to directory to choose random wallpaper from"
    echo " -f | --fallback [path] -- path to fallback image if no url/dir wallpaper is available"
    echo " -e | --executor [name] -- executor to set image; default hyprpaper or feh depending on XDG_SESSION_TYPE"
    echo " -v | --verbose -- prints debug info to /dev/stderr - will not broke '--executor output'"
    echo " -b | --browse -- use with --dir to browse and set image; if 'sxiv' is available press 'm' to mark image and 'q' to quit preview and set"
    echo " -l | --last -- use last used wallpaper; saved at $XDG_CACHE_HOME/set_wallpaper/last.jpg|png"
    echo " -s | --save [path] -- change save path from $XDG_CACHE_HOME/set_wallpaper/ to another one; used with '--last-saved'"
    echo ""
    echo "Supported executors:"
    echo " - hyprpaper -- creates ~/.config/hypr/hyprpaper.conf and restarts hyprpaper"
    echo " - feh -- runs feh --bg-scale"
    echo " - betterlockscreen -- refreshes cache and runs betterlockscreen -w"
    echo " - output -- prints wallpaper path to output"
    echo " - hyprpaper -- creates ~/.config/hypr/hyprpaper.conf and restarts hyprpaper"
    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=
    BROWSE=
    VERBOSE=0
    BROWSE=0
    USE_LAST=0
    SAVE_PATH=${XDG_CACHE_HOME:-$HOME/.cache}/set_wallpaper

    mkdir -p "${SAVE_PATH}"

    if [[ $XDG_SESSION_TYPE == "wayland" ]]; then
    EXECUTOR=hyprpaper
    @@ -134,6 +142,10 @@ do
    FALLBACK="$2"
    shift 2
    ;;
    -s | --save )
    SAVE_PATH="$2"
    shift 2
    ;;
    -u | --url )
    URL="$2"
    shift 2
    @@ -146,6 +158,10 @@ do
    BROWSE=1
    shift 1
    ;;
    -l | --last )
    USE_LAST=1
    shift 1
    ;;
    -h | --help)
    usage
    exit 2
    @@ -169,14 +185,26 @@ function error() { debug "$1"; exit 1; }
    source=""
    wallpaper=""

    # 0. try to use last wallpaper
    if [[ ${USE_LAST} == "1" ]]; then
    wallpaper=$(find "${SAVE_PATH}" -name 'last.*')
    if [[ -n "${wallpaper}" ]]; then
    source='last'
    debug "[last] using last wallpaper $wallpaper"
    else
    debug "[skip] there is no last wallpaper saved, proceeding"
    fi
    fi

    # 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"
    if [[ -z $wallpaper ]]; then
    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
    fi

    # 2. try to get random image from folder -- -d | --url
    @@ -224,6 +252,17 @@ fi

    debug "[found] $wallpaper"

    # 3.a. save last used wallpaper
    if [[ $source != "last" ]]; then
    extension="${wallpaper##*.}"
    saved="${SAVE_PATH}/last.${extension}"
    rm -f "${SAVE_PATH}/last.*" ## clean old wallpapers
    cp -f "$wallpaper" "${saved}"
    debug "[save] saved image to ${saved}"
    else
    debug "[skip] do not save again last used wallpaper"
    fi

    # 4. set wallpaper using executor -- -e | --executor

    if [[ $EXECUTOR == "output" ]]; then
  4. onjin revised this gist Jul 27, 2023. 1 changed file with 27 additions and 4 deletions.
    31 changes: 27 additions & 4 deletions set_random_wallpaper.sh
    Original file line number Diff line number Diff line change
    @@ -28,6 +28,7 @@
    ## -f | --fallback -- path to fallback image if no url/dir wallpaper is available
    ## -e | --executor -- executor to set image; default hyprpaper or feh depending on XDG_SESSION_TYPE
    ## -v | --verbose -- prints debug info to /dev/stderr - will not broke '--executor output'
    ## -b | --browse -- use with --dir to browse and set image; if 'sxiv' is available press 'm' to mark image and 'q' to quit preview and set"
    ##
    ## Supported executors:
    ## - hyprpaper -- creates ~/.config/hypr/hyprpaper.conf and restarts hyprpaper
    @@ -84,8 +85,8 @@ function usage() {
    ## ---------------------------------------------------------------------------------- ##
    ## ------------------------- Main code ---------------------------------------------- ##
    ## ---------------------------------------------------------------------------------- ##
    SHORT=d:,e:,f:,u:,v,h
    LONG=dir:,executor:,fallback:,url:,verbose,help
    SHORT=d:,e:,f:,u:,v,b,h
    LONG=dir:,executor:,fallback:,url:,verbose,browse,help
    OPTS=$(getopt --alternative --name "$0" --options $SHORT --longoptions $LONG -- "$@")

    eval set -- "$OPTS"
    @@ -97,6 +98,7 @@ function usage() {
    echo " -f | --fallback -- path to fallback image if no url/dir wallpaper is available"
    echo " -e | --executor -- executor to set image; default hyprpaper or feh depending on XDG_SESSION_TYPE"
    echo " -v | --verbose -- prints debug info to /dev/stderr - will not broke '--executor output'"
    echo " -b | --browse -- use with --dir to browse and set image; if 'sxiv' is available press 'm' to mark image and 'q' to quit preview and set"
    echo ""
    echo "Supported executors:"
    echo " - hyprpaper -- creates ~/.config/hypr/hyprpaper.conf and restarts hyprpaper"
    @@ -109,6 +111,7 @@ FOLDER=
    FALLBACK=
    URL=
    VERBOSE=
    BROWSE=

    if [[ $XDG_SESSION_TYPE == "wayland" ]]; then
    EXECUTOR=hyprpaper
    @@ -139,6 +142,10 @@ do
    VERBOSE=1
    shift 1
    ;;
    -b | --browse )
    BROWSE=1
    shift 1
    ;;
    -h | --help)
    usage
    exit 2
    @@ -162,6 +169,7 @@ function error() { debug "$1"; exit 1; }
    source=""
    wallpaper=""


    # 1. try to download wallpaper -- -u | --url
    if [[ -n $URL ]]; then
    source=$URL
    @@ -176,7 +184,22 @@ if [[ -z $wallpaper ]]; then
    if [[ -n $FOLDER ]]; then
    source=$FOLDER
    debug "[find] using folder $FOLDER"
    wallpaper=$(set_from_folder "$FOLDER")
    # browse mode

    if [[ $BROWSE == 1 ]]; then
    if [[ -z $FOLDER ]]; then
    usage
    exit 0
    fi
    if command -v "sxiv" &> /dev/null
    then
    wallpaper=$(sxiv -t -o -r $FOLDER/*/*)
    else
    wallpaper=$(find $FOLDER | rofi -dmenu -show-icons)
    fi
    else
    wallpaper=$(set_from_folder "$FOLDER")
    fi
    else
    debug "[skip] use -d | --dir to choose wallpaper from folder"
    fi
    @@ -242,4 +265,4 @@ if [[ $EXECUTOR == "betterlockscreen" ]]; then
    fi

    notify "executor $EXECUTOR not supported :/"
    error "[set] executor $EXECUTOR not supported :/"
    error "[set] executor $EXECUTOR not supported :/"
  5. onjin revised this gist Jul 19, 2023. No changes.
  6. onjin revised this gist Jul 19, 2023. 1 changed file with 13 additions and 7 deletions.
    20 changes: 13 additions & 7 deletions set_random_wallpaper.sh
    Original file line number Diff line number Diff line change
    @@ -1,30 +1,36 @@
    #!/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 ##
    ## image. Then using executor like hyprpaper, feh, betterlockscreen or just prints ##
    ## found image to the output. ##
    ## ---------------------------------------------------------------------------------- ##
    ## https://gist.github.com/onjin/411d7f9c6ebcaf66aa75abe6941bea55
    ## ---------------------------------------------------------------------------------- ##

    ## Example usage
    #
    ## set_random_wallpaper.sh \
    ## set_random_wallpaper.sh \
    ## --url https://minimalistic-wallpaper.demolab.com/?random \
    ## --dir ~/dotfiles/assets/wallpapers/ \
    ## --fallback ~/dotfiles/assets/background.jpg \
    ## --executor hyprpaper
    ## --executor hyprpaper \
    ## --verbose

    ## verbose output (if -v | --verbose is set):
    ## [find] using https://minimalistic-wallpaper.demolab.com/?random
    ## [found] /tmp/download_online_wallpaper.png
    ## [set] using 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
    ## -e | --executor -- executor to set image; default hyprpaper 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
    ## - hyprpaper -- creates ~/.config/hypr/hyprpaper.conf and restarts hyprpaper
    ## - feh -- runs feh --bg-scale
    ## - betterlockscreen -- refreshes cache and runs betterlockscreen -w
    ## - output -- prints wallpaper path to output
    @@ -89,11 +95,11 @@ function usage() {
    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 " -e | --executor -- executor to set image; default hyprpaper 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 " - hyprpaper -- creates ~/.config/hypr/hyprpaper.conf and restarts hyprpaper"
    echo " - feh -- runs feh --bg-scale"
    echo " - betterlockscreen -- refreshes cache and runs betterlockscreen -w"
    echo " - output -- prints wallpaper path to output"
  7. onjin revised this gist Jul 19, 2023. 1 changed file with 61 additions and 13 deletions.
    74 changes: 61 additions & 13 deletions set_random_wallpaper.sh
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,33 @@
    #!/bin/bash
    ## example usage
    ## ---------------------------------------------------------------------------------- ##
    ## 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
    #
    ## in this exaple script will try:
    ## - download image from url https://minimalistic-wallpaper.demolab.com/?random
    ## - if can't then try random from directory ~/dotfiles/assets/wallpapers/
    ## - if cant't then use fallback image ~/dotfiles/assets/background.jpg
    ## - set wallpaper using 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 --------------------------------------- ##
    @@ -54,8 +70,6 @@ function notify() {
    fi
    }

    function debug() { echo "$1" > /dev/stderr; }
    function error() { debug "$1"; exit 1; }

    function usage() {
    echo "Usage: $0"
    @@ -64,8 +78,8 @@ function usage() {
    ## ---------------------------------------------------------------------------------- ##
    ## ------------------------- Main code ---------------------------------------------- ##
    ## ---------------------------------------------------------------------------------- ##
    SHORT=d:,e:,f:,u:,h
    LONG=dir:,executor:,fallback:,url:,help
    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"
    @@ -76,11 +90,19 @@ function usage() {
    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
    @@ -107,6 +129,10 @@ do
    URL="$2"
    shift 2
    ;;
    -v | --verbose )
    VERBOSE=1
    shift 1
    ;;
    -h | --help)
    usage
    exit 2
    @@ -121,6 +147,9 @@ do
    esac
    done

    function debug() { if [[ $VERBOSE == "1" ]]; then echo "$1" > /dev/stderr; fi; }
    function error() { debug "$1"; exit 1; }

    ## ---------------------------------------------------------------------------------- ##
    ## ------------------------- Find wallpaper to set ---------------------------------- ##
    ## ---------------------------------------------------------------------------------- ##
    @@ -165,9 +194,14 @@ if [[ -z $wallpaper ]]; then
    fi

    debug "[found] $wallpaper"
    notify "[$source] $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
    @@ -179,11 +213,25 @@ if [[ $EXECUTOR == "hyprpaper" ]]; then
    killall hyprpaper
    sleep 2
    hyprpaper > /dev/null 2>&1 &

    notify "[$source] $wallpaper"
    exit 0
    fi

    if [[ $EXECUTOR == "feh" ]]; then
    feg --bg-scale "$wallpaper"
    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

  8. onjin revised this gist Jul 19, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion set_random_wallpaper.sh
    Original file line number Diff line number Diff line change
    @@ -48,7 +48,7 @@ function set_from_folder() {

    function notify() {
    MSG=$1
    if ! command -v notify-send &> /dev/null
    if command -v notify-send &> /dev/null
    then
    notify-send "Wallpaper set" "$MSG"
    fi
  9. onjin revised this gist Jul 19, 2023. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions set_random_wallpaper.sh
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,12 @@
    ## --dir ~/dotfiles/assets/wallpapers/ \
    ## --fallback ~/dotfiles/assets/background.jpg \
    ## --executor hyprpaper
    #
    ## in this exaple script will try:
    ## - download image from url https://minimalistic-wallpaper.demolab.com/?random
    ## - if can't then try random from directory ~/dotfiles/assets/wallpapers/
    ## - if cant't then use fallback image ~/dotfiles/assets/background.jpg
    ## - set wallpaper using hyprpaper

    ## ---------------------------------------------------------------------------------- ##
    ## ------------------------- Helper functions --------------------------------------- ##
  10. onjin created this gist Jul 19, 2023.
    185 changes: 185 additions & 0 deletions set_random_wallpaper.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,185 @@
    #!/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 :/"