Skip to content

Instantly share code, notes, and snippets.

@debendraoli
Created April 8, 2023 19:19
Show Gist options
  • Save debendraoli/142aafd99154662a96063c4dfc6718d8 to your computer and use it in GitHub Desktop.
Save debendraoli/142aafd99154662a96063c4dfc6718d8 to your computer and use it in GitHub Desktop.
Change random background from directory with persistence support.
#!/usr/bin/env bash
wallpapers_dir="$HOME/Pictures/Wallpapers"
get_random_file() {
find "$1" -type f -print0 | xargs -0 shuf -e -n 1 -z
}
write_selected_wall() {
echo "$1" >"$HOME/.current_wall"
}
write_selected_lock() {
echo "$1" >"$HOME/.current_lock"
}
set_wallpaper() {
if [ -n "$1" ]; then
swaymsg output '*' bg "$1" fill
write_selected_wall "$1"
fi
}
set_lock() {
swaylock --font "pango:Liberation Sans 10" -e -f -t -F -i "$1"
write_selected_lock "$1"
}
loop() {
while true; do
wall=$(get_random_file "$1")
set_wallpaper "$wall"
sleep $((3600 * 2)) # 1 hour x 2
done
}
if [ "$#" -ne 1 ]; then
if [ -f "$HOME/.current_wall" ]; then
set_wallpaper "$(cat "$HOME"/.current_wall)"
else
set_wallpaper "$(get_random_file "$wallpapers_dir")"
fi
else
if [ "$1" == "loop" ]; then
loop "$wallpapers_dir"
exit
elif [ "$1" == "lock" ]; then
if [ -f "$HOME/.current_lock" ]; then
set_lock "$(cat "$HOME/.current_lock")"
else
set_lock "$(get_random_file "$wallpapers_dir")"
fi
elif [ -d "$1" ]; then
if [ -n "$(ls -A "$1")" ]; then
wall="$(get_random_file "$1")"
if [ -n "$2" ] && [ "$2" = "path" ]; then
exit 0
elif [[ "$2" =~ ^[0-9]+$ ]]; then
loop
fi
set_wallpaper "$wall"
else
echo "Dir is empty"
fi
elif [ -f "$1" ]; then
set_wallpaper "$1"
else
echo "Path does not exist"
fi
fi
@debendraoli
Copy link
Author

debendraoli commented Apr 8, 2023

Below first line loops at selected interval and second one configures swayidle and it's background.

If loop is not given in its first argument it gets random wallpaper and persists until state is deleted. So deleting ~/.current_wall or ~/.current_lock will cause a reset.

# config
exec_always '/path/to/file/waller.sh loop'

exec '/path/to/file/waller.sh lock'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment