| 1 | #!/bin/sh |
| 2 | # OCR a screen region: slurp -> grim -> tesseract -> clipboard, with a notification showing what was read. |
| 3 | # usage: ocr [lang] (tesseract language, default eng; e.g. `ocr eng+hin`) |
| 4 | # slurp takes aurora's matugen colours when they're there. |
| 5 | |
| 6 | lang="${1:-eng}" |
| 7 | c="${XDG_CACHE_HOME:-$HOME/.cache}/matugen/aurora.json" |
| 8 | |
| 9 | if command -v jq >/dev/null && [ -r "$c" ]; then |
| 10 | bg=$(jq -r '.surfaceLowest // empty' "$c") |
| 11 | fg=$(jq -r '.primary // empty' "$c") |
| 12 | fi |
| 13 | if [ -n "$bg" ] && [ -n "$fg" ]; then |
| 14 | g=$(slurp -b "${bg}88" -c "${fg}ff" -s "#00000000" -w 1) || exit 0 |
| 15 | else |
| 16 | g=$(slurp) || exit 0 |
| 17 | fi |
| 18 | |
| 19 | text=$(grim -g "$g" - | tesseract stdin stdout -l "$lang" 2>/dev/null | sed -e 's/[[:space:]]*$//' -e '/./,$!d') |
| 20 | if [ -z "$text" ]; then |
| 21 | notify-send -a ocr "OCR" "no text found" |
| 22 | exit 1 |
| 23 | fi |
| 24 | printf '%s' "$text" | wl-copy |
| 25 | notify-send -a ocr "OCR · copied" "$(printf '%s' "$text" | head -c 200)" |