Skip to content

Instantly share code, notes, and snippets.

@evanjs
Last active May 2, 2018 01:51
Show Gist options
  • Save evanjs/a901982defdbb93bdbe6db94659cb540 to your computer and use it in GitHub Desktop.
Save evanjs/a901982defdbb93bdbe6db94659cb540 to your computer and use it in GitHub Desktop.
Query images in $wallpapers and select a random image for each connected display.
#!/bin/bash
# sets a random background image with a resolution
# that matches each connected display
# Query connected displays
echo "Attempting to randomize wallpapers ..."
wallpapers="/usr/share/wallpapers"
# get a list of connected displays and their respective resolutions
displays=$(xrandr --prop | egrep '\sconnected' | awk '{print $1" " $3}' | cut -d '+' -f-1 )
# set the app which will be used to set background images
base="feh "
shopt -s globstar
while IFS='\n' read -ra line; do
display_name=$(echo $line | cut -d ' ' -f1)
resolution=$(echo $line | cut -d ' ' -f2)
echo -e "\nDisplay: $line\n"
echo "Resolution: $resolution"
# find pictures in $wallpapers that match the resolution $resolution
#echo $wallpapers
pictures=$(identify -quiet -ping -format '%d/%f %wx%h\n' $wallpapers/**/*.jpg | rg $resolution$ | cut -d ' ' -f1)
echo -e "Pictures: $pictures"
# select a single picture at random
random_picture=$(echo $pictures | tr ' ' '\n' |shuf | head -n1)
# set picture as background
#echo "Random picture: $random_picture"
base+="--bg-fill $random_picture "
echo "Set $display_name to $random_picture"
done <<<"$displays"
#echo "Command: $base"
exec $base
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment