Random wallpaper with swaybg

Setting a wallpaper in Sway, with swaybg, is easy. Unfortunately there is no way of setting a random wallpaper automatically out of the box. Here is a little helper script to do that.

The script is based on a post from Silvain Durand1 with some slight modifications. I just linked the script my sway config instead of setting a background there.

Sway config: ~/.config/sway/config

1
2
# ...
exec ~/scripts/swaybg/rand_wallpaper.sh

The script spawns a new swaybg instance, changes the wallpaper, and kills the old instance. With this approach there is no flickering of the background when changing.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#!/usr/bin/env sh

# Wallpaper directory
WP_FOLDER=~/wallpaper

# Time in seconds to change wallpaper
WAIT_TIME=599

while true; do
    PID=$(pidof swaybg)
    FILE=$(find "$WP_FOLDER" -type f -name '*.png' -o -name '*.jpg' | shuf -n1)
    swaybg -i "$FILE" -m fill &
    sleep 1
    kill "$PID"
    sleep "$WAIT_TIME"
done

An always up-to-date version can be found in my dotfiles.

This post was created on .