Wrapper for Xpdf for setting the window title

This is a simple shell script for setting title of Xpdf window.

Xpdf default window title is in format "xpdf: foo.pdf" where foo.pdf also contains path to the .pdf file. Often this title takes too much space on taskbar button, so by looking at the taskbar button it is hard to know what document has Xpdf opened.

Here is how it looks like with default Xpdf title:

    1. Xpdf window with default title
    2. Xpdf window with default title
    1. Taskbar button with default Xpdf title
    2. Taskbar button with default Xpdf title

But Xpdf has command line option -title title which sets the title of the Xpdf window.

The following wrapper is a shell script, which you can place in your PATH variable and make it executable. It makes use of the option.

When executed, it scans original Xpdf arguments. If it detects that -title option is already used, it just passes all of the original arguments to Xpdf binary. If the option is not used, it finds a name of .pdf file for Xpdf to open and prepends arguments list with -title set to the basename of that file name. Then it executes Xpdf binary and passes this modified arguments list to it.

And here is how it looks like when using this wrapper:

    1. Xpdf window with title set by the wrapper
    2. Xpdf window with title set by the wrapper
    1. Taskbar button with title set by the wrapper
    2. Taskbar button with title set by the wrapper
#!/bin/sh                                                                                   

xpdf_binary="$(which -a xpdf | head -n2 | tail -n1)"

function find_file_in_args {
    f=""
    while [ $# -gt 0 ]; do
        if [ "$(cut -c1 <<< "$1")" != "-" ]; then
            f="$(echo $1 | rev | cut -d/ -f1 | rev)"
            return
        fi
        shift
    done
}

find_file_in_args "$@"
[ -n "$f" ] && targ="-title '$f'" || targ=""
# eval is used to preserve apostrophes around argument for -title                           
# option                                                                                    
eval '"$xpdf_binary"' $targ '"$@"'