-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddcopypathscript
More file actions
executable file
·59 lines (48 loc) · 1.84 KB
/
addcopypathscript
File metadata and controls
executable file
·59 lines (48 loc) · 1.84 KB
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
#!/usr/bin/env bash
set -euo pipefail
# Install required packages, skipping any already present.
apt-get install -y xclip python3-nautilus > /dev/null
# When run via sudo, write into the invoking user's home, not root's.
if [[ -n "${SUDO_USER:-}" ]]; then
user_home=$(getent passwd "$SUDO_USER" | cut -d: -f6)
else
user_home="$HOME"
fi
# Remove the older Scripts-submenu version if present.
old_script="$user_home/.local/share/nautilus/scripts/Copy path"
if [[ -f "$old_script" ]]; then
rm "$old_script"
printf 'Removed old scripts-submenu version.\n'
fi
ext_dir="$user_home/.local/share/nautilus-python/extensions"
mkdir -p "$ext_dir"
ext_file="$ext_dir/copy-path.py"
new_content=$(cat <<'PYEXT'
from gi.repository import GObject, Nautilus
import subprocess
import os
class CopyPathExtension(GObject.GObject, Nautilus.MenuProvider):
def get_file_items(self, files):
if not files:
return []
item = Nautilus.MenuItem(name='CopyPath::copy', label='Copy path')
item.connect('activate', lambda _m: self._copy(files[0]))
return [item]
def _copy(self, nautilus_file):
path = nautilus_file.get_location().get_path()
if not path:
return
session = os.environ.get('XDG_SESSION_TYPE', '')
if session == 'wayland' and subprocess.run(['which', 'wl-copy'], capture_output=True).returncode == 0:
subprocess.run(['wl-copy'], input=path.encode(), check=True)
else:
subprocess.run(['xclip', '-selection', 'clipboard'], input=path.encode(), check=True)
PYEXT
)
if [[ -f "$ext_file" ]] && [[ "$(cat "$ext_file")" == "$new_content" ]]; then
printf 'Already up to date: %s\n' "$ext_file"
else
printf '%s\n' "$new_content" > "$ext_file"
printf 'Installed: %s\n' "$ext_file"
fi
printf 'Restart Nautilus to activate: nautilus -q && nautilus\n'