Jérôme Belleman
Home  •  Tools  •  Posts  •  Talks  •  Travels  •  Graphics  •  About Me

How to Misuse Xpdf for More Interactivity?

26 Sep 2009

Xpdf can use hyperlinks in PDF files to open web pages, as they are meant to be used. For good or bad, there are tricks to make Xpdf open anything else too.

Sometimes, it would be nice to not only be able to have hyper references open web pages in a browser but just about anything else too, local or remote; if the hyper reference points to a web page we want to start a browser, if it points to another PDF file we want another (or the same) Xpdf instance to show it, if it points to a directory on the file system we want a terminal with a shell in the corresponding directory, ready to hear from us.

1 Misusing Xpdf

Xpdf will only be told what browser it is to use by setting the urlCommand option. In other words, it can't make out the difference between a web page or something else. The point is to trick it by specifying as browser a script which will decide what to do with the hyper reference. Something along the lines of:

#!/bin/sh
arg=`echo $1 | sed -e 's/^file:\(.*\)/\1/'`

if [ -d $arg ]; then
   cd $arg
   urxvtc 
else
   xdg-open $arg
fi

Such as script first cleans up the path passed as argument: I don't know if it's the LATEX hyperref package, or common trait of links in all PDF files, or Xpdf that adds a file: prefix in front of the path when it's not a PDF but we want to get rid of it. It will then open a terminal with a shell in the right place should the hyper reference point to a local directory; otherwise, we leave it to xdg-open to open the file with the right program.

2 Misusing xdg-open

For the rest, it's a case of telling xdg-open how to open the files we'll need if it doesn't know it already. It's even possible to execute custom scripts and programs by clicking on a hyper reference. A good approach is to first check whether xdg-open knows how:

xdg-open xclock.sh

If nothing is configured yet, xdg-open will angrily croak:

Error: no "view" mailcap rules found for type "application/x-sh"

If so, add the appropriate line in your ~/.mailcap:

application/x-sh; sh %s

Clearly, from there, the sky's the limit. The only remaining consideration is that of security, and whether you're ready to have PDFs wield that much freedom.

3 References