Google
 

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:

#!/usr/bin/python
import gtk
import sys

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


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/script_name.py %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 !

9 comments:

Anonymous said...

I don't see the need for this in Ubuntu. You can just copy the file. When you paste into a text editor it will paste the path.

It doesn't work like this in Windows, so that is why the copy as path is required...

Hesham A. Amin said...

Yes, but it does not work nice with terminal. (file://) is added.
Also, the demonstration of the concepts is what matters.

Alexander Kurochkin said...

ye, thank you

The Grouchy Gaijin said...

This makes attaching files in mutt much easier - thank you.

Unknown said...

Thanks a lot for this post!
Saves a lot of time for mee to.

Unknown said...

Ah, thank you very much. This will save me a bunch of time.

Unknown said...

Unfortunately, this won't work on some newer distributions due to dependency issues with gtk. However, modifying the script to the following worked for me:

```
#!/usr/bin/python
import sys
from tkinter import Tk
path = sys.argv[1]
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append(path)
r.update() # now it stays on the clipboard after the window is closed
r.destroy()
```

Anonymous said...

Unfortunately, this won't work on some newer distributions due to dependency issues with gtk. However, modifying the script to the following worked for me on Ubuntu 20.04:

```
#!/usr/bin/python
import sys
from tkinter import Tk
path = sys.argv[1]
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append(path)
r.update() # now it stays on the clipboard after the window is closed
r.destroy()
```

Anonymous said...

This command worked for me with nautilus actions:
bash -c "echo -n %f | xclip -i -selection clipboard ; sleep 5"
xclip must be installed.