A nautilus script is just an executable shell script (usually bash) that
is placed in a special scripts directory so that the Nautilus
graphical shell can find it. This is a really neat function of
Nautilus, because it allows you to extend the functionality the the
file browser to do just about anything.
Scripts are invoked by selecting a file or group of files, and right-clicking
with the mouse, to bring up a 'Context' menu (see picture above.) One of the options of this menu
is the 'Scripts' submenu, which allows you to select a script to invoke on
the selected files.
For a script to appear on the script menu, it must: (1) be placed in your
scripts directory, and (2) be executable. If you place an executable
script is in your scripts directory, its name will not necessarily
appear on the scripts menu immediately. You first must visit the scripts
directory with Nautilus (which can be done using the last option in the
scripts menu.) Once the directory is visited, Nautilus will know about which
scripts you have, and you will be able to use them. By the way, there is
a handy script above (make-nautilus-script)
that will copy a file to the Nautilus subdirectory and make it
executable for you.
Prior to Nautilus release 1.0.5, the scripts directory was
~/Nautilus/scripts. Since 1.0.5, it is ~.gnome/nautilus-scripts,
although there was some confusion about the scripts directory being in
~/.nautilus/scripts.
As of Nautilus version 1.0.5, the scripting menu of Nautilus only
recognizes a hierarchical scripts subdirectory. By making
subdirectories in your scripts folder, you can arrange your scripts so
that they can be accessed more easily, based on what you want to do.
Unfortunately, at least for me, there is a bug so that items
inside the subdirectories don't always show up.
I'm not really very good at this, but I can give you a couple pointers.
First, you should learn a scripting language. The easiest way to make simple
scripts is via bash scripts, but for more complicated tasks,
perl,
python,
csl,
GnomeBasic
, etc. can be useful. You can find resources for programming Bash scripts
here,
here, and
here.
Basically, a Bash script is just a bunch of Bash commands strung together
in a text file. The file should be executable (e.g., with the command chmod
u+x [filename]), and the first line should be a special comment of the form #!/bin/sh, which tells the kernel
what program to use to execute the rest of the script.
There are a couple of other useful things to know. When a script is invoked,
Nautilus sets a few environment variables that can be used by the script:
- NAUTILUS_SCRIPT_SELECTED_FILE_PATHS:
- newline-delimited paths for selected files (only if local)
- NAUTILUS_SCRIPT_SELECTED_URIS:
- newline-delimited URIs for selected files
- NAUTILUS_SCRIPT_CURRENT_URI:
- current location
- NAUTILUS_SCRIPT_WINDOW_GEOMETRY
- position and size of current window
The selected files can also be referred to with the more traditional "$@"
and "$*" bash variables.
The zenity application, available in Zenity, is a simple dialog box you can invoke from a shell script to give the users information or get information from the user. Currently, it seems like it needs a little work, but hopefully it will get more usable in the future.
The biggest problem is to be able to handle multiple files with spaces.
Many (but not all) of the scripts here are designed to be able to do this,
but they require a little thought to construct correctly. Most of the scripts
in the "File System Management" (cf. uppercase, lowercase, make-nautilus-script)
section are robust for this. If you are not careful, your script will be
unable to tell whether a filename with a space is a single file or multiple
files. One way is to simply protecting the $@ variable with "$@". Another trick to process multiple files with spaces to use something like this:
#!/bin/sh
for arg
do
echo "$arg" #"$arg" must be protected with quotes
done
This can make it tricky to invoke a single command on all the files at
once, and so you may need to iteratively build the commands arguments with
a temporary variable. Another solution is to process the input filenames with sed,
like this:
files=`echo "$1" | sed 's/ /\\ /g'`
Some people have found that the above do not work well in
all cases. Paolo Bacch suggested the following:
#!/bin/sh
quoted=$(echo -e "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | awk 'BEGIN {
FS = "\n" } { printf "\"%s\" ", $1 }' | sed -e s#\"\"##)
eval "your-program $quoted"
If you useful scripts that you would like to see here,
please send them to g-scripts-devel@lists.sourceforge.net
(archives).
This mailing list does not archive attachments, so if you
want your script to be archived, send it in-line.
If the script
is simple (a one-liner), I would suggest you submit it
license-free (public domain), because the the licensing
material required could make the file hundreds of times
larger than the content, and it may make it more difficult
for me to make fixes others might suggest, because I am not
a lawyer and I don't understand licenses very well. Authors
of scripts are acknowledged when available, and I have
denoted those scripts licensed under the GPL with a *.
The unix utility file [filename] can be used to determine what
the input filetype is. The scripts filetype and mimetype
provide simple examples of how to use this, and pprint provides a more complicated
example.
If you want to make Nautilus file-type
sensitive, so that a script only will be offered when when a
specific type of file is selected, you can add it to your
"Open" context menu via the control center's "File Types and
Programs" applet.
One way to use these scripts is by creating panel
launchers or .desktop files (either in Gnome or in another
desktop environment), enabling the script to be launched when another
file is dragged onto the launcher. For example, these
pictures show me dragging files to launchers set up on my
menu panel and with a .desktop launcher (the cursors don't
appear in the screenshots.)

However, these launchers will do not call scripts with the
same arguments that nautilus does, and they will not set up
some of the environment variables that nautilus does. So,
you should call a modified version of the
pseudo-nautilus
script available above (modified so that the pseudo-nautilus
script runs the script of your choice.) This script will
process you commands and call a script in the same way
Nautilus will.
The easiest way is to right click on a panel and choose
"Panel | Add to Panel | Launcher…", and used the dialog to
specify the script and icon you want to use. You should also
create the appropriate pseudo-nautilus script, as described
above. Alternately, you can write your own. The desktop
entry standard (found here)
can be used as a reference for creating .desktop launchers,
but be warned--Gnome 1.4 does not implement all
functionality described in this spec. For example, you
don't need to use the '%f' code to pass command-line-like
arguments. A simple example .desktop file might look like
this:
[Desktop Entry]
Type=Application
Name=Junk Sorter
Exec=/home/me/.panel-scripts/junksorter
Icon=/usr/share/pixmaps/gnome-ftp.png
This file is named junksorter.desktop,
~/.panel-scripts/junksorter is a pseudo-nautilus script that
launches the
junksorter script found above.