I wrote the following script to accomplish the desired behavior for the flatpak version of Okular (I’m using LMDE 5). The script is based on changing the window title via wmctrl.
Getting the desired behavior was tricky because of the new welcome screen, but I think I made it (I’m not an expert programmer whatsoever, so I appreciate any suggestions to improve the code.)
Before running the script, make sure to uncheck the option “Display document title in titlebar if available”, and to set the option “When not displaying document title" to “Display full file path” (the script will change it, but it requires the full path to make each singular file identifiable). Also, uncheck the option “Open new files in tabs.”
#!/usr/bin/env bash
# Set expectedwindowtitle whether there is an argument or not, to handle the
# welcome screen:
if [ -z "$1" ]
then
expectedwindowtitle="Welcome"
else
pdfbasename=$(basename "$1")
pdffullpath=$(dirname "$(realpath "$1")")
pdfpath=${pdffullpath/#$HOME/\~}
expectedwindowtitle="$pdfbasename ($pdfpath)"
fi
# Search for a window with the substring "— Okular" in the title (with em-dash,
# U+2014, that is, untouched), and get its ID and filename:
originaltitle=`wmctrl -l | grep -v "okularjump" | grep -v "grep" | grep -m 1 "— Okular" | awk 'BEGIN{RS=" — Okular"; FS="'$HOSTNAME' "}NF>1{print $NF}'`
originalwindowID=`wmctrl -l | grep -v "okularjump" | grep -v "grep" | grep -m 1 "— Okular" | awk '{ print $1; }'`
# If there is an existent window with the substring "— Okular" (with em-dash)
# and a filename in the title, then rename it to a new format, as follows:
# [basemane (short path) – Okular
# Note that the separator in the new format is an en-dash (U+2013)
if [ ! -z "$originaltitle" ]
then
originalfullpath=$(dirname "$originaltitle")
originalbasename=$(basename "$originaltitle")
originalpath=${originalfullpath/#$HOME/\~}
newwindowname="$originalbasename ($originalpath) – Okular" # With en-dash
wmctrl -r $originaltitle -N "$newwindowname"
fi
# Search for an already modified window title that matches the expected window
# title and extract the basename and the path
existentwindowtitle=`wmctrl -l | grep -v "okularjump" | grep -v "grep" | grep "– Okular" | grep -m 1 "$expectedwindowtitle" | awk 'BEGIN{RS=" – Okular"; FS="'$HOSTNAME' "}NF>1{print $NF}'` # With en-dashes
# If expected and existent windows are the same, then switch to the existent
# window
if [[ "$expectedwindowtitle" == "$existentwindowtitle" ]]
then
wmctrl -a "$existentwindowtitle"
else
# There is an argument and nothing to switch to. Then, open the argument with
# the new format in the title
if [ ! -z "$1" ]
then
/usr/bin/flatpak run --branch=stable --arch=x86_64 --command=okular --file-forwarding org.kde.okular @@ "$1" @@ &>/dev/null &
while ! test -n "$argoriginalwindowID"; do
sleep 0.01
argoriginalwindowID=`wmctrl -l | grep -v "okularjump" | grep -v "grep" | grep "— Okular" | grep -i -m 1 "$(basename "$1")" | awk '{ print $1; }'`
done
pdffullpath=$(dirname "$(realpath "$1")")
pdfpath=${pdffullpath/#$HOME/\~}
argWname="$(basename "$1") ($pdfpath) – Okular"
wmctrl -ir $argoriginalwindowID -N "$argWname"
else
# There is no argument and nothing to switch to. Open a welcome screen
# with the word Welcome in the title (to make possible to find it later)
/usr/bin/flatpak run --branch=stable --arch=x86_64 --command=okular --file-forwarding org.kde.okular --title "Welcome" &>/dev/null &
while ! test -n "$welcomeWname"; do
sleep 0.01
welcomeWname=`wmctrl -l | grep -v "okularjump" | grep -v "grep" | grep -m 1 "— Okular" | grep -m 1 "Welcome"`
done
originalwindowID=`wmctrl -l | grep -v "okularjump" | grep -v "grep" | grep -m 1 "— Okular" | grep -m 1 "Welcome" | awk '{ print $1; }'`
newwindowname="Welcome – Okular"
wmctrl -ir $originalwindowID -N "$newwindowname"
fi
fi
To use the script as default all around the system, I saved the code as ~/okularjump, made it executable and copied it to /usr/bin. Then, I copied the default .desktop file to the user folder twice: one with the purpose of running the script and the other to hide the default flatpak launcher:
chmod +x okularjump
sudo cp okularjump /usr/bin/
cp /var/lib/flatpak/app/org.kde.okular/current/active/export/share/applications/org.kde.okular.desktop ~/.local/share/applications/
cp /var/lib/flatpak/app/org.kde.okular/current/active/export/share/applications/org.kde.okular.desktop ~/.local/share/applications/okular.desktop
echo "Hidden=true" >> ~/.local/share/applications/org.kde.okular.desktop
Then, I changed the Exec line in ~/.local/share/applications/okular.desktop to Exec=okularjump %U
Finally, I set Okular as the default PDF manager in the system settings.
Regarding opening multiple instances of the same file, it is still possible to do it by opening the file from Okular via the open dialog. The script will adjust the window title the next time it runs.
I took inspiration from Jumpapp https://github.com/mkropat/jumpapp (that should work for the non-Flatpak version, but I’m not sure) and Flatjump https://github.com/ceranco/flatjump (which didn’t work for me, but I invite to try).