
156
|
Chapter 6, GNOME Desktop Hacks
#47 Create Your Own GNOME Right-Click Actions
HACK
Checking File Types
The one time you can easily get into trouble with Nautilus scripts is when
you run a script against the wrong kind of file. It’s easy to find the type of a
file, but it’s not that easy to narrow it down to something usable in a script.
The following script has some additional code that checks to make sure the
file is an image:
#!/bin/bash
convertprg=`which convert`
while [ $# -gt 0 ]; do
picture=$1
filetype=`file $picture | cut -d ' ' -f 3`
if [ $filetype = "image" ]
then
newfile=`echo "$picture" | cut -d . -f 1`
$convertprg "$picture" "$newfile".gif
fi
shift
done
This additional code runs the file command against the target image. The
file command returns a string of text describing the type of the file. Then it
uses the
cut command to find the word “image” in the text output of file.
The word “image” should be the third field if you separate the words with
spaces. If this command returns the word “image,” the script attempts to
convert the image; otherwise, the file is skipped. The second field would
identify the type of the image (JPEG, for example), but if we used that field
we would have to compare it against a long list of image types. It is much
easier to simply identify the file as an “image.”
One final tip: the preceding script doesn’t care what kind of image it is con-
verting. That makes ...