I wanted to learn more about using the Vision framework to perform OCR on images and decided to create this thread with some of my test scripts. As a starting point, I was fortunate to find a script written by Stephen Kaplan on the Stackoverflow forum, which can be found here.
The following script gets text from a single image file:
use framework "Foundation"
use framework "Vision"
use scripting additions
set theFile to POSIX path of (choose file of type {"public.image"})
set theText to getText(theFile)
on getText(theFile)
set theFile to current application's |NSURL|'s fileURLWithPath:theFile
set requestHandler to current application's VNImageRequestHandler's alloc()'s initWithURL:theFile options:(missing value)
set theRequest to current application's VNRecognizeTextRequest's alloc()'s init()
requestHandler's performRequests:(current application's NSArray's arrayWithObject:(theRequest)) |error|:(missing value)
set theResults to theRequest's results()
set theArray to current application's NSMutableArray's new()
repeat with aResult in theResults
(theArray's addObject:(((aResult's topCandidates:1)'s objectAtIndex:0)'s |string|()))
end repeat
return (theArray's componentsJoinedByString:linefeed) as text
end getText