| Charles Curley - Software Engineer, Writer
| << | < | > | >> | Blog | Linked In Profile
+ Larger Font | - Smaller Font
Charles Curley

Valid XHTML 1.0! Valid CSS!


Wallpaper2

This script is a follow-on to one I described in an article on the Linux Journal web site, Slide Show, "Some background information for Linux desktops." The article shows how to create a slide show in the root window of your desktop.

Update: Five years later, the Gnome folks finally caught up with me.

The first version, shown in the articles, lets the user specify a regex for the size. This version allows the user to specify a minimum height and a minimum width, so that any image with height and width greater than the minima is selected. This version does arithmetic greater than or equal comparisons. So you get different shaped pictures more easily, and therefore more pictures are likely to be selected.

In addition, this version lets you count the eligible images in the path with the -c option. If you make a verbose count (-cv), the script will print out a randomized list of eligible images as well as the count.

This version gets into some pretty hairy string manipulations in BASH. Have a BASH manual handy when you read the code that extracts and analyzes the height and width, like Chapter 9 of the Advanced Bash-Scripting Guide. It was fun writing that!

#! /bin/bash
# Time-stamp: <2009-06-25 14:55:57 ccurley Wallpaper2>

# A script to run images in the root window.

# Copyright 2003 through the last date of modification Charles Curley.

# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

# You can also contact the Free Software Foundation at http://www.fsf.org/

# For more information contact the author, Charles Curley, at
# http://www.charlescurley.com/.

oops () {
  echo "$0: a script to run images in the root window."
  echo "options: -v: verbose; -w: min. width; -h: min. height; -c: count only;"
  echo " -d: duration of each image; -e: extension, e.g. 'jpg'."
  echo " -o: option for image display."
  echo " -k: desktop to use, e.g. 0. Multiple paths accepted."
}


# Set the way we handle the background, one of $validoptions. For
# information on what these do, see "1.2. Customizing the Desktop
# Background" under "Using the Basic Preference Tools" in the Gnome
# help.

picture_option="scaled"

validoptions="|centered|scaled|stretched|zoom|wallpaper|"

# Extension to find
ext="jpg";

# -iq: case insensitive, silently
# grep -iq gnome ~/.Xclients-default
# desktop=$?
desktop=0

# How long we wait between pictures.
duration=600

# The minimum height picture we want. '' indicates all sizes
height=600

# The minimum width
width=800

# Shall we be verbose?
verbosity=0

# Shall we just count target images?
count=0

while getopts "cd:e:h:k:o:vw:" Option
do
  case $Option in
    c     ) count=1;;
    d     ) duration=$OPTARG;;
    e     ) ext=$OPTARG;;
    h     ) height=$OPTARG;;
    k     ) desktop=$OPTARG;;
    o     ) picture_option=$OPTARG;;
    v     ) verbosity=1;;
    w     ) width=$OPTARG;;
    *     ) oops; exit;;   # DEFAULT
  esac
done

shift $(($OPTIND-1))

if [ -z "$*" ]; then
  oops; exit;
fi

imagepath=$*;

# This tests for the desktop we're running. It works on Red Hat or
# other systems where switchdesk and its bretheren are the standard
# way of changing the desktop. If ~/.Xclients-default isn't there, run
# "switchedesk X" where X is your desktop: gnome, KDE, TWM, etc. or
# run the GUI front end for your desktop. Or just hard code it.

# Gnome users may also take Nautilus out of their gnome session:

# gnome-session-remove nautilus

# To get it back, just run:

# nautilus &

soption='|'$picture_option'|'
# echo $soption

echo $validoptions | grep -i "$soption" > /dev/null
results=$?
# echo results: $results
if [ $results -ne 0 ] ; then
  echo \"$picture_option\" is an invalid option. Use one of:
  echo $validoptions
  oops; exit
fi

if [ $verbosity -gt 0 ]; then
  echo imagepath = $imagepath. duration = $duration.
  echo picture_option = $picture_option. height = $height. width = $width.
  echo validoptions = $validoptions.
fi

# Build an array called $pictures of random pictures to display.

if [ $width -eq 0 ] || [ $height -eq 0 ] ; then
    echo 'No size limits.'
    pictures=( $(find $imagepath -type f -iname "*.${ext}" | randomize) )
else
    rawpictures=( $(find $imagepath -type f -iname "*.${ext}" |  xargs identify -format "%d/%f^%w^%h\n" | randomize ) )
    if [ $verbosity -gt 0 ]; then
        echo "Image size set."
        echo rawpictures has ${#rawpictures[@]} elements
        echo rawpictures = $rawpictures
        for index in ${!rawpictures[*]}; do
            printf '%4d: %s \n' $index ${rawpictures[$index]}
        done
    fi
    for i in "${rawpictures[@]}"
    do
#         echo $i;
        p=${i%%^*};             # Get the path
        w=${i#*^};              # Get the width+height
        w=${w%%^*};             # Get the width
        h=${i##*^};             # Get the height
#         echo "$p    $h     $w"

        # If either height or width is greater, allow it.

        if [ $h -ge $height ] || [ $w -ge $width ]; then
#             echo $p
            pictures[$j]=$p;
            let j=$j+1;
        fi
    done

    # kill of the old array to conserve memory.

    unset rawpictures;
fi

# echo ${#pictures[@]}

# If there are any pictures to show, we show them.
if [ ${#pictures[@]} -gt 0 ]; then

    if [ $count -gt 0 ]; then
        echo "${#pictures[@]} images found."

        if [ $verbosity -gt 0 ]; then
            for ele in $(seq 0 $((${#pictures[@]} - 1))); do
                printf '%4d: %s \n' $ele ${pictures[$ele]}
            done
        fi

    else

        if [ $verbosity -gt 0 ]; then
            echo "I will show ${#pictures[@]} images $duration apart (default is seconds unless specified)."
        fi

        if [ $desktop == 0 ]; then
            if [ $verbosity -gt 0 ]; then
                echo We\'re running Gnome;
            fi

            # If there are any file names in the array
            for ele in $(seq 0 $((${#pictures[@]} - 1))); do
                if [ $verbosity -gt 0 ]; then
                    printf "%4d: %s\n" $ele ${pictures[$ele]}
                fi
                gconftool-2 -t str -s /desktop/gnome/background/picture_filename "${pictures[$ele]}"
                gconftool-2 -t str -s /desktop/gnome/background/picture_options "$picture_option"
                sleep $duration
            done

        else
            if [ $verbosity -gt 0 ]; then
                echo We\'re not running Gnome
            fi

            echo ${pictures[@]} | xargs xv  -root -wait $duration -rmode 4

        fi
    fi
else
    echo "No pictures found."
fi
 

randomize

You will need this script, randomize as well.

#! /usr/bin/perl

# Time-stamp: <2003-11-21 15:11:05 root randomize>

# randomize: a perl script to take stdin input, randomize it, and ship
# it out stdout. Suitable for shell scripts. Auto-detect and handle 0
# delimited arguments.

# Copyright 2003 through the last date of modification Charles Curley
# except for the subroutine fisher_yates_shuffle. fisher_yates_shuffle
# Copyright (c) 1998 Tom Christiansen, Nathan Torkington and O'Reilly
# & Associates, Inc.

# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

# You can also contact the Free Software Foundation at http://www.fsf.org/

# For more information contact the author, Charles Curley, at
# http://www.charlescurley.com/.

# I wrote this so I could use ImageMagick's display program to set my
# root window every so often with one of several images, chosen at
# random. The key line of the script looks like this:

# find <path> -iname "*.jpg" -print0 | randomize | xargs -0 display -delay 60000 -window root

# (Try _that_ with that cheap operating system! :-)

# fisher_yates_shuffle ( \@array ) : generate a random
# permutation. From Perl Cookbook, Christiansen & Torkington,
# O'Reilly, 1999, page 122.

sub fisher_yates_shuffle {
    my $array = shift;
    my $i;
    for ($i = @$array; --$i; ) {
    my $j = int rand ($i+1);
    next if $i == $j;
    @$array[$i,$j] = @$array[$j,$i];
    }
}

while (<STDIN>) {
    chomp;
    push (@array, $_);
}


# If the size of the array is one, then we have one line, presumably 0
# delimited. ($#array returns the last valid index, not the size. perl
# being 0 based, the size is one larger.) You produce this with find
# by using the -print0 option. It's good for file and directory names
# with (ugh) spaces in them.

if ($#array == 0) {
    $string = @array[0];
    @array = split (/\0/,$string);
    $zeros = 1;
}


fisher_yates_shuffle (\@array);

# If the input was zero-delimited, the output will be also. Use "xargs
# -0" to further process the results.

if ($zeros) {
    $string = join ("\0", @array);
    print "$string\n";
} else {
    foreach $item (@array) {
    print "$item\n";
    }
}

Copyright © 1996 through 2010 by Charles Curley
Last Modified: 29 Jul, 2010
100% Microsoft-free web site.