Skip to content

Instantly share code, notes, and snippets.

Last active May 20, 2024 18:19
Show Gist options
  • Save firexcy/c55a94ed13e8a5c2af5533fe6316d640 to your computer and use it in GitHub Desktop.
Save firexcy/c55a94ed13e8a5c2af5533fe6316d640 to your computer and use it in GitHub Desktop.

This Gist provides a solution to periodically capture screenshots of your Mac, and create therefrom a searchable PDF archive so that you can always get an answer to the “what, when, and where” questions about your usages.

To use these scripts:

  1. Download the shell script rewind, then:

    1. put it under ~/bin (or other fixed path you prefer);

    2. execute

      chmod +x ~/bin/rewind

      in Terminal to grant the execute permission;

  2. Download the xyz.cyhsu.script.rewind.plist, then:

    1. put it under ~/Library/LaunchAgents/;

    2. (REQUIRED) change the value of the key array in line 9 to the absolute, full path of the rewind script.

      N.B. No env or ~ will be interpolated by launchd;

  3. Install OCRmyPDF with Homebrew:

    brew install ocrmypdf

    and download the necessary dataset from GitHub, which should be put under one of the following paths:

    • Apple silicon models: /opt/homebrew/Cellar/tesseract/[VERSION]/share/tessdata/
    • Intel models: /usr/local/Cellar/tesseract/[VERSION]/share/tessdata/
  4. Select Apple menu > System Settings, then go to Screen Recording under Security and Privacy.

    There, click the add button, and ensure all following items are added to the list:

    • /bin/bash
    • /System/Library/CoreServices/launchservicesd
    • /usr/sbin/screencapture
  5. Execute

    launchctl load ~/Library/LaunchAgents/xyz.cyhsu.script.rewind.plist

    in Terminal to load the agent.

Thereafter, you can find timestamped, OCR’d screenshots in PDF format under ~/Pictures/Rewind. The default period is 30 seconds. Multiple-display setup is supported and screenshots will be captured for each display.

A detailed description of this method is available at (paywalled). If you find these scripts helpful, you may consider a subscription as support. Thank you.

mkdir -p "$outpath"
nDisplay=$(system_profiler SPDisplaysDataType | grep -c Resolution)
ts=$(date +%Y%m%d%H%M%S)
# Detect whether ocrmypdf is installed
if ! command -v ocrmypdf &>/dev/null; then
echo "ocrmypdf could not be found"
omp=$(command -v ocrmypdf)
# Capture screenshots
echo "Capturing at $ts"
for ((i = 1; i <= nDisplay; i++)); do
echo "$outpath/capture.$ts.$i.pdf"
echo "$capture" | xargs screencapture -x -t pdf 2>&1 && echo "Captured"
# OCR output files
for ((i = 1; i <= nDisplay; i++)); do
taskpolicy -b\
"$omp" "$outpath/capture.$ts.$i.pdf" "$outpath/capture.$ts.$i.pdf" \
-l chi_sim+eng\
--output-type pdf\
--optimize 3
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">
Copy link

This looks very interesting. How do you search your screenshots?

Copy link

The screenshots does not contain any windows/apps, only the desktop screen. Do you know how to fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment