New: letterpost.eu, forms for static sites

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 Sylvain Durand1 with some slight modifications. I just linked the script in my sway config instead of setting a background there.

Sway config: ~/.config/sway/config

1# ...
2exec ~/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#!/usr/bin/env sh
 2
 3# Wallpaper directory
 4WP_FOLDER=~/wallpaper
 5
 6# Time in seconds to change wallpaper
 7WAIT_TIME=599
 8
 9while true; do
10    PID=$(pidof swaybg)
11    FILE=$(find "$WP_FOLDER" -type f -name '*.png' -o -name '*.jpg' | shuf -n1)
12    swaybg -i "$FILE" -m fill &
13    sleep 1
14    kill "$PID"
15    sleep "$WAIT_TIME"
16done

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

This content was created on and updated on .
Enjoying codedge?

If I helped you in some way with my writing, a small contribution would support me. Your support keeps me creating!
Send a tip