Files
digital-garden-flo/bash/watch-pdf.md
T
2026-10-07 11:20:20 +01:00

34 lines
1.0 KiB
Markdown

# Watch PDF
So when using typst to create documents, it is fine to have something watching the typst changes, and reload the
file with evince, so you constantly see the changes to the pdf.
I use this bash script with inotifywait, make a bash script and add it to path
Use watch-pdf /path/to/file.pdf okay?
```bash
#!/usr/bin/env bash
set -euo pipefail
[[ -z "$1" ]] && { echo "ERROR: No PDF filename provided"; exit 1; }
PDF_PATH="$1"
if [[ ! -f "${PDF_PATH}" ]]; then
echo "ERROR: ${PDF_PATH} is not a regular file"
exit 1
fi
command -v evince &> /dev/null || { echo "❌ evince not found. Install: sudo apt-get install evince"; exit 1; }
echo "=== Reload Watcher Started ==="
echo "Monitoring ${PDF_PATH} for writes…"
while IFS= read -r change_file; do
[[ "${change_file}" == "${PDF_PATH}" ]] || continue
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ${PDF_PATH} modified → opening evince"
evince "${PDF_PATH}" &
done < <(inotifywait -mr --format '%w%f' -e close_write "${PDF_PATH}")
echo "Watcher stopped."
```