Run OCR / Text Recognition On Selected Area On Screen

Step-by-Step: Setting Up an OCR + QR Script Workflow in BetterTouchTool using Tessaract OCR Engine


Step 1: Install Homebrew (if not already installed)

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Step 2: Install Tesseract OCR

brew install tesseract

Step 2a (Optional): Install additional Tesseract languages

brew install tesseract-lang

Tesseract language data will be in /opt/homebrew/share/tessdata/ (Apple Silicon Macs) or /usr/local/share/tessdata/ (Intel Macs).


Step 3 (Optional): Install zbar for QR code recognition

brew install zbar

Step 4: Check installation paths

which tesseract
which zbarimg

You should see output like /opt/homebrew/bin/tesseract.


Step 5: Add your script to BetterTouchTool

  • Open BetterTouchTool and go to your preferred trigger section (e.g., Keyboard Shortcut, Trackpad Gesture).
  • Click "Add New Trigger" and set your shortcut or gesture.
  • For the action, select "Run Shell Script / Task."
  • Paste your script. Example script below:
#!/bin/zsh
export PATH="/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
LANGS="eng"
ocr_dir="$HOME/.ocr_temp"
mkdir -p "$ocr_dir"
timestamp=$(date +%s)
run_dir="$ocr_dir/run_${timestamp}"
mkdir -p "$run_dir"
img_file="$run_dir/screencap.png"
txt_file="$run_dir/out.txt"
/usr/sbin/screencapture -i "$img_file" || exit 1
qr_output=$(zbarimg --quiet --raw "$img_file" 2>/dev/null)
if [[ -n "$qr_output" ]]; then
    echo "$qr_output" | pbcopy
else
    tesseract "$img_file" "$txt_file" -l $LANGS >/dev/null 2>&1
    awk -v ORS="" '
        BEGIN{para=""}
        {
          if (NF==0) {
            if (para!="") print para "\n\n";
            para="";
          } else {
            if (para=="") para=$0;
            else para=para " " $0;
          }
        }
        END{if (para!="") print para}
    ' "${txt_file}.txt" | iconv -f UTF-8 -t UTF-8 | pbcopy
    rm -f "${txt_file}.txt"
fi
rm -f "$img_file"

Step 6: Usage

  • Invoke your BTT gesture or shortcut.
  • Select an area on your screen.
  • The extracted text or QR code link will be copied to your clipboard, ready to paste anywhere.
2 Likes