Customize the Right-Click Contextual Menu




Customize the Right-Click Contextual Menu

Write your own scripts to perform custom actions when you right-click on a file, folder, or the desktop, and add templates to make document creation quick and painless.

Right-clicking on items on the desktop or in the Nautilus file browser pulls up a contextual menu that allows you to perform operations directly on the item. But you're not limited to just the default options: you can add template documents and scripts to the menu for easy access with a single click.

Easy-Access Templates

Right-clicking on the desktop or in the background of a Nautilus window brings up a Create Document menu item, which normally includes only an "Empty File" item. If you select Empty File, a new file called new file will be created, and you can rename it to anything you like. However, the new file is just a totally empty file. Creating a document this way is essentially the same as running:

$ touch "new file"
            

and about as useful.

However, it's easy to add your own templates to the menu. Create a directory called Templates in your home directory:

$ mkdir ~/Templates
            

Any document you place in that directory will now be available through the Create Document contextual menu. If the menu becomes large, you can group items into submenus by placing them in subdirectories within Templates.

HTML developers can put a file called HTML File.html in this directory (it will appear as "HTML File" on the Create Document menu) and fill it with the skeleton of an HTML file. If you create a lot of corporate documents using OpenOffice.org templates, copy the templates in, and you'll be able to start a new document in any location by just right-clicking and selecting the template.

If you do not see your new templates appear in the menu right away, just log out of GNOME and log back in again.

Custom Scripts

You can also execute custom scripts directly from the contextual menu by placing your scripts into a special directory located inside your home directory: .gnome2/nautilus-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 submenu. (The Scripts menu will not be available unless you have some scripts installed.)

When a script is executed via the contextual menu, it is passed a number of environment variables and usually a number of arguments so it can optionally act on the files you have selected. If you execute the script from the context of a local folder on your computer, it will be passed the names of all selected files as arguments. If you execute the script from the context of a remote folder [Hack #20], such as a Nautilus window showing web or FTP content, no arguments will be passed to it.

Four environment variables are also set, which you can access from within the script:


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

There are even packages for various prewritten script collections, such as the Nautilus Subversion Management Scripts and Nautilus Audio Convert packages, which allow you to perform Subversion actions and convert audio file formats by right-clicking on files:

$ sudo apt-get install nautilus-script-collection-svn \\
                 nautilus-script-audio-convert
            

Not seeing the Scripts menu when you right-click? That may be because Nautilus doesn't think you have any scripts. To give it a heads-up, select GoAs a simple example, you could place the following into ~/.gnome2/nautilus-scripts/Terminal to give you easy access to a terminal from the contextual menu:

#!/bin/sh
gnome-terminal

This will open a terminal with the current directory set to the directory that encloses whatever you right-clicked on. So, if you right-clicked on the icon for ~/foo, you'd get a terminal whose current working directory was ~. But suppose you right-click on a directory. The following Terminal script will check each item in NAUTILUS_SCRIPT_SELECTED_FILE_PATHS, and if it finds a directory, it will cd to it and open the terminal there. Otherwise, it will just open the terminal in the directory that contains the item you clicked on:

#!/bin/sh
for d in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS; do
 if [ -d $d ]; then
    cd $d
    gnome-terminal
    exit
  fi
done
gnome-terminal

More complex uses could be to encrypt a selected file using GPG, set a selected image as the desktop background, or send a selected file as an email attachment. For a collection of a variety of scripts specifically designed for use in the contextual menu, visit http://g-scripts.sourceforge.net.