Monday, August 3, 2009

Enhancing contextual menus on Ubuntu Desktop

Ubuntu desktop uses Gnome and its default desktop. You can add several interesting features to your contextual menus. ( You get a contextual menu when you right click on an object.). Let us see how we can add a simple script to the contextual menu.
There is a special directory ( .gnome2/nautilus-scripts/ ) inside your home folder into which you can put your custom scripts. Any script you place in that location can be accessed by right-clicking on a file or window and selecting it from the Scripts sub menu. (The Scripts menu item will be visible only if you have some valid scripts installed.

Let us try to open a terminal from the contextual menu. Change to the scripts directory. ( Note that .gnome is a hidden directory, try control -h in nautilus to see hidden files. )

$ cd ~/.gnome2/nautilus-scripts/

Create the following script

Code:
 #!/bin/sh
gnome-terminal
Save the file as myterminal
Change the permission of the file and make it executable

$ chmod u+x myterminal

Now right click on the contextual menu. You should get it as below.





When you execute a contextual menu script , a number of environment variables are passed to the script from nautilus. For example, if you select some files and then right click and select a script , the list of selected files will be passed to the script as an environment variable . You can use this variable inside the script for further processing.

Some of the variables passed are.

NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
Newline-delimited paths for selected files if they are local
NAUTILUS_SCRIPT_SELECTED_URIS
Newline-delimited URIs for selected files
NAUTILUS_SCRIPT_CURRENT_URI
URI for current location
NAUTILUS_SCRIPT_WINDOW_GEOMETRY
Position and size of the current window

Here is a bare minimum example with which you can try out the use of environment variable.

Suppose you want to open a terminal and change to a particular directory, you can put the following code in nautilus-scripts directory. ( There is no error checking).

Code:
 #!/bin/sh

mydir=$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
if [ -d $mydir ]; then
cd $mydir
gnome-terminal
exit
fi

Now right click and select the script. Your terminal will open with the selected directory as the working directory.

There are a number of nautilus scripts available on the internet. In the ubuntu 9.04 repository, the following script collections are available.
nautilus-script-audio-convert - A nautilus audio converter script
nautilus-script-collection-svn - Nautilus subversion management scripts

You can also get lot of useful scripts from http://g-scripts.sourceforge.net/.

3 comments:

Ryan Haigh said...

Alternately you can install nautilus-open-terminal. There are also others which you might find useful including nautilus-gksu to open as administrator and nautilus-image-converter for image manipulation.

I believe these packages are based on nautilus actions which is a great alternative to nautilus scripts. With nautilus actions you can restrict which filetypes an option will be shown for and the action appears in the right click menu directly with an icon of your choosing.

Anonymous said...

What a waste of time - why not just install the nautilus-open-terminal package?

Anonymous said...

Two of my favorite nautilus scripts:

1. This script opens selected files in gedit for easy editing:

#!/bin/bash
# Description: This script opens selected files in gedit for editing
# Requires: bash, gedit, nautilus, zenity

filesall=""
while [ $# -gt 0 ]
do
files=`echo "$1" | sed 's/ /\?/g'`
filesall="$files $filesall"
shift
done
gedit $filesall&


2. This script opens the gnome search tool in the current directory:

#!/bin/sh
# Description: This script opens a gnome-search-tool in the selected directory.

cd $NAUTILUS_SCRIPT_CURRENT_URI
exec gnome-search-tool