For some reason, I don’t like having the same background for an extended amount of time (as in, more than 15 minutes). Call me crazy, but that’s just how it is.
So when I bought my current laptop (Asus G1S), it came pre-installed with Windows Vista Ultimate, so I figured I’d give it a try. With the sidebar thing I found an widget that changed my desktop background every 15 minutes or whatever I liked. Hmm, cool enough, but I don’t like that sidebar, and for some reason Win Vista always seem to run awfully slow on my computers (maybe it’s just me abusing them).
Anyways, after a few days, I’ve had enough with Vista. So I decided to give good old Win XP SP2 a try on this brand new baby. After an install, things are flying like crazy, but nothing is changing my desktop background :\
After some checking around, it seems that in a vanilla XP, it ain’t possible to change the desktop background by command line or even by API as far as I found. So I was stuck changing it manually.
A few weeks ago, I’d had enough of Windows XP (as I always do after a little while). I downloaded Ubunty Hardy, installed it, and always I was happy when I saw the comforting GNOME desktop. Ahhh.
But still, my background isn’t changing by itself
Must fix.
The “easy” solution (atleast in my head), write a perl script that manages it for me.
My starting point was this:
- All my wallpapers are in one folder.
- Not all images matches my desktop resolution.
- I don’t really wanna think about what resolution I’m running, I want the script to figure it out themself.
So, this is what I ended up with:
You need Image:Size to be able to use this script. If you’re running Ubuntu, just do:
sudo apt-get install libimage-size-perl
#!/usr/bin/perl
use Image::Size;## Set this to 0 if you don’t want this program to do anything
$go = 1;## Desktop background changer for GNOME
## By Anders Østhus (grapz666+perl@gmail.com)
## 27-04-2008
##
## You are free to use and spread this code as you like
## If you are spreading this code, or hacking on it for your own use
## I would appreciate a e-mail about it, just so I know that someone
## is actually useing it for something.
##
## Usage: ./wallpaper.pl /path/to/wallpapers/if ($go eq 0) {
exit();
}## Some variable declarations
@files = ();
$dir = “”;
$dir = @ARGV[0];
$previmg = “”;
$wall = “”;## If you want a different background color, you can chage it here.
$bgcolor1 = “#000000″;
$bgcolor2 = “#000000″;## Get the current image
$previmg = `gconftool -g /desktop/gnome/background/picture_filename`;
$wall = $previmg;opendir(BIN, $dir) or die “Can’t open $dir: $!”;
while( defined ($file = readdir BIN) ) {
if ( $file eq “..”) {
} elsif ( $file eq “.”) {
} elsif ( $file eq “Thumbs.db”) {
} else {
push(@files, $file);
}
}## Find out what resolution we are running…
## Current resolution ends up in $tmpres[3]
$res = `/usr/bin/xrandr | grep ‘*’`;
@tmpres = split(/ /, $res);
@subres = split(/x/, $tmpres[3]);## As long as the selected image is the same as the previous image
## we select a new one
while ($wall eq $previmg) {
$wall = $dir . @files[&getRand];
}## Get the resolution of the image
my ($size_x, $size_y) = Image::Size::imgsize($wall);## Sets the background image
$cmd = `gconftool -s /desktop/gnome/background/picture_filename “$wall” -t string`;## If the image is the same resolution as the desktop, set it centered
## If the image is smaller than the desktop, we keep it centered
## If the image is larger than the desktop, we set it to scaled
if ($subres[0] > $size_x || $subres[1] > $size_y) {
$cent = “centered”;
}
if ($subres[0] < $size_x || $subres[1] < $size_y) {
$cent = “scaled”;
}
if ($subres[0] eq $size_x && $subres[1] eq $size_y) {
$cent = “centered”;
}## Sets scaling option and background color(s).
## Also sets ‘draw_background’ to ‘1′, incase something has turned it off.
$cmd2 = `gconftool -s /desktop/gnome/background/picture_options $cent -t string`;
$cmd3 = `gconftool -s /desktop/gnome/background/primary_color $bgcolor1 -t string`;
$cmd4 = `gconftool -s /desktop/gnome/background/secondary_color $bgcolor2 -t string`;
$cmd5 = `gconftool -s /desktop/gnome/background/draw_background 1 -t bool`;closedir(BIN);
## The function that returns a random image from the image array
sub getRand {
my $range = scalar(@files);
return int(rand($range));
}
And then I’ve just added this line in my crontab:
*/5 * * * * /home/grapz/devel/perl/wallpaper.pl /media/sda5/wallpaper/
This makes sure that this script runs every 5 minutes, every day of the week, every week of the month, every month of the year and so on.
Alex said
Your blog is interesting!
Keep up the good work!
w15p said
Hey – wanted to thank you for posting this. I took the opportunity to rewrite this in python as I had never done anything in python, but your script was exactly what I was looking for.
Thanks!
willowisp.net said
python and gconftool goodness…
After spending a bit of time getting my new work laptop (Dell D630) to work properly with twinview and the external 24″ widescreen I picked up, I realized that due to the single instance of X, gnome was screwing up the wallpaper. So I hacked up…
RobynW said
just stopped by….
Looking for something else, but fine website. All the best….
Trikke said
Heya…
Very nice site. All the best….