Skip to content

Instantly share code, notes, and snippets.

@incandescentman
Created June 3, 2020 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save incandescentman/e073eb93ca55faeaaf5d06675f4e322b to your computer and use it in GitHub Desktop.
Save incandescentman/e073eb93ca55faeaaf5d06675f4e322b to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# Siddharth Dushantha 2020
#
# https://github.com/sdushantha/bin
#
TEXT_FILE="/tmp/ocr.txt"
IMAGE_FILE="/tmp/ocr.png"
# Check if the needed dependencies are installed
dependencies=(tesseract screencapture -i pbcopy)
for dependency in "${dependencies[@]}"; do
type -p "$dependency" &>/dev/null || {
# The reason why we are sending the error as a notification is because
# user is most likely going to run this script by binding it to their
# keyboard, therefor they cant see any text that is outputed using echo
osascript -e "ocr" "Could not find '${dependency}', is it installed?"
exit 1
}
done
# Take screenshot by selecting the area
screencapture -i -s "$IMAGE_FILE"
# Get the exit code of the previous command.
# So in this case, it is the screenshot command. If it did not exit with an
# exit code 0, then it means the user canceled the process of taking a
# screenshot by doing something like pressing the escape key
STATUS=$?
# If the user pressed the escape key or did something to terminate the proccess
# taking a screenshot, then just exit
[ $STATUS -ne 0 ] && exit 1
# Do the magic (∩^o^)⊃━☆゚.*・。゚
# Notice how I have removing the extension .txt from the file path. This is
# because tesseract adds .txt to the given file path anyways. So if we were to
# specify /tmp/ocr.txt as the file path, tesseract would out the text to
# /tmp/ocr.txt.txt
tesseract "$IMAGE_FILE" "${TEXT_FILE//\.txt/}"
# Check if the text was detected by checking number
# of lines in the file
LINES=$(wc -l < $TEXT_FILE)
if [ "$LINES" -eq 0 ]; then
osascript -e "ocr" "no text was detected"
exit 1
fi
# Copy text to clipboard
pbcopy -selection clip < "$TEXT_FILE"
# Send a notification with the text that was grabbed using OCR
osascript -e "ocr" "$(cat $TEXT_FILE)"
# Clean up
# "Always leave the area better than you found it"
# - My first grade teacher
rm "$TEXT_FILE"
rm "$IMAGE_FILE"
@sdushantha
Copy link

sdushantha commented Jun 4, 2020

#!/usr/bin/env bash
#
# Siddharth Dushantha 2020
# 
# https://github.com/sdushantha/bin
#

TEXT_FILE="/tmp/ocr.txt"
IMAGE_FILE="/tmp/ocr.png"

# Check if the needed dependencies are installed
dependencies=(tesseract screencapture pbcopy)
for dependency in "${dependencies[@]}"; do
    type -p "$dependency" &>/dev/null || {
        # The reason why we are sending the error as a notification is because
        # user is most likely going to run this script by binding it to their
        # keyboard, therefor they cant see any text that is outputed using echo
        osascript -e 'display notification "Could not find '${dependency}', is it installed?" with title "ocr"'
        exit 1
    }
done

# Take screenshot by selecting the area
screencapture -i -s "$IMAGE_FILE"

# Get the exit code of the previous command.
# So in this case, it is the screenshot command. If it did not exit with an
# exit code 0, then it means the user canceled the process of taking a
# screenshot by doing something like pressing the escape key
STATUS=$?

# If the user pressed the escape key or did something to terminate the proccess
# taking a screenshot, then just exit
[ $STATUS -ne 0 ] && exit 1

# Do the magic (∩^o^)⊃━☆゚.*・。゚
# Notice how I have removing the extension .txt from the file path. This is
# because tesseract adds .txt to the given file path anyways. So if we were to
# specify /tmp/ocr.txt as the file path, tesseract would out the text to 
# /tmp/ocr.txt.txt
tesseract "$IMAGE_FILE" "${TEXT_FILE//\.txt/}"

# Check if the text was detected by checking number
# of lines in the file
LINES=$(wc -l < $TEXT_FILE)
if [ "$LINES" -eq 0 ]; then
    osascript -e 'display notification "no text was detected" with title "ocr"'
    exit 1
fi

# Copy text to clipboard
pbcopy -selection clip < "$TEXT_FILE"

# Send a notification with the text that was grabbed using OCR
osascript -e 'display notification "'"$(cat $TEXT_FILE)"'" with title "OCR"'

# Clean up
# "Always leave the area better than you found it" 
#                       - My first grade teacher
rm "$TEXT_FILE"
rm "$IMAGE_FILE"

EDIT: fixed issue mentioned by @ThatIsAPseudo, where the notification would not show the grabbed text.

@ThatIsAPseudo
Copy link

Changed line 55 to osascript -e 'display notification "'"$(cat $TEXT_FILE)"'" with title "OCR"' in order to make the notification display the grabbed text, otherwise it displays $(cat $TEXT_FILE).

Before :
Screenshot before
After :
Screenshot after

See osascript using bash variable with a space (Stackoverflow)

@sdushantha
Copy link

@ThatIsAPseudo Thanks for letting us know, I will update the code snippet I posted earlier

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