Code Self Study Forum

Desktop Background Switcher for i3wm

I wanted rotating background images on my desktop in i3wm (a great tiling window manager) and had trouble configuring some of the other options, so I wrote this script:

#!/bin/zsh

# Put this file at `~/bin/desktop_background_switcher`
# and `chmod +x ~/bin/desktop_background_switcher`

# Load a directory of image filenames. Change the path to match your system.
PICS=($HOME/Pictures/WALLPAPERS/LIVE/*)

# Choose a random picture
CURRENT_PIC="${PICS[RANDOM % ${#PICS[@]}+1]}"

# Change desktop background
feh --bg-fill "$CURRENT_PIC"

# Notify the user
notify-send -i "$CURRENT_PIC" 'Desktop Changed' $(basename -- "$CURRENT_PIC")

Set it to switch automatically by typing crontab -e and entering the following information:

SHELL=/usr/bin/zsh
PATH=$HOME/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
DISPLAY=:0

# Switches the desktop background every 20 minutes
*/20 * * * * $HOME/bin/desktop_background_switcher

# Or for every hour
# 0 * * * * $HOME/bin/desktop_background_switcher

Pexels is a good site for finding images. I try to put a mix of images there that get me to think about what life is like in various places around the world.