OCR to Clipboard
What It Does¶
This script enables you to extract text from visual media like games, manga, videos and copies the result to your clipboard.
You can then quickly look the words up through spotlight or through Yomichan by using use a clipboard inserter, or apps like Game2Text to facilitate your flashcard creation workflow.
Install Tesseract, the OCR engine.
If you have brew installed, you can just type the following commands to Terminal.
brew install tesseract brew install tesseract-lang
Create the Quick Action¶
Launch Automator by pressing Command⌘+Space for Spotlight, typing in automator, and hitting Enter.
Create a Quick Action.
Add a Run AppleScript action by searching for applescript in the upper left search bar and double clicking the Run AppleScript item.
Paste the following script to the action.
set outPath to "/tmp" set tesseractCmd to (do shell script "bash -l -c 'which tesseract'") do shell script "screencapture -i " & outPath & "/untitled.png" do shell script tesseractCmd & " " & outPath & "/untitled.png " & outPath & "/output -l jpn" set the_text to (do shell script "cat " & outPath & "/output.txt") set the clipboard to the_text do shell script "rm " & outPath & "/untitled.png " & outPath & "/output.txt"
Press Command⌘+S and name the action as Capture2Clipboard.
Run the action, draw a selection around some text on your screen and paste the result to a text editor to see if works.
Add a Keyboard Shortcut¶
Go to System Preferences > Keyboard > Shortcuts. Select Services from the sidebar and find the service Capture2Clipboard. Add a shortcut by double clicking (none).
I use the hotkey Command+Control+O (^⌘O).
Command+Control+A-Z are usually available for your custom global shortcuts, with Command+Control+F being a notable exception.
You might also need to go to System Preferences > Security > Privacy > Accessibility and give Automator permission to read keystrokes. This is not required on my Mac running Catalina 10.15.7.
Whenever you press the hotkey, you can select a region to extract text and the result will go to your clipboard.