Script to auto-rename OCRscanned documents based on content

Is such a thing possible?

For an example, I usually scan my bills into DT with my Fujitsu ScanSnap, one after the other and wind up with a folder full files entitled ScanSnap-001.pdf, ScanSnap-002.pdf, etc…Many of these bills are reoccurring monthly bills, and many of them have big, bold OCR-friendly headings that are always recognized accurately at the top of the bill…

Is it possible manually select a batch of files in DT, and then have a script search for a predefined list of OCR words/phrases to act as triggers, which if matched correctly, would automatically rename the file to a predetermined file name and then perhaps append a sequence number?

SO that If I had just scanned all of my monthly bills and there is one from “Time-Warner Cable Company” with that exact text appearing at the top of the document, then the when I run the script on this document, it could automatically be renamed from SnapScan-001.pdf to “Time-Warner Cable Company Bill_029”

This is of course possible. Basically such a script would look like this:


tell application "DEVONthink Pro"
	set theSelection to the selection
	repeat with theRecord in theSelection
		set theText to plain text of theRecord
		set theName to name of theRecord
		-- Add code to look for & extract titles and to update theName here
		set name of theRecord to theName
	end repeat
end tell

Seeing as how my applescripting ability is entirely non-existent, do you think you could help me figure out how to fill in the code for the section you referenced as:

I would be sincerely grateful if you could help me out with this, as it would save me hours and hours if I could get such a functionality added to DT…

Thanks, and I look forward to hearing back from you!

Here’s a working but still quite basic example:


property pTitles : {"Time Warner", "Devon Technologies"}

tell application "DEVONthink Pro"
	set theNum to 1
	set theSelection to the selection
	repeat with theRecord in theSelection
		set theText to plain text of theRecord
		repeat with pTitle in pTitles
			if theText contains pTitle then
				set theName to pTitle & "-" & (theNum as string)
				set name of theRecord to theName
				set theNum to theNum + 1
				exit repeat
			end if
		end repeat
	end repeat
end tell

You have to add your desired strings to the array pTitles.

1 Like

So I can’t get this code to work. Any advice? I’m not a programmer and don’t know what or how to really use the script. I am hoping to rename files based on their group and creation date so if I drop a document into a group I want it to automatically rename itself to that group with the date appended.

Example: I’ve got a costco receipt I’ve scanned in and OCR’d via a Fujitsu Scansnap. I want that file to automatically get filed into the correct folder and renamed as I choose. I know apps like Hazel and a Better Rename can handle this, but a super powerful program like DevonThink cannot? or Do I have to script it or something? Any help greatly appreciated.

1 Like