Friday, October 19, 2012

Adding "Copy as Path" menu item to Nautilus

I use both Windows and Ubuntu Linux. One of the features I miss in Nautilus file manager under Ubuntu is the "Copy as Path" menu item which is available in Windows explorer context menu by holding the Shift key while right-clicking on a file or folder.
I tweaked Nautilus to do have "Copy as Path":

This required two steps:

First step:
A simple Python script to copy the path to clipboard:

import gtk
import sys

clipboard = gtk.clipboard_get()
text = sys.argv[1]

This script uses gtk Clipboard to copy the path of the file, which is passed as the second parameter, the first is the script itself.
Save the script to a .py file and then move to the:

Second step:
Use Nautilus-Actions to add the menu item that will invoke the script.
It can be installed from Ubuntu Software Center or using terminal:

sudo apt-get install nautilus-actions

Open Nautilus Actions and Add a new action:

In the Command tab, enter python for the Path, and "/path_to_script/ %f" in the Parameters text box (replace with your own values, where you saved the script).
Note that the %f parameter means the selected file or folder path.

By default, Nautilus Actions creates a sub menu, and adds the custom actions to it. I did not like this behavior, which can be changed from the preferences by un-checking: "Create a root 'Nautilus-Actions' menu".
Changing this setting requires quiting nautilus:

nautilus -q

This simple tweak saved me a lot of time !