0

Before recently discovering about zathura I have been using evince.

I have a script to build and open my resume and want to open it with zathura. How can I force zathura to only open it once?

The command I am using in my script is zathura file.pdf & but this opens a new instance every time the script is run?

user10853
  • 1,506
  • 5
  • 21
  • 42
  • Presumably you want zathura to resync when the build is finished? This should happen automatically IIRC (zathura watches for changes to open files). – muru Jan 15 '18 at 05:13
  • @muru Thanks. I understand that. However, the last command in my script is `zathura file.pdf &` which will run every time I build. So basically I either want to avoid relaunching zathura for every build or force zathura to open only one instance (preferably of this file only). – user10853 Jan 15 '18 at 14:16

3 Answers3

1

When using SyncTeX with zathura, it does this automatically:

zathura --synctex-forward 10:1:main.tex main.pdf

searches for zathura instances looking at main.pdf and sends them the command to look at the output of line 10, column 1 in main.tex. If no instance is open, it opens a new one. All subsequent calls then just focus the already open one.

When there is no synctex file for the pdf, it works fine. But if there is a corresponding synctex file and you enter an invalid address, zathura complains in standard output, so the command that I am using for viewing (without synctex) is

zathura --synctex-forward :: main.pdf > /dev/null &

where the :: is so that zathura can parse the synctex command and > /dev/null is to silence the output

Maybe there is another option than hijacking the SyncTeX functionality.

Vladidobro
  • 11
  • 1
1

In that case your script could work by killing all zathura instances before open it again:

killall zathura #Kill all previous zathura instances zathura file.pdf & #Open zathura

Hope it helps.

galoget
  • 2,943
  • 2
  • 20
  • 24
  • Thanks but that's not what I'm looking for. After all I want the first instance to remain open and refresh to see the changes. – user10853 Jan 14 '18 at 20:57
0

You can write a script to launch the same window if a file is already opened in zathura.

Save script below to your local drive. For example ~/unique_zathura_launcher.sh. Now, you open your file with ~/unique_zathura_launcher.sh filename.pdf instead of zathura filename.pdf. You can create an alias to save keystrokes.

You can use this script as the default application for pdf files. Simply right click a pdf file and choose 'open with command' and add ~/unique_zathura_launcher.sh as the command. As a result, you will no longer open the same file in multiple zathura instances.

What the script below does is simply check if any zathrua process contains the input file path. If yes, find the window id and activate it. Otherwise, launch a new zathura instance.

#!/bin/bash

# This script assumes all files are passed with an absolute path. Otherwise, it may not work properly.

inputAbsPath=$(realpath -s $1)
inputBasename=$(basename "$1")
zathuraProcess=$(ps aux | grep zathura | grep -v grep)

if [[ -z $zathuraProcess ]]
then
    # open the file directly if no zathura process is detected
    zathura "$1" &
    exit
fi

pdfProcessWindowId="undefined"

while read -r pdfProcess; do
    pdfProcessPid=$(echo $pdfProcess | awk '{print $2}')
    if [[ -n $(wmctrl -l -p | grep $pdfProcessPid | grep -v grep) ]] && [[ -n $(wmctrl -l -p | grep $pdfProcessPid | grep -v grep | awk '{print $1}') ]]
    then
        pdfProcessWindowId=$(wmctrl -l -p | grep $pdfProcessPid | grep -v grep | awk '{print $1}' | head -n 1)
    fi
done <<< "$(ps aux | grep zathura | grep "$inputAbsPath" | grep -v grep)"


if [[ $pdfProcessWindowId == "undefined" ]]
then
    zathura "$1" &
else
    wmctrl -ia $pdfProcessWindowId
fi
Hank
  • 1
  • 1