OCR for Spectacle

If anyone wants a simpler bash script to use spectacle and tesseract:

#!/usr/bin/env bash

TMPFILE=$(mktemp)
trap 'rm -f "$TMPFILE"' EXIT
spectacle -r -o $TMPFILE -b -n 2>/dev/null
if [ -s "$TMPFILE" ]; then
    if [ "$XDG_SESSION_TYPE" = "x11" ]; then
        tesseract $TMPFILE - | xclip -selection clipboard
    fi

    if [ "$XDG_SESSION_TYPE" = "wayland" ]; then
        tesseract $TMPFILE - | wl-copy
    fi
    kdialog --passivepopup "OCR finished" 7 --title "OCR"
fi

Requires tesseract and either xclip or wl-clipboard to be installed of course. Set it in KDE as a keyboard shortcut to launch it and you’re ready to go.

4 Likes