Document viewer script

From Vifm Wiki
Revision as of 14:00, 25 May 2021 by Viminator25 (talk | contribs) (Created page with "This script is for viewing files that are to be opened with [https://pwmt.org/projects/zathura/ zathura]. What's nice about this is that it puts everything into a [https://to...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This script is for viewing files that are to be opened with zathura.

What's nice about this is that it puts everything into a tabbed interface for you and auto-maximizes the window.

These scripts are designed to work with visual selections as well, so you can open up as many documents as you wish all at once.

Source Code

#!/bin/bash

# This script is for launching zathura in a tabbed enviornment,
# and placing every subsequent instance of zathura in that enviornment.
# It also maximizes the window according to the height of your bottom panel

xid_file="$HOME/.config/zathura/xid"

# Open tabbed if necessary
if ! ps -e | grep 'zathura$'; then
    num_wins=$(wmctrl -l | wc -l)
    tabbed -cd -p -1 zathura "$1" -e 1>$xid_file &
    while [ $num_wins -eq $(wmctrl -l | wc -l) ]; do
	sleep 0.0001
    done
    wmctrl -ir $(wmctrl -l | sed -ne '$p' | cut -d " " -f 1) -b add,maximized_vert,maximized_horz
    shift
fi

# Open the rest of the files into tabbed
if [ "$#" -gt 0 ]; then
    read xid < $xid_file
    zathura -e $xid "$@" &
fi

The script uses:

  1. zathura as the document viewer.
  2. tabbed for your tabbed interface. Every subsequent file will be loaded as a new tab.
  3. wmctrl for auto-maximizing the window.
  4. sed, works with all versions.
  5. cut, works with all versions.
  6. wc, works with all versions.
  7. ps, should work with all versions.

The last four are for detecting a new process and processing it to be fed into wmctrl. They should come with every Unix-like operating system.

Installation

Copy the above code into a zathura.sh script (or whatever you want) in either a $PATH directory or in your $VIFM/scripts directory. Then go into your vifm rc file and assign the file type:

filextype *.pdf,*.djvu,*.epub zathura.sh %f &

For Other Kinds of Documents

For other kinds of documents, you can convert them to a pdf using unoconv. Paste this code into a shell script called zadocx.sh

#!/bin/bash

while [ "$#" -gt 0 ]; do
    pdf_name=$(basename -s .docx "$1").pdf
    unoconv "$1"
    rm "$1"
    zathura.sh "$pdf_name"
    shift
done

Then assign the file type in your rc file in the same manner as the last one.