Skip to content

Instantly share code, notes, and snippets.

@Lahorde
Last active March 12, 2020 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lahorde/4cde873e72249c58f1ccd3d22bcb8a85 to your computer and use it in GitHub Desktop.
Save Lahorde/4cde873e72249c58f1ccd3d22bcb8a85 to your computer and use it in GitHub Desktop.
Add some file metadata in linux file explorer

Many PDF file names are not explicit, but their title is. I was looking for a mean to show PDF title in file explorer column (when using detailed view), I posted on stackexchange but did not found any solution I was using Thunar, and I changed file explorer to thunar because some python bindings exists. It is Python nautilus project Just install it, on arch it is python2-nautilus package

After, you have just to read /usr/share/doc/nautilus-python/README And check it works adding copying /usr/share/doc/nautilus-python/examples/block-size-column.py to ~/.local/share/nautilus-python/extensions/ and selectring block-size in column list nautilus preferences.

Now I was sure it was possible some custom columns in nautilus detailed view. It was time to show pdf title. I've tested different python packages to get pdf title :

  • python2-pdfrw package, it cannot decrypt encrypted titles... see issue
  • python2-pdfminer.six, it was very slow to get PDF title for large files, slowing file explorer

I got a simple tool pdfinfo, it is very fast to get PDF title and shows encrypted title, so I used it from python to get PDF name as you can see in pdf-name-column.py

# Nautilus python extension that shows pdf name in a nautilus
# detailed view column - Python 3 script
import os
from urllib.parse import unquote
import subprocess
from os.path import splitext
from gi.repository import GObject,Nautilus
TITLE_PDFINFO = 'Title:'
class ExifColumnExtension(GObject.GObject, Nautilus.ColumnProvider, Nautilus.InfoProvider):
def __init__(self):
pass
def get_columns(self):
return Nautilus.Column(name="NautilusPython::pdf-name-column",
attribute="pdf_name",
label="PDF name",
description="Get pdf name"),
def update_file_info(self, file):
if file.get_uri_scheme() != 'file':
return
filename = unquote(file.get_uri()[7:])
if splitext(filename)[1].lower() == '.pdf' :
res = subprocess.check_output(['pdfinfo', filename]).decode('utf-8')
for line in res.splitlines():
if TITLE_PDFINFO in line :
file.add_string_attribute('pdf_name', line.split(TITLE_PDFINFO)[1].strip())
return
file.add_string_attribute('pdf_name', '')
@alanjbeck
Copy link

alanjbeck commented Jun 4, 2019

Hi there, this is probably a long shot but do you know if there would be any way to modify this script to display PDF keyword metadata in the file explorer.

@arvidj
Copy link

arvidj commented Mar 12, 2020

Very convenient. Here are my notes.
In ubuntu, to install the necessary bindings, simply do sudo apt install python-nautilus. This package seems to have been configured for python2, whereas the script above is written for python3. No worries, simply change the line

from urllib.parse import unquote

to

from urlparse import unquote

After placing the script in the correct location, you need to restart nautilus. Running pkill nautilus and starting it again should do the job. Starting nautilus from the command line will show you any errors from the script, and helps to debug.

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