osx - Applescript + pdfpen for ocr on multiple files: how to move on when pdfpen freezes

08
2014-07
  • user2818584

    I'm using the following AppleScript to batch-process pdfs for ocr:

    tell application "Finder"
        set target_folder to target of front Finder window
    repeat with i from 1 to count (every folder of target_folder)
        set current_folder to folder i of target_folder
        set current_batch to (document files of entire contents of current_folder whose name ends with "pdf")
        repeat with i from 1 to count (every item of current_batch)
            set current_pdf to item i of current_batch
            tell application "PDFpen"
                activate
                open current_pdf as alias
                tell document 1
                    ocr
                    repeat while performing ocr
                        delay 1
                    end repeat
                    delay 1
                    close with saving
                end tell
            end tell
        end repeat
    end repeat
    end tell
    

    While the script is perfectly functional, I have the following problem. After processing a number of files (say, between 10 and 50), pdfpen sometimes freezes during the ocr (progress-bar stops close to the end). The only way out is to halt the AppleScript and then ForceQuit pdfpen. Afterwards, I can restart where I ended, and the pdf on which pdfpen froze is not processed correctly. Conclusion: the freezing is independent of the pdf itself.

    Is there a way to:

    • make AppleScript notice that pdfpen froze,
    • forcequit pdfpen
    • and then restart the process with the last unprocessed file

    I have an idea of how I could deal with the last part by having the script keep track of its progress, and could probably achieve the second point with

    do shell script "killall pdfpen"
    

    but am totally lost when it comes to the first point. I've experimented with "timeout" declarations, but this doesn't seem to make any difference.

    Any suggestions on how to deal with this?

    (I need to process more than 150 folders that contain each between 5 and 20 pdf-files, so I'd rather avoid doing the quitting and restarting manually)

  • Answers
    Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

    Related Question

    osx - How do You Open the Contents of a Folder with AppleScript?
  • Orion751

    I am currently using this UNIX shell command in my script file:

    do shell script "open ~/Dropbox/Media/VisionBoard/*"
    

    Is there another way to do it and if so would it be any better or different? I'm still new to programming, so please be gentle.


  • Related Answers
  • ischeriad
    tell application "Finder"
        set myFolder to ((home as text) & "Dropbox:Media:VisionBoard") as alias
        set myFiles to (every item of myFolder) as alias list
        open myFiles
    end tell
    

    Instead of "every item" you can also say "every file" or "every folder".