How to script PDFPenPro to OCR Selected Finder Items?

Currently, I use Hazel and an AppleScript from Katie Floyd to OCR documents in PDFPenPro. My goal is to do this in KM instead.

I want to set up a macro that invokes a similar script for each selected finder file.

I believe I need to create a macro that uses “For Each Path in Finder Selection” and then execute the AppleScript.

I cannot figure out how to modify the script to work in this context. Katie’s script is below. I assume I need to replace “theFile” with a KM variable?

tell application "PDFpenPro"
   open theFile as alias
   tell document 1
      ocr
      repeat while performing ocr
         delay 1
      end repeat
      delay 1
      close with saving
   end tell
   tell application "PDFpenPro"
      quit
   end tell
end tell

(Pardon this noob question. I am totally new to AppleScript, let alone invoking it from KM. I have tried searching around, but have been unable to figure out how do do this.)

OK I think I answered my own question after a lot of web searches and experimentation.

I had to add a command to Keyboard Maestro Engine to set up the variable needed, and I had to change the script line that opens the file as a POSIX file.

Here is the final script:

-- Modified version of Katie Floyd's script to work with Keyboard Maestro
-- Found at https://katiefloyd.com/blog/automatically-ocr-documents-with-hazel-and-pdfpen
tell application "Keyboard Maestro Engine"
   set theFile to value of variable "Path"
end tell
tell application "PDFpenPro"
   open theFile as POSIX file
   tell document 1
      ocr
      repeat while performing ocr
         delay 1
      end repeat
      delay 1
      close with saving
   end tell
   tell application "PDFpenPro"
      quit
   end tell
end tell

Hey Chris,

Here’s how I’d do that myself.

--------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/11/19 19:50
# dMod: 2016/11/19 21:05
# Appl: PDFpenPro & Finder
# Task: OCR Selected PDF files in the Finder.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @PDFpenPro, @OCR, @Selected, @Files, @Finder
--------------------------------------------------------------------------------

tell application "Finder" to set finderSelectionList to selection as alias list
if length of finderSelectionList = 0 then error "No files were selected in the Finder!"

try
   
   tell application "PDFpenPro"
      if not running then run
      activate
      
      repeat with theFile in finderSelectionList
         
         open theFile
         
         tell document 1
            
            if needs ocr = true then
               
               ocr it
               
               repeat while performing ocr
                  delay 1
               end repeat
               
               close with saving
               
            end if
            
         end tell
         
      end repeat
      
      quit
      
   end tell
   
on error e number n
   set e to e & return & return & "Num: " & n
   if n ≠ -128 then
      try
         tell application (path to frontmost application as text) to set ddButton to button returned of ¬
            (display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
               default button "OK" giving up after 30)
         if ddButton = "Copy Error Message" then set the clipboard to e
      end try
   end if
end try

--------------------------------------------------------------------------------

NOTE to new PDFPenPro users:

PDFPenPro’s default set-up is to automatically open an open dialog when the app is activated.

It also is set up to automatically OCR documents it thinks need it.

These settings can interfere with AppleScripts that employ the app.

I turned these settings off in the prefs as soon as I started trying to script the app.

-Chris

Does this script need to be saved in a specific folder?
Does this script require using KM?
Please provide more details- on what one needs to do to use it.

thanks

@Chris-100's script final script requires KM to be installed and to have a variable called Path that contains the path of the file you want OCRed, but @ccstone's script requires neither, and is the one I would recommend you try first. These scripts can be saved as files wherever you like, and while then can be run from KM, they do not require KM to run. If you're more familiar with KM than you are with AppleScript, I would suggest copying @ccstone's script in its entirety as shown here and pasting it into a KM Execute an AppleScript action. Here's a quick sample macro with no triggers assigned containing the script that you can download to get started:

PDFPenPro – OCR Selected Finder Items.kmmacros (3.1 KB)

1 Like

Hey @cappy2112: I will second what @gglick said:

The script I posted doesn’t work standalone, which seems to be what you want. It’s meant to be run with KM.

Use @ccstone’s script instead, it can be used either way. Had I not already gotten Katie Floyd’s script working, I would have switched to @ccstone’s.

1 Like