Backdrop in Python

#!/usr/bin/python

import os, sys, random, imghdr

# This is a little program I call via cron to change my desktop every
# few minutes.  With no arguments it goes to my directory of backdrop
# images and picks a valid image at random.  If I specify a path and a
# file the program will put it up as the display.

# I don't want to fill up my inbox with emails from cron telling me that
# X isn't running, so I check first.
xisrunning = os.popen("pidof /usr/bin/X11/X").read()

def changebackdrop():
    # The below command works for transparent Eterm or Urxvt terminals,
    # populating their backgrounds with the image they occlude.  xli or
    # xsetroot can be called, but they don't work as desired for
    # transparent terminals.
    command = "/usr/bin/Esetroot"
    # If I was logging into X remotely, this would change.
    commandargs = " -display :0.0 "

    # This is where my backdrops live
    picdir = "/home/willyyam/misc/bmps/"

    if sys.argv[1:]:
        doit = command + commandargs + sys.argv[1]
        os.popen(doit, 'r')
    else:
        files = os.listdir(picdir)
        os.chdir(picdir)
        pics = []
        for file in files:
            # This is a test for valid images - it includes rgb files,
            # which are not supported by my image software, but the
            # error thrown is not terrible - it knows what it can and
            # cannot run.
            if imghdr.what(file):
                pics.append(file)

        randpic = random.choice(pics)
        doit = command + commandargs + picdir + randpic
        os.popen(doit, 'r')

if xisrunning:
    changebackdrop()
else:
    exit

# Copyright 2005 William Witteman

Randomized Backdrops 2.0

#!/usr/bin/perl -w

# This little program sets my background image
# randomly in blackbox (and now openbox).
# It's called when I startx.

$isxrunning = `ps -C startx | grep "startx"`;

if ($isxrunning =~ "startx") {
    changebackdrop();
} else {
    exit;
}

sub changebackdrop {
    if (!defined $ARGV[0]) {
        $backdrop_directory = "/home/willyyam/misc/bmps/";
        # Set this to your backdrop files directory.

        opendir(BD, "$backdrop_directory") or die;
        # Normally we'd report errors, but since we will be
        # running this via script and discarding the STDOUT
        # that it generates, that would be a waste of time.

        @backdrops = readdir(BD);
        # Grab the contents of the backdrop directory.

        @spordkcab = reverse(@backdrops);
        pop(@spordkcab);
        pop(@spordkcab);
        # The first two entries readdir() finds are
        # . and  ..  We don't want to hand these
        # entries to bsetbg, so I reverse the array
        # and do away with them.  There is a smooth,
        # tidy way to do this.  I don't care :-)

        $randpic = $spordkcab[ rand @spordkcab];
        $randpic = $backdrop_directory . $randpic;
        `/usr/bin/Esetroot -display :0.0 $randpic`;
        #`/usr/bin/X11/xli -display :0.0 -onroot $randpic > /dev/null &`;

        exit;
    } else {
        # If backdrop is called with an argument, use the argument
        # as the backdrop.
        `/usr/bin/Esetroot -display :0.0 $ARGV[0]`;
        #`/usr/bin/X11/xli -display :0.0 -onroot $ARGV[0] > /dev/null &`;
        exit;
    }
}

# William O'Higgins yam@nerd.cx

Randomized Backdrops

#!/usr/bin/perl -w

# This little program sets my background image
# randomly in blackbox (and now openbox).
# It's called when I startx.

$backdrop_directory = "/home/willyyam/crap/backdrop/";
# Set this to your backdrop files directory.

opendir(BD, "$backdrop_directory") or die;
# Normally we'd report errors, but since we will be
# running this via script and discarding the STDOUT
# that it generates, that would be a waste of time.

@backdrops = readdir(BD);
# Grab the contents of the backdrop directory.

@spordkcab = reverse(@backdrops);
pop(@spordkcab);
pop(@spordkcab);
# The first two entries readdir() finds are
# . and  ..  We don't want to hand these
# entries to bsetbg, so I reverse the array
# and do away with them.  There is a smooth,
# tidy way to do this.  I don't care :-)

$pic_o_the_moment = $spordkcab[ rand @spordkcab];
$pic_o_the_moment = $backdrop_directory . $pic_o_the_moment;
`/home/willyyam/bin/bsetbg $pic_o_the_moment &`;

# This the program call to bbsetbg, written by
# tmk@lordzork.com, is what does all the work --
# I just call it.

exit;

# William O'Higgins yam@nerd.cx