#!/usr/bin/env bash # # Recursively searches the specified directory (the first script argument), # randomly selects one image, and set that image as the wallpaper. # # Usage: # random_wallpaper.sh /path/to/wallpapers # # Requirements (X11): # feh # # Requirements (Wayland): # swaybg # x11_set() { local wallpaper="${1}" # Check for feh type -p feh 2>/dev/null 1>/dev/null if [ $? -eq 1 ]; then echo "Required program 'feh' not found. Please install and run again." return 1 fi # Set the wallpaper feh --bg-fill "${wallpaper}" } wayland_set() { local wallpaper="${1}" local pidfile=/dev/shm/swaybg-$(whoami).pid printf 'Setting wallpaper to %s\n' "${wallpaper}" swaybg -o '*' -i "${wallpaper}" -m fill 2>/dev/null 1>/dev/null & echo $! > "${pidfile}.new" sleep .5 if [ -f "${pidfile}" ] && [ -d /proc/$(cat ${pidfile}) ]; then kill $(cat "${pidfile}") fi mv "${pidfile}.new" "${pidfile}" } # Make sure a wallpaper path was specified if [ -z ${1} ]; then echo "Please specify a wallpaper path." exit 1 fi search_path=${@} randpic=$(find ${search_path} -type f \ -iname '*.jpg' \ -o -iname '*.png' \ | sort -R | head -n 1) if [ -n "${WAYLAND_DISPLAY:-}" ]; then wayland_set "${randpic}" elif [ -n "${DISPLAY:-}" ]; then x11_set "${randpic}" else printf 'ERROR: Could not detect display manager\n' exit 1 fi