Skip to content

Instantly share code, notes, and snippets.

@pnc
Last active July 25, 2023 18:50
Show Gist options
  • Save pnc/b7fb38d70f157cd40595d9e52bebc055 to your computer and use it in GitHub Desktop.
Save pnc/b7fb38d70f157cd40595d9e52bebc055 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
CONTENTS=$(tesseract -c language_model_penalty_non_dict_word=0.8 --tessdata-dir /usr/local/share/tessdata/ "$1" stdout -l eng | xml esc)
hex=$((cat <<EOF
<?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">
<string>$CONTENTS</string>
</plist>
EOF
) | plutil -convert binary1 - -o - | xxd -p | tr -d '\n')
xattr -w -x com.apple.metadata:kMDItemFinderComment "$hex" "$1"
mdimport "$1"
@berkus
Copy link

berkus commented Mar 19, 2018

@sclarke shouldn't be any downsides, only larger indexes (but not too daunting).

@jk
Copy link

jk commented Mar 19, 2018

For @amake tip you have to brew install imagemagick. And if you need other languages than englisch, you need to install brew install --with-all-languages tesseract and change the -l argument on tesseract. For example if you want to recognize english and german use l eng+deu.

@crystoneme
Copy link

any plan to support ocr pdf files?

@smcnally
Copy link

smcnally commented Nov 2, 2019

This script is very helpful - thank you for sharing it.

I'm running macos 10.13.6 and needed to modify ocr-shot.sh to specify

--tessdata-dir /usr/local/share/tessdata/
vs
--tessdata-dir /usr/local/share/

ocr-shot.sh is now working as designed.

two notes:

  • I'm getting

"Warning: Invalid resolution 0 dpi. Using 70 instead."

macos-native and Firefox screenshots are 72 dpi. I'll look into where I can set that explicitly.

  • I presumed ocr-shot.sh's file meta additions would 1) change files' modified timestamp and 2) appear in either Finder or Preview Get Info views. Neither is the case. Adding the extracted text to files' Comments or Annotations could be useful.

@tianchentong
Copy link

why cannot Automator run the workflow, asserting "line 4 tesseract command not found"?

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