Skip to content

Instantly share code, notes, and snippets.

@641i130
Last active January 13, 2023 00:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 641i130/acee16ba351e522c5ad8dc9cc9af926f to your computer and use it in GitHub Desktop.
Save 641i130/acee16ba351e522c5ad8dc9cc9af926f to your computer and use it in GitHub Desktop.
Cycle through multiple wallpaper folders for a proper shuffled feel. Will re-shuffle once it has done a cycle. This was written using ChatGPT and further cleanup by me.
#!/bin/bash
# Directories containing the images for both landscape and portrait
p_pics="$HOME/Pictures/Wallpapers/portrait"
l_pics="$HOME/Pictures/Wallpapers/landscape"
# File to store the shuffled images for both landscape and portrait
p_pics_shuf="$HOME/.p_pics_shuf"
l_pics_shuf="$HOME/.l_pics_shuf"
# Flag file to keep track of whether the shuffled images have been used up
flag_file="$HOME/.bg_shuf_flag"
# Check if the shuffled image files exist
if [ ! -f "$p_pics_shuf" ] || [ ! -f "$l_pics_shuf" ] || [ -f "$flag_file" ]; then
# Shuffle the images in the portrait and landscape directories
shuf -e "$p_pics"/* > "$p_pics_shuf"
shuf -e "$l_pics"/* > "$l_pics_shuf"
# Reset the flag file
rm -f "$flag_file"
fi
# Read the shuffled images into arrays
p_p_shuf=( $(cat "$p_pics_shuf") )
l_p_shuf=( $(cat "$l_pics_shuf") )
# File to store the current image index
index_portrait="$HOME/.bg_p"
index_landscape="$HOME/.bg_l"
# Get the current index from the file, or set it to 0 if the file doesn't exist
current_index_p=${current_index_p:-$(cat "$index_portrait")}
current_index_l=${current_index_l:-$(cat "$index_landscape")}
# Set the background to the current image
feh --bg-fill "${l_p_shuf[current_index_l]}" "${p_p_shuf[current_index_p]}"
# Update the current index and save it to the file
current_index_l=$(( (current_index_l + 1) % ${#l_p_shuf[@]} ))
echo "$current_index_l" > "$index_landscape"
current_index_p=$(( (current_index_p + 1) % ${#p_p_shuf[@]} ))
echo "$current_index_p" > "$index_portrait"
# Check if all images have been used
if [ $current_index_l -eq 0 ] && [ $current_index_p -eq 0 ]; then
touch "$flag_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment