Skip to content

Instantly share code, notes, and snippets.

@meramsey
Last active October 11, 2021 20:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meramsey/f1d20da371de82e801df19c92a673e63 to your computer and use it in GitHub Desktop.
Save meramsey/f1d20da371de82e801df19c92a673e63 to your computer and use it in GitHub Desktop.
Add Copy Path to nautilus

Add Copy Path to nautilus

Download the file then move it and make executable and restart nautilus to see the Copy Path icon

wget https://gist.githubusercontent.com/meramsey/f1d20da371de82e801df19c92a673e63/raw/2bb9cb9007f21e85b00aade70d5db21c41b44f44/nautilus-copy-paths.py
sudo mv nautilus-copy-paths.py /usr/share/nautilus-python/extensions/nautilus-copy-paths.py
sudo chmod +x /usr/share/nautilus-python/extensions/nautilus-copy-paths.py
nautilus -q

See also related: https://askubuntu.com/questions/225666/copy-file-and-folder-path-from-nautilus/1367229#1367229 https://fostips.com/open-as-administrator-ubuntu-21-04-fix/

import os
from gi import require_version
require_version("Gtk", "3.0")
require_version("Nautilus", "3.0")
from gi.repository import Gdk, Gtk, Nautilus, GObject
from gettext import gettext, bindtextdomain, textdomain
NAUTILUS_PATH = "/usr/bin/nautilus"
class NautilusAdmin(Nautilus.MenuProvider, GObject.GObject):
"""Simple Nautilus extension that adds some file path actions to
the right-click menu, using GNOME's new admin backend."""
def __init__(self):
pass
def get_file_items(self, window, files):
"""Returns the menu items to display when one or more files/folders are
selected."""
# Don't show when more than 1 file is selected
if len(files) != 1:
return
file = files[0]
# Add the menu items
items = []
self._setup_gettext()
self.window = window
if file.get_uri_scheme() == "file": # must be a local file/directory
if file.is_directory():
if os.path.exists(NAUTILUS_PATH):
items += [self._create_nautilus_item(file)]
return items
def get_background_items(self, window, file):
"""Returns the menu items to display when no file/folder is selected
(i.e. when right-clicking the background)."""
# Add the menu items
items = []
self._setup_gettext()
self.window = window
if file.is_directory() and file.get_uri_scheme() == "file":
if os.path.exists(NAUTILUS_PATH):
items += [self._create_nautilus_item(file)]
return items
def _setup_gettext(self):
"""Initializes gettext to localize strings."""
try: # prevent a possible exception
locale.setlocale(locale.LC_ALL, "")
except:
pass
bindtextdomain("nautilus-admin", "/usr/share/locale")
textdomain("nautilus-admin")
def _create_nautilus_item(self, file):
item = Nautilus.MenuItem(
name="NautilusAdmin::Nautilus",
label=gettext("Copy path"),
tip=gettext("Copy File path to clipboard"),
)
item.connect("activate", self._nautilus_run, file)
return item
def _nautilus_run(self, menu, file):
"""'Copy File path' menu item callback."""
uri = file.get_uri()
file_path = uri.replace("file://", "")
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
clipboard.set_text(file_path, -1)
clipboard.store()
@meramsey
Copy link
Author

meramsey commented Oct 3, 2021

Video to show it in action
copy_path

@meramsey
Copy link
Author

meramsey commented Oct 4, 2021

Recommend actually using the other repository below which adds this via user local setup.
https://github.com/meramsey/nautilus-copy-path

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment