Monday, October 27, 2008

Switching backgrounds in gnome

Surprisingly, gnome doesn't have a built in background switcher to change the background every N minutes. I found some on the 'net but didn't like any i found, so wrote my own as i'm want to do.

I have it start every time i log in by going to System -> Preferences -> Personal -> Sessions (Add):



#! /bin/bash

# System -> Preferences -> Personal -> Sessions

function dohelp() {
echo "$0 has three optional arguments."
echo "'-d dirname' to specify the directory to examine (~/Backgrounds"
echo " by default)."
echo "'-s sleep' to specify the number of seconds to sleep between"
echo " changing backgrounds (1800 (half an hour) by default)."
echo "'-m method' to specify the method of displaying the images."
echo " Values are:"
echo " 'zoom' to fill the screen and chop off any extra from the two"
echo " edges that overflow. Aspect ratio is maintained. This is"
echo " the default."
echo " 'scaled' to fit the image to two edges and fill with the"
echo " background color on the two edges that don't reach."
echo " Aspect ratio is maintained."
echo " 'streched' to fit the image to all four edges while not"
echo " maintaining the aspect ratio."
echo " 'centered' to place on the screen with no scaling at all"
echo " 'wallpaper' to repeat the image as many times as it will fit"
echo " with no scaling."
echo " One can also have picture-specific methods by creating a file"
echo " prefixed by a dot ".DSC0001.jpg for example" that has one word"
echo " in it, either zoom, scaled, streched, centered, or wallpaper."
echo " If none is specified, the default is used."
exit
}

DIRNAME=~/Backgrounds
SLEEP=1800
METHOD=zoom

while getopts 'd:s:m:h' OPTION
do
case $OPTION in
d) DIRNAME="$OPTARG" ;;
s) SLEEP="$OPTARG" ;;
s) METHOD="$OPTARG" ;;
h) dohelp ;;
?) echo "Usage: [-d directory] [-s sleep] [-m method]" >&2
exit 2
;;
esac
done

FILENAME=/desktop/gnome/background/picture_filename
OPTIONS=/desktop/gnome/background/picture_options

method=$METHOD
gconftool-2 --type string --set $OPTIONS $method

while true
do
# (re)read directory to pick up any new files
for i in $DIRNAME/*
do
filename=${i#$DIRNAME/}
# echo $filename

methodfile=$DIRNAME/.$filename
if [[ -e $methodfile ]]
then
# echo $methodfile exists
newmethod=`cat $methodfile`
else
newmethod=$METHOD
fi

if [[ $newmethod != $method ]]
then
gconftool-2 --type string --set $OPTIONS $newmethod
method=$newmethod
fi

gconftool-2 --type string --set $FILENAME $i

sleep $SLEEP

done
done

YAF with FC8

Yet another frustration with Fedora Core 8.

So, i want to sync with the local calendar server via evolution-jescs. Not in the yum repositories, naturally. Grabbed the latest source, 2.24. Needed to install intltool (easy) and libsoup 2.4 (impossible as it needs a newer version glib). 2.23, same deal. Etc. Had to go back to 2.12 to get it compiled. And that didn't work in the end either.

Ubuntu you ask? Installed via synaptics and worked the first time.

You connect the dots.

Image previews in nautilus

I wasn't getting previews for images on a compact flash drive attached via a USB connector in nautilus. Turns out, nautilus seems to believe that isn't a "local" drive, so under Preferences, Preview, Show Thumbnails, one has to drop down to "Always". Also see http://ubuntuforums.org/showthread.php?t=244898 for how to get around the error "The filename "..." indicates that this file is of type "jpg document". The contents of the file indicate that the file is of type "JPEG image""

Tuesday, October 21, 2008

Formatting arrays in Ruby

So i've been playing around some more with Ruby. The default formatting of arrays leaves something to be desired in my opinion. Here's what i use instead:

class Array
def to_s
'[' + collect{|x| x.to_s}.join(', ') + ']'
end
end

Saturday, October 18, 2008

Easy ginger tea

1.5 cup water, 1 teaspoon ground ginger. Microwave for 2 minutes. Add 1-2 teaspoon honey. Strain. Drink.

Sunday, October 5, 2008

Some PHP practice problems

Given:

<?php
echo "Please enter your name: ";
$name = trim (fgets (STDIN));
echo "Welcome, $name\n";

if ($name == "Sushil")
echo "You rock!!!\n";

print "I'm now going to count to 10\n";

for ($i=1; $i <=10; $i++)
echo $i . " ";

echo "\n";
?>


extend to the following:
  1. read three numbers and print the square of each,
  2. read three numbers, sort them, and print the square of each,
  3. print the even numbers from 0 to 20, and
  4. print the current date.
A useful resource is the php tutorial.

launch4l

I wanted to create a self-launching UNIX jar file, similar to what launch4j does for Windows. There are a couple of solutions out there, but none worked well for me. So I wrote my own. Here it is (formatted via http://formatmysourcecode.blogspot.com/):

#! /bin/bash

path=$1
jarname=${path##.*/}
basename=${jarname%%.jar}

## want substitutions this time as we need jarname expanded
cat - << ONE > $basename
#! /bin/bash

trap 'rm -f /tmp/$jarname' ERR
rm -f /tmp/$jarname
ONE

## but not here
cat - << "TWO" >> $basename
UUDECODE=`which uudecode 2> /dev/null`
if [ "$UUDECODE" = "" ]
then
UUDECODE=`which gmime-uudecode 2> /dev/null`
if [ "$UUDECODE" = "" ]
then
echo "No uudecode command found in $PATH, exiting"
exit 1
fi
fi

TWO

## but here again
echo '$UUDECODE -o /tmp/'$jarname' << "ENDOFJAR"' >> $basename

UUENCODE=`which uuencode 2> /dev/null`
if [ "$UUENCODE" = "" ]
then
UUENCODE=`which gmime-uuencode 2> /dev/null`
if [ "$UUENCODE" = "" ]
then
echo "No uuencode command found in $PATH, exiting"
exit 1
fi
fi

$UUENCODE $path /tmp/$jarname >> $basename

cat - << THREE >> $basename
ENDOFJAR

java -jar /tmp/$jarname \$*
rm -f /tmp/$jarname
THREE

chmod u+x $basename