Tag Archives: xmonad

Randomly Set XMonad Background

Feh can Stretch, Center, and Tile a background image, among other functions, but cannot Scale an image. That is fine. Feh follows the Unix Philosophy. This is where Image Magick and some bash scripting come in.

The plan is to create a directory of images that a bash script will peruse and feh will plaster to the background.

Start by compiling a directory of images to use. Copy these images to a new directory as the next step could irrevocably change the size of the used images. This is unlikely, however, as Image Magick tends to leave the originals alone.

Enter this into a terminal emulator:

convert * -resize 1024×768 background

It runs “convert” on all files. Resizes them to a best fit within a 1024 by 768 pixel box. Then names the new copy after the last argument. Adjust the numbers to your screen’s resolution. You can find it using:

xdpyinfo | grep dimensions

After the images are properly sized, it is time to set up the random selector. This is the bash code used. It assumes you have feh installed. Stick it in a file named “Roller” (or whatever you want; change the $NAME variable so that it can prevent itself from being selected) and make it executable.


#!/bin/bash
# Roller
#
# Version 1
#
# Does not sterilize input and such
# Assumes feh is installed

# Change to match the name of the file the script sits in
# Only need to change the script's name once this way
NAME="Roller"

# Change to the directory that contains the images
# /home/user/Pictures/Wallpaper/*
dir="/home/user/Pictures/Wallpaper/*"

# Counts the number of files in the directory
count=`ls $dir | wc -l`

# Sets a variable for counting the number of files skipped to that point
j=0

# Generates a psuedo-random number
ran_num=$RANDOM

# Sets ran_num to an integer in range:
# 0 to $count - 1
let "ran_num %= $count"

# For all files in the directory
for file in $dir
do
# If the file to be used has been found
if [ "$j" -eq "$ran_num" ]
then
# If the image to be used is not this script
# (Originally, this script sat in the same directory as the images)
if [ "$file" != "$NAME" ]
then
# Set the image using feh
feh --bg-center $file
# Break out of the loop: it is done
break
else
# Skip past Roller, will use the next file found
let "j++"
let "ran_num++"
fi

# Appropriate file not found yet: increment and try again
else
let "j++"
fi
done

exit 0

Edit the $NAME and $dir variables to your needs.

Advertisements

1 Comment

Filed under Computing, How-To