GNOME/Extending Nautilus HOWTO
From openSUSE
Nautilus, the GNOME file manager, is a very powerful application that provides access to the most common file manager operations. Though, powerful (or advanced) users tend to need a terminal close to their Nautilus file manager for lots of operations that Nautilus does not offer, thus making the terminal a very much needed application.
While this is ok for hackers or power users, the rest of the world like using graphical file managers for most operations. And this is where Nautilus' extensibility features come to scene.
There are 2 ways for extending Nautilus functionality: Nautilus scripts and extensions.
Contents |
Nautilus scripts
This is the simplest way to extend Nautilus, by adding scripts to the user's ~/.gnome2/nautilus-scripts folder. Those scripts can be written in any language, even you could write a program in C or C++, compile it and copy it there. The only thing that is needed is for the script to have the executable bits on.
chmod +x my_script
All executable files in this folder will appear in the Scripts menu. Choosing a script from the menu will run that script, with the list of selected files (if run from a local folder) will be passed as arguments. For remote folders (e.g. a folder showing web or ftp content) though, scripts will be passed no parameters.
In all cases, both for local and remote folders, the following environment variables will be set by Nautilus, which the scripts may use:
- NAUTILUS_SCRIPT_SELECTED_FILE_PATHS: newline-delimited paths for selected files, only if local.
- NAUTILUS_SCRIPT_SELECTED_URIS: newline-delimited URIs for selected files. This will work for both local files, which will have file:// uris, as well as remote
- NAUTILUS_SCRIPT_CURRENT_URI: URI for current location.
- NAUTILUS_SCRIPT_WINDOW_GEOMETRY: position and size of current window.
Thus, scripts just need to use those environment variables to know which files are selected and what is the current folder. So, let's see a very basic example:
#!/bin/sh # show-nautilus-scripts-environment echo "NAUTILUS_SCRIPT_SELECTED_FILE_PATHS = $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" echo "NAUTILUS_SCRIPT_SELECTED_URIS = $NAUTILUS_SCRIPT_SELECTED_URIS" echo "NAUTILUS_SCRIPT_CURRENT_URI = $NAUTILUS_SCRIPT_CURRENT_URI" echo "NAUTILUS_SCRIPT_WINDOW_GEOMETRY = $NAUTILUS_SCRIPT_WINDOW_GEOMETRY"
Basic GUI scripts
Of course, the previous script is quite useless, mainly because if you run it from Nautilus, you won't see its output, which is sent to the standard output, as with any script on the UNIX world, and Nautilus is not capturing it. So, unless you run nautilus on a terminal, you would not see anything while running this script. This means that, if you want to show any information, or ask the user for input on your scripts, you would need to use some graphical tools. For very simple needs, zenity, a dialog-like version for GNOME, should do the trick.
# zenity --help Usage: zenity [OPTION...] Help Options: -?, --help Show help options --help-all Show all help options --help-general Show general options --help-calendar Show calendar options --help-entry Show text entry options --help-error Show error options --help-info Show info options --help-file-selection Show file selection options --help-list Show list options --help-notification Show notification icon options --help-progress Show progress options --help-question Show question options --help-warning Show warning options --help-scale Show scale options --help-text-info Show text information options --help-misc Show miscellaneous options --help-gtk Show GTK+ Options Application Options: --calendar Display calendar dialog --entry Display text entry dialog --error Display error dialog --info Display info dialog --file-selection Display file selection dialog --list Display list dialog --notification Display notification --progress Display progress indication dialog --question Display question dialog --warning Display warning dialog --scale Display scale dialog --text-info Display text information dialog --display=DISPLAY X display to use
As you can see in the help screen of zenity shown above, it provides some useful arguments to create basic dialogs for showing/asking the user for information. You can show a calendar, a text entry, error/info/warning dialogs, a file selector for the user to select a file, a list, notification balloons, progress dialogs, scale widgets and wide text entries for showing large pieces of text. So, let's do a better version of the previous script, now with a GUI via zenity:
#!/bin/sh zenity --list --text "Nautilus environment variables" --column "Variable" --column "Value" NAUTILUS_SCRIPT_SELECTED_FILE_PATHS $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS NAUTILUS_SCRIPT_SELECTED_URIS $NAUTILUS_SCRIPT_SELECTED_URIS NAUTILUS_SCRIPT_CURRENT_URI $NAUTILUS_SCRIPT_CURRENT_URI NAUTILUS_SCRIPT_WINDOW_GEOMETRY $NAUTILUS_SCRIPT_WINDOW_GEOMETRY
Just select some files in Nautilus, and run the script (right click on Nautilus, Scripts->Your Script). It will show the environment variables in a nice GTK dialog:
Zenity provides, as you can see, abilities to show somewhat complex GUIs, but it just works ok for very simple operations (show some information or prompt for it), so whenever you need something more ellaborate, you would need a better way. The following sections cover these.
Python scripts
Python is a very powerful scripting language, which, thanks to the huge set of libraries available, is a perfect option for writing all kind of scripts. One of the most important of those libraries, for our purpose of extending Nautilus, is PyGTK, the official Python bindings for GTK+, the GNOME toolkit. With these bindings, you can build any GUI you can with plain GTK+, which is just about almost any GUI you can imagine. This, combined with the huge collection of libraries for Python, provides a very powerful way of writing scripts to extend Nautilus functionality.


