How can you add an item to the right-click menu in nautilus (without Nautilus-Actions)? [duplicate]

Solution 1:

In my case, I needed to make a script for Meld to compare modifications to folders/files.

I'm running on UbuntuGNOME 14.04, with gnome-shell 3.10 and Nautilus (Files) 3.10.1.

And I've done like this:

  1. Open the folder ~/.local/share/nautilus/scripts/.
  2. (Optionally) Create a sub-folder to your script, which in my case was Meld/.
  3. Create your bash, perl or python script (see the sample at the end of answer).
  4. Make your script executable (right-click at script file > properties > permissions > mark the execute option)
  5. To make sure that Nautilus will apply the changes, I've restarted Nautilus, running nautilus -q; nautilus; in a terminal.

And now, you can right-click on any folder/file to see the context menu Scripts > my-script.

Sample: scripts/Meld/Compare_with_Meld

#!/bin/bash    
meld $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS

Predefined Nautilus variables:

  • NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
    newline-delimited paths for selected files - only if local ( i.e. /home/user/someFile).
  • NAUTILUS_SCRIPT_SELECTED_URIS
    newline-delimited URIs for selected files (i.e. file:///home/user/someFile).
  • NAUTILUS_SCRIPT_CURRENT_URI
    current location
  • NAUTILUS_SCRIPT_WINDOW_GEOMETRY
    position and size of current window

Solution 2:

You could create a Nautilus Extension as described here:

http://saravananthirumuruganathan.wordpress.com/2010/08/29/extending-nautilus-context-menus-using-nautilus-actions-scripts-and-python-extensions/

Nautilus is one of the most commonly used file manager for GNOME. One of the reasons for its popularity is its extensible architecture that allows developers to write scripts to customize it. Even if you are a command line person, extending Nautilus will result in dramatic increase in productivity. In this post, I will discuss the multiple ways in which Nautilus can be extended and the relative merits in each approach.

The first step in extending Nautilus is to find the set of actions that are tedious (atleast not straightforward) – Tasks that need additional clicks or switching to terminal to get completed. The next step is to determine if there are command line tools available to automate the task or the task can be completed by additional code – Again since you are extending Nautilus, the task involved has to relate to files or folders. For eg, opening a file as administrator is a "relevant" task but starting a nuclear war from Nautilus is not !

Informally, it is easy to extend Nautilus if your task falls in the following categories : Adding new entries in context menus (or in toolbar) that involve selected files/folders or current folder, add additional custom properties to files and display those details in list view, modify the properties page to display additional tabs with information etc. There are other possibilities but these are the most common ones.

If the above discussion sounds very abstract let us give some examples :

  1. Open a terminal in the current folder or open the selected file as root.
  2. Selecting a few audio files and adding them to Rhythmbox "Now Playing" queue.
  3. Selecting a few files and sending them to thunderbird for attachment
  4. Display IMDB details about the selected movie file in the property page etc.

The above examples show a gradient of use cases in the order of complexity. Some of them are so simple that they can automated using simple means. Tasks like (4) are tricky and need powerful tools. Selecting the right tool is important and we will discuss how to select the best approach.

Different Approaches to Customize Nautilus Context Menus Like everything in Linux, there is always a variety of ways to customize Nautilus ranging from simple to complex. In this post, we will discuss the three most common approaches :

  1. Using tools like nautilus-actions
  2. Using Nautilus scripts
  3. Using extensions written in nautilus-python

As before, all my discussion will be focused on Ubuntu but it should be relatively easy to apply to other Linux distributions.

Customizing Nautilus context menu using nautilus-actions This is probably the easiest method. All you need to know is the shell command or script to perform the task. Nautilus actions provides an intuitive GUI to decide on the filters and the actions to be performed. This approach works best if the following conditions are met : a. the task you want to be automated is easily translatable in command line b. the command line utility accepts the arguments in a relatively simple form (eg space separate arguments etc) c. The command line utility depends only on information pertaining to the selected file/folder.

To install the package, type the following at terminal (or install this package from Synaptic) :

 sudo apt-get install nautilus-actions

Once the package is installed it can be accessed at System -> Preferences -> Nautilus Actions Configuration. I will only give a basic discussion here as there is a decent tutorial on how to create a new action at How To Add Custom Functionality To Nautilus.

Let us take a simple example – If I right click on a folder , I want a new menu which says, "Open Terminal Here" and when it is clicked, a new terminal must be opened and the working directory of the terminal must be the selected folder. The first step is to find if it can be expressible in a "single" command. Find the name of the command to invoke the terminal – it is called gnome-terminal. Read the man page to find that it accepts an argument "–working-dir". When provided, it starts the terminal in specified folder.

Now start the Nautilus Action from System -> Preferences -> Nautilus Actions Configuration . The steps are : a. Create an action. b. In "Action" tab, give the action some name and select "Display item in selection context menu". If you want it to be visible in the toolbar, it can done too ! Select "Display item in toolbar" and choose some icon. c. In the command tab, give "gnome-terminal" as path and parameters as "–working-directory=%d/%f". The %d and %f are special codes that will be expanded when the command is invoked. To see other special codes and what they mean, click on the "Legend" button. d. In "Conditions" tab, select "Only Folders".

Now open a new Nautilus window , select a folder and right click. Presto ! You will see a "Open Terminal Here" menu. Select it and you will see a new terminal open with the selected folder as its current directory !