SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2013-05-31 18:59:47

Poohblah
Member
Registered: 2013-05-29
Posts: 13

How-to: random wallpaper slideshow

I wanted to have a random wallpaper slideshow that rotates out at arbitrary intervals. I got this set up without very much work and I'll show you how I did it.

First, you will need a standard UNIX tool called "at", which is not included in the #! distro. At-jobs are similar to cron jobs but are scheduled to run once at some time in the future rather than at periodic intervals. If you do not want to install the at package, you can use a standard cron job instead, but your wallpaper won't get rotated out if your computer is shut down when the cronjob is supposed to run. So, to begin:

$ sudo apt-get install at

Next, let's write a simple script that chooses a random wall paper from a directory. For this script I chose ~/images/wallpapers, though of course you can change this to whatever you like. I called this script "randwall.sh". You must put this script somewhere in your $PATH. I chose to put it in ~/bin/.

#!/bin/bash

# set the DISPLAY variable because cron or at won't do it for us
export DISPLAY=:0

# the location of the directory which holds the wallpapers
walldir=~/images/wallpapers

# get a list of all image files in that directory
files=($walldir/*.jpg $walldir/*.png)

# choose a random file from that directory
wallpaper=$(printf "%s\n" "${files[RANDOM % ${#files[@]}]}")

# call the function which changes the wallpaper for us
nitrogen --set-auto $wallpaper --save

# call this script recursively
echo "randwall.sh" | at now + 30 minutes -M # "-M" prevents mail from being sent to user

Okay, that's it. Some of the lines may seem like magic, so I'll explain -

  • the variable DISPLAY is an environment variable that tells the X server what display to put output on. It's normally set when you start an X session, but at and cron don't listen to environment variables, so we have to set it manually. On a laptop or most desktop systems you are probably going to be using a DISPLAY value of :0.

  • the variable wallpaper uses bash's array methods to pull a random element from the files array. I got the idea for this script when I saw this StackOverflow thread.

  • we need to use both the --set and the --save flags to nitrogen to make sure our wallpaper is saved for the next time we log in

  • the at job is called recursively, so a new job is queued up for every 30 minutes after one finishes. You can set arbitrary time periods using "minutes", "hours", "days", "weeks", etc. See the at man page for more info.

Okay, now that you have written the script and saved it to somewhere in your $PATH, let's verify that it works.

$ chmod 755 /path/to/randwall.sh
$ randwall.sh

Your background should have changed to a random image in ~/images/wallpapers. Verify that the at job is queued up by running:

$ atq

The output should show a queued job like this:

12	Fri May 31 12:42:00 2013 a jay

Where "a" is the letter representing the at queue (you can have a bunch of different queues each represented by a different letter) and "jay" is my username on my machine.
Alright! That's all there is to it! Your wallpaper will now change to something random from ~/images/wallpapers every 30 minutes while you are logged in. Don't forget to add "randwall.sh" to the bottom of your ~/.config/openbox/autostart.sh so that it starts the rotation every time you login!

If you are having problems, remove the "-M" flag from the last line and keep an eye on your mail or /var/spool/mail/<username> since at-jobs send warnings and errors via the mail spool. This should help with debugging.

Offline

Help fund CrunchBang, donate to the project!

#2 2013-05-31 20:37:10

uname
#! Junkie
Registered: 2013-03-15
Posts: 476

Re: How-to: random wallpaper slideshow

Randomize in one line. Hope it works.

nitrogen --set-scaled --save "/images/wallpapers/$(ls /images/wallpapers/*.{jpg,png} 2>/dev/null | sed 's#.*/##' | sort -R | sed 1q)"

Offline

#3 2013-06-01 02:12:52

pidsley
Window Mangler
Registered: 2012-05-23
Posts: 1,752

Re: How-to: random wallpaper slideshow

Offline

#4 2013-06-01 06:02:49

Poohblah
Member
Registered: 2013-05-29
Posts: 13

Re: How-to: random wallpaper slideshow

ooh, I should have posted in that thread instead. I wasn't aware of it. Thanks pidsley. Though I did mention cron in my post wink

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo