Linux.Desktop.Hacks [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Linux.Desktop.Hacks [Electronic resources] - نسخه متنی

Jono Bacon, Nicholas Petreley

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید






Hack 45. Randomize Your GNOME Wallpaper

Like many Linux enthusiasts, you spend hours each day staring at your
computer screen. You're lucky to have nice graphical
interfaces sprinkled with pretty pictures to make all the work you do
seem more interesting, but after a time, things can still get dull.
To spice up your daily working grind, it would be nice to have the
GNOME desktop periodically change to a random image. With a
collection of suitable wallpaper this could add a little more sparkle
to your desktop.

Unfortunately, a default option for performing this kind of
randomized wallpaper adjustment does not exist. Therefore, you will
write a small script in this hack to accomplish this. After a little
time preparing the script, not only will you have a randomized
wallpaper set up, but also you will have a deeper understanding of
how GNOME can be scripted.


6.2.1. Selecting a Random Image


With a small bash script, it is possible to randomly select an image
from a directory and change the current GNOME wallpaper to that
image. It's easy to forget just how powerful bash
can be; more than just a simple command shell,
bash has a whole host of features that make it
well-suited for even complex programming tasks.

To begin this hack, you need to have a directory full of wallpapers
somewhere. Assume this directory is located at
/home/foo/Images/Wallpapers/. This script will
take an image from that directory and set it as the current
wallpaper. Here's the first part of the script:

#!/bin/bash
export DIR='/home/foo/Images/Wallpapers/'
export NUMBER=$RANDOM
export TOTAL=0

The first line is a standard piece of code that says which program
should be used to run the script (in this case
bash). After this line is the location of the
directory containing your images, stored in the
$DIR variable for future use. Next, you store a
random number, which is generated by the built-in variable named
$RANDOM. You also set $TOTAL to
0 to begin with; this variable stores the total
number of images in the directory.


If you get an error when running this script that reports a
"Bad Interpreter" or something
similar, you should check the documentation for your distribution and
make sure the path to the bash binary is correct
(/bin/bash should be correct on most
distributions).

After this initial code, you need to create a loop that counts the
number of images in the directory using the output of the
ls command. Because this script
doesn't check file types, it is important that you
store only images in this directory. Here is the loop:

for f in `ls $DIR`
do
let "TOTAL += 1"
done
let "NUMBER %= TOTAL"

The line let "NUMBER %= TOTAL"
is the part that actually selects which image will be used. This line
divides the randomly generated number by the number of images in the
directory and stores the remainder of this division in the
$NUMBER variable. If you're
wondering how this works, the remainder must be between 0 and 1,
minus the number of images, and because in the next part the script
starts counting from 0, it is possible for any image to be selected
with this method.

The final part of the script simply counts through each image to see
if it is the one that was selected. When it finds the correct image,
it modifies the GConf setting that stores the
filename of the wallpaper using the gconftool
command (this might be gconftool-2 on some
systems). Nautilus notices this change immediately and updates the
wallpaper. So, here's the final script:

#!/bin/bash
export DIR='/home/adam/Images/Wallpapers/'
export NUMBER=$RANDOM
export TOTAL=0
for f in `ls $DIR`
do
let "TOTAL += 1"
done
let "NUMBER %= TOTAL"
export CURRENT=0
for f in `ls $DIR`
do
if [ $CURRENT = $NUMBER ]
then
/usr/bin/gconftool-2 -t string -s /desktop/gnome/background/picture_filename
$DIR/$f
break
fi
let "CURRENT += 1"
done

Save the script somewhere convenient such as
/home/foo/setbg.sh. Also make it executable by
running the following in a terminal:

foo@bar:~$  chmod +x setbg.sh

Now when you run this script your wallpaper will be changed.


6.2.2. Automating the Task


Using cron you can run this script
automatically at set times.
[Hack #70] discusses how to use
cron. Another option is to use
GNOME's session manager to have the script run when
you log in. You can even add a launcher to your panel so that you can
change the wallpaper with one click. Just enter
/home/foo/setbg.sh as the program name.

Adam McMaster


/ 140