Script to OCR image coming from clipboard

I searched the Automation forum with a few different queries but could not find an existing script. Is it possible to script this process? Take as input a jpeg image from the system clipboard, apply OCR conversion via DTP, set the clipboard with the returned text. For simplicity, I’m hopeful that temporary objects won’t need to be created in the DTP database. If possible, I would prefer that DTP simply remain in the background.

Why? I occasionally have a PDF with an existing text layer but the text selection is defective – discontiguous parts of the document are selected when I drag. Sometimes the desired part cannot be selected no matter how I tinker with the selection range. But in these cases it is overkill to OCR the entire PDF. Instead, it would be helpful to screenshot the offending text block (I’m using SnagIt) and then convert it to text, ready for pasting elsewhere. It is too slow to manually switch to DTP, create a new entry with New With Clipboard, then OCR, and so forth.


1 Like

Create a folder inside DT. Then a Smart Rule that will OCR anything dropped in that folder. I cannot give you more precise details but remember someone posted or explained here how to do this.

Following @rfog’s idea here’s a Smart Rule that

  • does OCR
  • copies text
  • moves the record to trash
  • moves the OCR result to trash

You could:

  • Create a Smart Rule with this script
  • Create an indexed group
  • In Snagit Preferences > Outputs add the path of the indexed group

This way you could get the text copied without activating DEVONthink.

:warning: Caution:

This script moves everything from the Smart Rule’s target group into the trash.

-- OCR, copy text and move to trash

on performSmartRule(theRecords)
	tell application id "DNtp"
			repeat with thisRecord in theRecords
				set thisPath to (path of thisRecord)
				set thisGroup to (parent 1 of thisRecord)
				set newRecord to ocr file thisPath to thisGroup
				set newRecord_Text to plain text of newRecord
				set the clipboard to newRecord_Text
				display notification "Copied"
				move record thisRecord to trash group of database of thisRecord
				move record newRecord to trash group of database of newRecord
			end repeat
		on error error_message number error_number
			if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		end try
	end tell
end performSmartRule

1 Like

Uhhh …

Why don’t you use Snagit’s menu Edit > Grab text (I think that’s the english name)?

Or simply grab the text directly? I have a Snagit action in the menu bar that grabs text of the selected area.

See How to Capture Text | Snagit Tutorial | TechSmith

Ouch, embarrassed!! I use many of Snagit’s image editing features, but never noticed the Grab Text command. That is the obvious solution.

Nevertheless, the applescript implementations for DTP will be educational.


1 Like