October 22, 2007

Extending Nautilus context menus with Nautilus-actions

Author: Shashank Sharma

There are literally dozens of plugins and extensions for Nautilus, the default file manager on the GNOME desktop environment, but there is just one that allows you to customize the Nautilus context menu items. The Nautilus-actions extension enables you to add customized entries to the context menu such that, when you right-click a file, the context menu will show options specific to that file.

Ubuntu and Fedora users can use apt-get and yum respectively to install Nautilus-actions. The nautilus-image-converter and nautilus-open-terminal extensions we discussed recently were both created using the Nautilus-actions extension, so let's see how Nautilus-actions works.

On my Fedora 7 machine, the Nautilus Actions Configuration tool is available under System -> Preferences -> Look and Feel -> Nautilus Actions Configuration. This tool allows you to create new context menu items. To create a new action (context menu item), click the Add button. This brings up the Add a New Action dialog box. You can fill in the name of the menu item (label), a description of what it does (tooltip), and path and other parameters under the Menu Item & Action tab. The Conditions tab lists options that allow you to control the file types to which your custom action applies. Various advanced options are listed under the Advanced Conditions tab.

To begin, let's create a simple bash script that, when provided with a file, prints out every URL in that file along with five lines above and below the link in a new file. We will next create a context menu item such that right-clicking on any text file will give us an option to execute the script on the file. Here's the script:



grep -F -A5 -B5 http $FILE > ~/Desktop/linksfile.txt

I've named the script script.sh and saved it on my Desktop. This script accepts a filename, and using grep prints out every line in which http appears, along with five lines above and below it, into a new files called linksfile.txt. Next we need to create a context menu item using the Nautilus Actions Configuration tool. Click on Add in the Nautilus Action configuration window, and in the Add a New Action dialog box, fill in the field like this:

Label: Print the URLs
Tooltip: Print five lines before and after every URL
Path: /home/yourdirectory/Desktop/script.sh
Parameters: %M

The actual text you want to appear in the context menu should go into the label field. The message in tooltip text field is displayed when a user hovers the mouse over the context menu entry. You can click the Legends button for a description of all the parameters. %M gives a complete path of the selected file. It is this path that is stored in the FILE variable in our script.

Next, click on the Conditions tab. If you wish to run the script only on text files, write *.txt in the Filename field. Since the script can't operate on Folders, click on the Only Files radio button. Finally, click OK and close the Nautilus Actions Configuration tool. Now, when you right-click on text files, you'll see the Print the URLs option, and clicking on it will create a file linksfile.txt on your Desktop.

The purpose of this very mundane script is to show you what Nautilus-actions can do. There are several other configurations available on the extension's official Web site. Simply download the configuration files to your system, open the Nautilus Actions Configuration tool, and click Import/Export. In the Import/Export Settings dialog box, write the full path to where you saved the configuration file, and click on the Automatic radio button under Type of Configuration. The configuration will be added when you click OK.

Nautilus-actions's ability to execute script on files, execute programs with various command options, and operate on multiple files at once makes it exceptionally useful.

Every Monday we highlight a different extension, plugin, or add-on. Write an article of less than 1,000 words telling us about one that you use and how it makes your work easier, along with tips for getting the most out of it. If we publish it, we'll pay you $100. (Send us a query first to be sure we haven't already published a story on your chosen topic recently or have one in hand.)


  • Tools & Utilities