| 1 |
""" |
| 2 |
Author: @DodoLeDev |
| 3 |
|
| 4 |
Add a 'Edit pdf metadata' shortcut button to the right-click menu for pdf files (Nautilus GTK4) |
| 5 |
|
| 6 |
Based on the file collision-extension.py created by myself and @GeopJr for the Collision application |
| 7 |
-> https://github.com/GeopJr/Collision/blob/main/nautilus-extension/collision-extension.py |
| 8 |
|
| 9 |
""" |
| 10 |
|
| 11 |
from subprocess import Popen, check_call, CalledProcessError |
| 12 |
from urllib.parse import urlparse, unquote |
| 13 |
from gi import require_version |
| 14 |
from gettext import textdomain, gettext |
| 15 |
|
| 16 |
textdomain('io.github.diegoivan.pdf_metadata_editor') |
| 17 |
_ = gettext |
| 18 |
|
| 19 |
require_version('Gtk', '4.0') |
| 20 |
require_version('Nautilus', '4.0') |
| 21 |
|
| 22 |
from gi.repository import Nautilus, GObject, Gtk, Gdk |
| 23 |
|
| 24 |
def is_paperclip_installed(): |
| 25 |
try: |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
check_call("[[ -f /usr/bin/pdf-metadata-editor ]] &> /dev/null", shell=True) |
| 30 |
|
| 31 |
return True |
| 32 |
except CalledProcessError: |
| 33 |
return False |
| 34 |
|
| 35 |
class NautilusPaperClip(Nautilus.MenuProvider, GObject.GObject): |
| 36 |
|
| 37 |
def __init__(self): |
| 38 |
self.window = None |
| 39 |
return |
| 40 |
|
| 41 |
|
| 42 |
def openWithPaperClip(self, menu, file): |
| 43 |
|
| 44 |
get_path = lambda file: repr(unquote(urlparse(file.get_uri()).path)) |
| 45 |
|
| 46 |
file_path = get_path(file[0]) |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
Popen("/usr/bin/pdf-metadata-editor " + file_path, shell=True) |
| 52 |
|
| 53 |
|
| 54 |
def get_background_items(self, files): |
| 55 |
return |
| 56 |
|
| 57 |
def get_file_items(self, files): |
| 58 |
|
| 59 |
|
| 60 |
def check_file_types(): |
| 61 |
if files[0].get_uri().endswith('.pdf'): |
| 62 |
return True |
| 63 |
return False |
| 64 |
|
| 65 |
only_one_file = lambda : True if len(files) == 1 else False |
| 66 |
|
| 67 |
|
| 68 |
if not is_paperclip_installed() or not only_one_file() or not check_file_types(): |
| 69 |
return () |
| 70 |
|
| 71 |
|
| 72 |
menu_item = Nautilus.MenuItem( |
| 73 |
name="NautilusPaperClip::EditMetadata", |
| 74 |
label=_("Edit pdf metadata…")) |
| 75 |
|
| 76 |
menu_item.connect('activate', self.openWithPaperClip, files) |
| 77 |
|
| 78 |
return menu_item, |