#!/usr/bin/env bash # slab-pdf [more.pdf …] — open PDFs in slab's minimal viewer. # Appends absolute paths to $SLAB_HOME/state/open-pdf; the menubar's 2 s # tick consumes the file and raises a chromeless PDFKit panel per document # (Esc closes, one "Preview" button escapes to Preview.app). Re-asking for # an open file just brings its window forward. See # slab/menubar-swift/Sources/SlabMenubar/PdfViewer.swift. set -euo pipefail SLAB_HOME="${SLAB_HOME:-$HOME/.local/share/slab}" REQUEST="$SLAB_HOME/state/open-pdf" if [ $# -eq 0 ]; then echo "usage: slab-pdf [more.pdf …]" >&2 exit 1 fi mkdir -p "$SLAB_HOME/state" # Which Claude session asked? Walk up the process tree to the claude pid # (same loop as claude-prompt-log.sh) and match it against the live # active-prompts markers — the viewer chip then wears that session's # sticky emoji. Empty when not launched from a Claude session. session_id="" claude_pid=$$ for _ in 1 2 3 4 5 6 7 8; do parent=$(ps -o ppid= -p "$claude_pid" 2>/dev/null | tr -d ' ') [ -z "$parent" ] || [ "$parent" = "1" ] && break comm=$(ps -o comm= -p "$parent" 2>/dev/null | tr -d ' ') claude_pid=$parent case "$comm" in *claude*) break ;; esac done if command -v jq >/dev/null 2>&1; then for marker in "$SLAB_HOME"/state/active-prompts/*; do [ -f "$marker" ] || continue if [ "$(jq -r '.claude_pid // 0' "$marker" 2>/dev/null)" = "$claude_pid" ]; then session_id=$(jq -r '.session_id // ""' "$marker" 2>/dev/null) break fi done fi for f in "$@"; do if [ ! -f "$f" ]; then echo "slab-pdf: no such file: $f" >&2 exit 1 fi # realpath isn't guaranteed on stock macOS bash paths — resolve by hand. abs="$(cd "$(dirname "$f")" && pwd)/$(basename "$f")" printf '%s\t%s\n' "$abs" "$session_id" >> "$REQUEST" done