I have a lovely AppleScript droplet which performs OCR of a PDF file using Adobe Acrobat. I am a pretty good Python programmer but don’t really understand AppleScript. I have a list of all the PDFs on my system that need to be OCRed. It would be really annoying to have to drag each on one top of the script. I’d like to have either a small python program that has the droplet process each script, or else I’d like to modify the script to read the textfile and dispense with the dropping stuff.
I tried using osascript to open the PDFs with a single test:
tell application "OCRIt-Acrobat"
open alias "imac3:Users:vy32:FFJ.pdf"
end tell
And I got this lovely error:
31:103: execution error: OCRIt-Acrobat got an error: alias "imac3:Users:vy32:FFJ.pdf" of «script» doesn’t understand the open message. (-1708)
Well, that’s not too helpful.
Anyone know what I should do?
Here is OCRIt-Acrobat, in all its glory:
property mytitle : "ocrIt-Acrobat"
-- Modified from a script created by Macworld http://www.macworld.com/article/60229/2007/10/nov07geekfactor.html
-- I am called when the user open the script with a double click
on run
tell me
activate
display dialog "I am an AppleScript droplet." & return & return & "Please drop a bunch of PDF files onto my icon to batch OCR them." buttons {"OK"} default button 1 with title mytitle with icon note
end tell
end run
-- I am called when the user drops Finder items onto the script icon
-- Timeout of 36000 seconds to allow for OCRing really big documents
on open droppeditems
with timeout of 36000 seconds
try
repeat with droppeditem in droppeditems
set the item_info to info for droppeditem
tell application "Adobe Acrobat Pro"
activate
open droppeditem
end tell
tell application "System Events"
tell application process "Acrobat"
click the menu item "Recognize Text Using OCR..." of menu 1 of menu item "OCR Text Recognition" of the menu "Document" of menu bar 1
try
click radio button "All pages" of group 1 of group 2 of group 1 of window "Recognize Text"
end try
click button "OK" of window "Recognize Text"
end tell
end tell
tell application "Adobe Acrobat Pro"
save the front document with linearize
close the front document
end tell
end repeat
-- catching unexpected errors
on error errmsg number errnum
my dsperrmsg(errmsg, errnum)
end try
end timeout
end open
-- I am displaying error messages
on dsperrmsg(errmsg, errnum)
tell me
activate
display dialog "Sorry, an error occured:" & return & return & errmsg & " (" & errnum & ")" buttons {"Never mind"} default button 1 with icon stop with title mytitle
end tell
end dsperrmsg
Thanks!
By calling
aliasyou are essentially making a direct call to a file at that exact path, If the alias can’t be found, then Applescript throws an error. If you are reading a list from a text file, then an error could occur in the generation of that list you aren’t checking. At minimum, you need to use System Events to make sure you are working with a valid file:I have this template I use for processing any number of files or folders that were dropped on a droplet. As long as all your target files are in the same folder hierarchy, you won’t need that external list of files: