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

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

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

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

Jono Bacon, Nicholas Petreley

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

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

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






Hack 47. Create Your Own GNOME Right-Click Actions

Create custom menu actions in the Nautilus file
manager.

GNOME and its file manager, Nautilus,
have a
custom menu feature similar to KDE/Konqueror. This hack shows you how
to exploit that Nautilus feature, add your own menu selections, and
get around some of the limitations of Nautilus.

The Nautilus approach and the KDE approach to adding custom menu
selections differ in that you can't make the
Nautilus menu selections context-sensitive so that they appear only
when they would be useful. This means you can create menu actions
that are useless in the wrong context. For example,
there's no reason why you would want to click a
spreadsheet and then select "convert to PNG
image" from a custom menu. Yet, if you create a
script that converts a file to a PNG image, that menu option (along
with all the other script menu selections you have created) will
appear even when you select files where such a conversion would be
nonsense.

The only way to make a script context-sensitive is to associate the
script with a type of file by using the GNOME File Types and Programs
applet. After that, the script will appear as one of the ways to
"Open" the file.


6.4.1. Adding a Scripts Option to the Nautilus Menu


You run custom scripts from
the
Nautilus File menu or by right-clicking and selecting the Scripts
option. You might notice your current Nautilus menu
doesn't contain a Scripts option. This simply means
you haven't defined any scripts yet. Once you define
a script, the option to run scripts will appear (along with the
option to open the scripts folder where you can add, change, delete,
or edit your scripts).

Place your scripts in the
~/.gnome2/nautilus-scripts directory. If you
find you are adding so many scripts that the menu gets confusing, you
can divide them into categories and place them into subdirectories of
~/.gnome2/nautilus-scripts. For example, you can
put all your file conversion-related scripts in the
~/.gnome2/nautilus-scripts/Convert directory.
This will place all the conversion scripts in a submenu so that you
can select ScriptsConvertYour Script to run one
of those scripts.


6.4.2. Image Conversion Script


This sample script takes an
image file and converts it to
the GIF format. It uses the convert command that
comes with the ImageMagick package. This command is capable of
converting images from just about any format to just about any other
format.

Create the
~/.gnome2/nautilus-scripts/Convert2Gif script.
Use your favorite editor to enter the following code, and then save
the file:

#!/bin/bash
convertprg=`which convert`
while [ $# -gt 0 ]; do
picture=$1
newfile=`echo "$picture" | cut -d . -f 1`
$convertprg "$picture" "$newfile".gif
shift
done

Make the script executable with this command:

$ chmod +x ~/.gnome2/nautilus-scripts/Convert2Gif

Now you're about to find out if Nautilus scripting
works in your version of GNOME. Use Nautilus to navigate to a non-GIF
image file in a folder where you have permission to create new files.
Right-click the image file and select ScriptsConvert2Gif.
If scripts work in your copy of Nautilus, you should see a new file
appear in GIF format.


6.4.3. 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 converting. That makes the script easily
adaptable to become a conversion script to any other format that the
ImageMagick convert command can handle. All you
have to change is the file extension of the target file. In short,
change .gif to .png and
then save the file as Convert2Png, and now you
have a script that converts images to PNG format. Place
Convert2Png in the scripts
directory, and off you go.


6.4.4. Environment Variables


Nautilus scripts automatically
recognize the following environment
variables which you can use in your scripts.

NAUTILUS_SCRIPT_SELECTED_FILE_PATHS


This environment variable includes the names of all the selected
files, separated by newline characters (\n). This
works only if the files are local, not if they are accessed over a
network.


NAUTILUS_SCRIPT_SELECTED_URIS


This environment variable returns newline-delimited URIs for the
selected files.


NAUTILUS_SCRIPT_CURRENT_URI


This environment variable returns the current URI, regardless of
whether it is local.


NAUTILUS_SCRIPT_WINDOW_GEOMETRY


This environment variable stores the position and size of the current
window.



If you're interested in Nautilus scripts, you can
find an excellent collection at http://g-scripts.sourceforge.net/. This site
also includes some tutorial information on scripting for Nautilus.


/ 140