Skip to content

Instantly share code, notes, and snippets.

@xream
Forked from firexcy/readme.md
Created October 12, 2023 16:35
Show Gist options
  • Save xream/59bb04d134e5bfbdb6e4bf77d13cba26 to your computer and use it in GitHub Desktop.
Save xream/59bb04d134e5bfbdb6e4bf77d13cba26 to your computer and use it in GitHub Desktop.
DIY a Rewind.ai

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 https://sspai.com/prime/story/rewind-diy (paywalled). If you find these scripts helpful, you may consider a subscription as support. Thank you.

#!/bin/bash
outpath="$HOME/Pictures/Rewind"
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"
exit
else
omp=$(command -v ocrmypdf)
fi
# Capture screenshots
echo "Capturing at $ts"
capture=$(
for ((i = 1; i <= nDisplay; i++)); do
echo "$outpath/capture.$ts.$i.pdf"
done
)
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
done
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>xyz.cyhsu.script.rewind</string>
<key>ProgramArguments</key>
<array>
<string>/Users/platyhsu/bin/rewind</string>
</array>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
</dict>
<key>StartInterval</key>
<integer>30</integer>
<key>RunAtLoad</key>
<true/>
<key>StandardOutPath</key>
<string>/tmp/rewind.log</string>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment