Random GNOME-Wallpaper

You always wanted to have a random Wallpaper for GNOME? Read ahead, I found a public domain script and modified it, so it works fine with Ubuntu and supports more image types.

You need a new directory to save only the wallpapers in (e.g. ~/.wallpapers). Your wallpapers should have the correct size for your resolution and can be .jpg, .png, .gif and .bmp.

Then copy this PERL script to a new file (e.g. named ~/random_background.pl), edit the path and make it executeable:
(Sorry for the messed up code, I’m not too firm with WordPress)

#!/usr/bin/perl -w

use strict;

my $pic_path = "/home/martin/.wallpapers/";

sub get_selected_pic($)
{
my $sel_pic = $_[0];
my $fname;

if(opendir(DIRFH, $pic_path))
{

while(defined($fname = readdir(DIRFH)))
{
if($fname eq "." ||
$fname eq ".." ||
$fname !~ /.jpg|.png|.gif|.bmp/)
{
next;
}
if($sel_pic == 0)
{
# We select the file which appears
# at the sel_pic number
return $fname;
}
$sel_pic--;
}

closedir(DIRFH);
}
}

my $tot_pic = `ls $pic_path | wc -l`;
my $sel_pic = int(rand($tot_pic));
my $sel_pic_name = get_selected_pic($sel_pic);

if($sel_pic_name =~ /.jpg|.png|.gif|.bmp$/)
{
`gconftool -t str -s /desktop/gnome/background/picture_filename "$pic_path/$sel_pic_name"`;
}

Now you have basically two options:

  1. Changing the wallpaper periodically, e.g. each x minutes. Type crontab -e on your bash, then insert line:
    x * * * * * /path/random_background.pl
    where x are the minutes (from 1 to 59).
  2. Changing the wallpaper at each login. This is much easier then changing the wallpaper periodically. Just go to System -> Preferences -> Sessions -> Startup Programs and add your script.

Just put atleast one wallpaper in your wallpapers folder and enjoy the rotating wallpapers.

3 Comments

  1. Martin Henze

    I’m currently using the script posted to change my wallpaper once when I log in (not periodically). Does WallpaperZapper offers this function, too?

Comments are closed.