pdf-tools/scripts/batch-index.sh
Eric Furst e464871b69 Initial public release of pdf-tools
Command-line tools for marked-up PDFs and scanned handwritten notes:
pdf-scrub, pdf-notes-md, scan-notes-md (Ollama by default, Claude on
request), scan-notes-index (per-page search metadata), and
scan-notes-stats.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011ZSvbNaLhgFuTm7wmkgbWs
2026-11-03 07:25:51 -05:00

52 lines
1.7 KiB
Bash
Executable file

#!/usr/bin/env bash
# batch-index.sh — run scan-notes-index on every transcribed PDF in a folder.
#
# usage: batch-index.sh <folder> [scan-notes-index options...]
#
# PDFs are taken newest first (reverse filename order, which is date order
# for date-prefixed names). A PDF without a <stem>.md transcript is skipped.
# Pages already indexed are kept, so the script can simply be rerun after an
# interruption. A failed PDF is reported and the batch moves on.
#
# Long runs belong in tmux: tmux new -s index 'batch-index.sh ~/notebooks'
set -uo pipefail
if [[ $# -lt 1 || ! -d "$1" ]]; then
echo "usage: $0 <folder> [scan-notes-index options...]" >&2
exit 64
fi
dir="$1"; shift
if command -v scan-notes-index >/dev/null 2>&1; then
run() { scan-notes-index "$@"; }
else
: "${PDF_TOOLS_DIR:?set PDF_TOOLS_DIR or install scan-notes-index}"
run() { (cd "$PDF_TOOLS_DIR" && uv run --quiet scan-notes-index "$@"); }
fi
pdfs=()
while IFS= read -r -d '' f; do pdfs+=("$f"); done \
< <(find "$dir" -type f -iname '*.pdf' -not -name '.*' -print0 | sort -rz)
total=${#pdfs[@]}
done_n=0; skipped=0; failed=()
start=$(date +%s)
for i in "${!pdfs[@]}"; do
pdf="${pdfs[$i]}"
echo "=== [$((i + 1))/$total] ${pdf#"$dir"/} ($(date '+%H:%M'))" >&2
if [[ ! -e "${pdf%.*}.md" ]]; then
echo "no transcript; skipping" >&2
skipped=$((skipped + 1))
continue
fi
if run "$@" "$pdf"; then
done_n=$((done_n + 1))
else
failed+=("${pdf#"$dir"/}")
fi
done
mins=$(( ($(date +%s) - start) / 60 ))
echo "=== finished in ${mins} min: $done_n indexed, $skipped skipped, ${#failed[@]} failed" >&2
for f in ${failed[@]+"${failed[@]}"}; do echo "failed: $f" >&2; done
[[ ${#failed[@]} -eq 0 ]]