-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnautilus-path.py
70 lines (61 loc) · 2.79 KB
/
nautilus-path.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Copyright (C) 2024 Janik Haitz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from urllib.parse import unquote, urlparse
from pathlib import Path
from gi import require_version
try:
require_version('Nautilus', '4.0')
require_version('Gtk', '4.0')
require_version('Gdk', '4.0')
gi_version = 4
except:
require_version('Nautilus', '3.0')
require_version('Gtk', '3.0')
require_version('Gdk', '3.0')
gi_version = 3
from gi.repository import GObject, Nautilus, Gtk, Gdk
FILE_SEPARATOR = '\n'
class NautilusPath(GObject.GObject, Nautilus.MenuProvider):
def __init__(self):
if gi_version == 4:
self.clipboard = Gdk.Display.get_default().get_clipboard()
else:
self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
def get_file_items(self, selected_files: list[Nautilus.FileInfo]):
copy_menu = Nautilus.MenuItem(name=f'PathExtension::CopyFile{"s" if len(selected_files) != 1 else ""}',
label=f'Copy Path{"s" if len(selected_files) != 1 else ""}',
tip='Copy selected file path(s).')
copy_menu.connect('activate', self._copy_path, selected_files)
return (copy_menu, )
def get_background_items(self, current_directory: Nautilus.FileInfo):
copy_menu = Nautilus.MenuItem(name='PathExtension::CopyDirectory',
label='Copy Path',
tip='Copy selected directory path')
copy_menu.connect('activate', self._copy_path, [current_directory])
return (copy_menu, )
def _copy_path(self, _menu, selected_files: list[Nautilus.FileInfo]):
posix_list: list[str] = []
for file in selected_files:
fp = Path(unquote(urlparse(file.get_uri()).path)) # strip fileinfo to plain paths
posix = fp.as_posix()
if ' ' in posix:
posix = f'"{posix}"'
posix_list.append(posix)
posix_string = FILE_SEPARATOR.join(posix_list)
if posix_string is not None:
if gi_version == 4:
self.clipboard.set(posix_string)
else:
self.clipboard.set_text(posix_string, -1)