Files
infoscreen-dev/scripts/present-pdf-auto-advance.sh

164 lines
4.4 KiB
Bash
Executable File

#!/bin/bash
# PDF Presentation with Auto-Advance
# Works for both native PDF and converted PPTX files
PDF_FILE="$1"
INTERVAL="${2:-10}"
LOOP="${3:-false}"
if [ -z "$PDF_FILE" ]; then
echo "Usage: $0 <file.pdf> [interval-seconds] [loop:true|false]"
exit 1
fi
if [ ! -f "$PDF_FILE" ]; then
echo "Error: File not found: $PDF_FILE"
exit 1
fi
echo "=========================================="
echo " PDF Presentation Mode"
echo "=========================================="
echo ""
echo "File: $(basename "$PDF_FILE")"
echo "Auto-advance: ${INTERVAL}s per slide"
echo "Loop: $LOOP"
echo ""
# Count pages in PDF
PAGE_COUNT=$(pdfinfo "$PDF_FILE" 2>/dev/null | grep "Pages:" | awk '{print $2}')
if [ -z "$PAGE_COUNT" ] || [ "$PAGE_COUNT" -eq 0 ]; then
echo "Warning: Could not determine page count, assuming 10 pages"
PAGE_COUNT=10
else
echo "Detected $PAGE_COUNT pages"
fi
echo ""
# Check for required tools
if ! command -v xdotool &> /dev/null; then
echo "Error: xdotool not installed"
echo "Install with: sudo apt-get install xdotool"
exit 1
fi
# Choose best PDF viewer (prefer Impressive for auto-advance)
PDF_VIEWER=""
PDF_VIEWER_CMD=""
if command -v impressive &> /dev/null; then
PDF_VIEWER="impressive"
PDF_VIEWER_CMD="impressive --fullscreen --nooverview --auto $INTERVAL"
if [ "$LOOP" = "true" ]; then
PDF_VIEWER_CMD="$PDF_VIEWER_CMD --wrap"
else
PDF_VIEWER_CMD="$PDF_VIEWER_CMD --autoquit"
fi
# Impressive handles auto-advance natively, no xdotool needed!
echo "Using Impressive (built-in auto-advance)..."
$PDF_VIEWER_CMD "$PDF_FILE"
exit 0
elif command -v evince &> /dev/null; then
PDF_VIEWER="evince"
PDF_VIEWER_CMD="evince --presentation"
elif command -v okular &> /dev/null; then
PDF_VIEWER="okular"
PDF_VIEWER_CMD="okular --presentation"
else
echo "Error: No suitable PDF viewer found"
echo "Install impressive: sudo apt-get install impressive"
exit 1
fi
echo "Using PDF viewer: $PDF_VIEWER"
echo "Starting presentation mode..."
echo ""
# Start PDF viewer in presentation mode
$PDF_VIEWER_CMD "$PDF_FILE" &
VIEWER_PID=$!
echo "Viewer PID: $VIEWER_PID"
echo "Waiting for viewer to start..."
sleep 5
# Verify it's still running
if ! ps -p $VIEWER_PID > /dev/null 2>&1; then
echo "Error: PDF viewer exited unexpectedly"
exit 1
fi
echo "Starting auto-advance..."
echo "Press Ctrl+C to stop"
echo ""
# Auto-advance loop
CURRENT_PAGE=1
MAX_LOOPS=0
if [ "$LOOP" = "false" ]; then
# Calculate total slides to advance (pages - 1, since we start on page 1)
MAX_ADVANCES=$((PAGE_COUNT - 1))
else
# Infinite loop
MAX_ADVANCES=999999
fi
ADVANCE_COUNT=0
while ps -p $VIEWER_PID > /dev/null 2>&1 && [ $ADVANCE_COUNT -lt $MAX_ADVANCES ]; do
sleep $INTERVAL
# Find the PDF viewer window
VIEWER_WINDOW=$(xdotool search --pid $VIEWER_PID 2>/dev/null | tail -1)
if [ -z "$VIEWER_WINDOW" ]; then
# Fallback: search by window name
VIEWER_WINDOW=$(xdotool search --name "$(basename "$PDF_FILE")" 2>/dev/null | tail -1)
fi
if [ -n "$VIEWER_WINDOW" ]; then
# Ensure window is focused
xdotool windowactivate --sync "$VIEWER_WINDOW" 2>/dev/null
sleep 0.2
# Send Right Arrow or Page Down (both work in most PDF viewers)
xdotool key --clearmodifiers --window "$VIEWER_WINDOW" Right
CURRENT_PAGE=$((CURRENT_PAGE + 1))
ADVANCE_COUNT=$((ADVANCE_COUNT + 1))
echo "[$(date '+%H:%M:%S')] Advanced to page $CURRENT_PAGE"
# Check if we've reached the end
if [ $CURRENT_PAGE -gt $PAGE_COUNT ]; then
if [ "$LOOP" = "true" ]; then
echo "Reached end, looping back to start..."
CURRENT_PAGE=1
else
echo ""
echo "Reached end of presentation (page $PAGE_COUNT)"
echo "Keeping viewer open..."
# Keep viewer running, just stop advancing
wait $VIEWER_PID
exit 0
fi
fi
else
# Fallback: send key to active window
xdotool key --clearmodifiers Right
CURRENT_PAGE=$((CURRENT_PAGE + 1))
ADVANCE_COUNT=$((ADVANCE_COUNT + 1))
echo "[$(date '+%H:%M:%S')] Advanced (fallback) #$ADVANCE_COUNT"
fi
done
echo ""
echo "Presentation ended"
exit 0