Random wallpapers for Nitrogen (Linux)

Here I am sharing my script which randomly selects picture file from the defined folder and applies this file as a wallpaper. This script is written for one of the most popular programs of this kind in Linux - nitrogen.

When I decided to make this script, I wanted it to do the following:

  1. Randomly select file as a wallpaper.
  2. Has minimum writing operations on the disk.

Here is what I came with.

I keep my wallpaper files in the ~/wallpaper/ folder and they are all JPG.

“nitrogen” config file

At first, I looked into the nitrogen configuration files. On my system they are located here: ~/.config/nitrogen/ and there are two files there: nitrogen.cfg and bg-saved.cfg. And I am interested in the latter.

bg-saved.cfg looks like this on my system:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# For the display 0 (default)
[xin_0]
# Path to the image file
file=~/wallpapers/current_bg_image.jpg
# Mode of wallpaper (I am using "scaled"). E.g. 4 is "stretched".
mode=3
# Background color
bgcolor=#000000

# For the display 1
[xin_1]
file=~/wallpapers/current_bg_image.jpg
mode=3
bgcolor=#000000

As you can see, I am using the same background on both displays (default laptop display and some external one if I use it).

The script

The trick here is that I want nitrogen configuration file from above not to change, so I came up with the use of symbolic link as a reference on the actually selected file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/bin/sh

# It is needed to make nitrogen work correctly
export DISPLAY=":0"

# Defining the directory with wallpapers
BG_DIR=$HOME/wallpapers

# Getting date of the creation of the current_bg_image.jpg (used in the nitrogen configuration)
BG_IMAGE_DATE=$(ls -l --time-style=full-iso "${BG_DIR}"/current_bg_image.jpg | awk '{print $6}')

# Getting current date
TODAY=$(date -I)

# Checking, if dates differ, then we randomly select new wallpaper
if [ "${BG_IMAGE_DATE}" != "${TODAY}" ]
then
    # Removing of the old symlink
    [ -f "${BG_DIR}"/current_bg_image.jpg ] && rm "${BG_DIR}"/current_bg_image.jpg 

    # Feeding random generator with the date in seconds (UNIX time)
    RANDOM=$$$(date +%s)

    # Generating list of all wallpapers in the directory
    BG_LIST=("${BG_DIR}"/*.jpg)

    # Counting total number of files
    BG_NUM=$(ls -1 "${BG_DIR}" | wc -l)

    # Randomly select some number from the total number of wallpapers
    SELECTED_BG=$(( $RANDOM % ${BG_NUM} ))

    # Creating new symbolic link to the selected wallpaper with the name "current_bg_image.jpg" 
    ln -s "${BG_LIST[$SELECTED_BG]}" "${BG_DIR}"/current_bg_image.jpg 

    # refreshing wallpaper image
    nitrogen --restore > /dev/null 2>&1 &
fi

Cron

I want my wallpaper picture to change every day. But because I use laptop, and it might be off in a particular point of time, I use cron task which runs every hour from 7 AM till 8 PM every day.

This guarantees that when I open laptop at any time of day, my wallpaper will eventually change.