Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

statico (5018)

statico
  ian.langworthNO@SPAMgmail.com
http://langworth.com/
AOL IM: eisforian (Add Buddy, Send Message)

PAUSE-ID: IAN [cpan.org]

Co-author of Perl Testing: A Developer's Notebook [oreilly.com]

Journal of statico (5018)

Saturday December 31, 2005
09:50 PM

switching backgrounds in Gnome 2

[ #28191 ]

One reason to use open-source software is if you enjoy the ability to create or alter any of the tools you use. I use Linux and the Gnome desktop environment, and I'm confident that if there's something that I want to do -- a customization or an application -- I can probably do it in Perl. If I can't, I can probably do it in Ruby. If not Ruby, there might be command-line tools. And if none of those are available, I can do it in C.

Every couple of minutes, the background on my dad's laptop changes to a random picture. The picture is one of many in a directory of Eleuthera images, some of which are mine. This sort of behavior doesn't exist for Gnome, plus I'm not always online with the ability to search for a solution, so I was curious as to how much work would be needed to this happen.

I fired up gconf-editor, the Gnome configuration editor, and searched for "background." Sure enough, there was a preference called /desktop/gnome/background/picture_filename whose value was an absolute path to the current background image. There were also other options to set the layout and background color.

Having the Gnome2 packages installed, I tried using Gnome2::Config to set the path to the background image:

#!/usr/bin/env perl
use strict;
use warnings;

use Gnome2;
Gnome2::Program->init( 'random-background', 0.01 );

my $path = '/desktop/gnome/background/picture_filename';
Gnome2::Config->set_string( $path,
    '/home/ian/pictures/to_upload/IMG_3644.JPG' );

This, unfortunately, didn't work at all. Before set_string(), the string returned by get_path() was empty. Afterwards, tests using get_string() claimed that the image had been set, but the background hadn't changed. Harumph.

Next I tried to use Ruby. Unfortunately, the Gnome libraries for Ruby weren't installed, so I looked for a command-line solution instead.

The gconftool-2 utility, which allows you set and get parameters on the command-line, worked perfectly. The following program takes one or more directories as arguments, finds all .jpg files in those directories, and picks one randomly and sets it as the background:

#!/usr/bin/env perl
use strict;
use warnings;

use IO::All;

die "usage: $0 <directory> ...\n" unless @ARGV;

my @pictures = grep /\.jpg$/i, map { io($_)->deep->all } @ARGV;
my $picture  = $pictures[ rand @pictures ];

my @options_to_set = (
    [ 'picture_options',  'scaled' ],
    [ 'picture_filename', $picture->absolute->pathname ],
    [ 'primary_color',    '#444444' ],
);

foreach my $tuple (@options_to_set) {
    my ( $key, $value ) = @$tuple;
    system(
        'gconftool-2',
        -t => 'string',
        -s => "/desktop/gnome/background/$key",
        $value
    );
}

This doesn't, however, perform the sexy fading transitions that Mac OS X does. I figure it's close enough for now.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.