Skip navigation

Need help filtering for PDF's in script.

426 Views 10 Replies Latest reply: Jan 15, 2012 5:10 AM by larsen67 RSS
PJJPtx Calculating status...
Currently Being Moderated
Jan 13, 2012 6:26 AM

My script to OCR works fined until I tried to filter for PDF's. Some of my folders have other files and it will throw an error if I try to pass a non-PDF to PDFPen.  When I added

 

   tell application "System Events"

        set theFiles to every file of folder NameOfOuterFolder whose name does not start with "." and (file type is "PDF")

    end tell

 

To the script below, I thought it would filter, but something it wrong and it throws an error.

 

try

    set the log_text to "====================" & return & "OCR Process beginning at: " & (get (time string of (current date))) & " on " & (get (current date)) & return

    write_progress_log(the log_text)

end try

 

tell application "Finder"

    set theFolder to (choose folder with prompt "Pick the folder containing the files to process:") as string

    set theNameOfFolder to name of folder theFolder

    set theFiles to every file in folder theFolder

    set theFolders to every folder in folder theFolder

    my ProcessFiles(theNameOfFolder, theFiles)

    my ProcessFolders(theFolders)

end tell

 

try

    set the log_text to return & "OCR Process completed at: " & (get (time string of (current date))) & " on " & (get (current date)) & " ===================="

    write_progress_log(the log_text)

end try

 

tell application "Finder"

    activate

    set the progress_log to ((path to desktop) as text) & "OCR Progress Log.txt"

    select file progress_log

    open selection

end tell

 

to ProcessFolders(theFolders)

    tell application "Finder"

        repeat with thisFolder in theFolders

            set theNameOfFolder to name of thisFolder

            set theFiles to every file in thisFolder

            set theFolders to every folder in thisFolder

            my ProcessFiles(theNameOfFolder, theFiles)

            my ProcessFolders(thisFolder)

 

            --set the log_text to "  Completed " & the name of thisFolder & " for files " & (get (time string of (current date)))

            --my write_progress_log(the log_text)

 

        end repeat

    end tell

end ProcessFolders

 

to ProcessFiles(NameOfOuterFolder, theFiles)

 

    tell application "System Events"

        set theFiles to every file of folder NameOfOuterFolder whose name does not start with "." and (file type is "PDF")

    end tell

 

    tell application "Finder"

        repeat with thisFile in theFiles

 

            tell application "PDFpen"

                open thisFile as alias

 

                set the the log_text to "     OCR beginning for file " & the name of thisFile

                my write_progress_log(the log_text)

 

                tell document 1

                    ocr --simple

                    repeat while performing ocr

                        delay 1

                    end repeat

                    delay 1

                end tell

                save document 1

                close document 1

 

                set the log_text to "     OCR complete for file " & the name of thisFile

                my write_progress_log(the log_text)

 

            end tell

 

        end repeat

    end tell

end ProcessFiles

 

on write_progress_log(this_log)

    set the progress_log to ((path to desktop) as text) & "OCR Progress Log.txt"

    try

        open for access file the progress_log with write permission

        write (this_log & return) to file the progress_log starting at eof

        close access file the progress_log

    on error

        try

            close access file the progress_log

        end try

    end try

end write_progress_log

 

iMac, Mac OS X (10.7.2)
  • Bernard Harte Level 4 Level 4 (3,025 points)
    Currently Being Moderated
    Jan 13, 2012 6:44 AM (in response to PJJPtx)

    It would be helpful if you told us the resulting error.

  • Bernard Harte Level 4 Level 4 (3,025 points)
    Currently Being Moderated
    Jan 13, 2012 9:42 AM (in response to PJJPtx)

    I presume the error is being thrown on this line:

     

                        set theFiles to every file of folder NameOfOuterFolder whose name does not start with "." and (file type is "PDF")

     

    To start with, AFAIK System Events doesn't know about folders and files, that's the province of the Finder.  I think you can simply move this line inside the Tell app "Finder" block below it.  Whether that will move you much further forward, I am not sure!

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Jan 13, 2012 9:51 AM (in response to Bernard Harte)

    Bernard, that's wrong - please read the System Events dictionary (Disk-Folder-File suite).

     

    PJ, the problem is that you're just giving it a folder name without context.  you're saying:

     

    set theFiles to every file of folder NameOfOuterFolder

     

    when you should be saying something like:

     

    set theFiles to every file of folder NameOfOuterFolder of folder "nextLevel" of disk "my disk"

     

    what you've done is the equivalent of telling someone to tell Bob that his wife called: how does that someone know which Bob you mean?  People are smart enough to read between the lines and figure it out; computers aren't.

  • Bernard Harte Level 4 Level 4 (3,025 points)
    Currently Being Moderated
    Jan 13, 2012 9:56 AM (in response to twtwtw)

    tw - apologies for the error!  I didn't check and was not certain - hence the AFAIK!

     

    What, however, is the benefit of using System Events vs Finder for this - especially when there's a Finder block immediately beneath?

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Jan 13, 2012 10:28 AM (in response to Bernard Harte)

    I haven't looked at the script closely (just glanced at it to answer the question) but speaking generally, System Events is usually a better choice than the Finder.  The Finder is often busy doing other things, and was retrofitted with applescript rather than built to use it; that means scripting the Finder can be slow and can sometimes cause the script to hang.  System Events was designed specifically to be scriptable so you get much better response time and fewer problems. 

     

    There are a few things you can only do through the Finder (get the entire (recursive) contents of a folder, get/set the target of an alias, duplicate file system objects, set label colors...) and sometimes the Finder is more convenient (it's always there, so no need to wait for System Events to launch), but I generally try to use system events where possible just because it's surer and snappier.

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Jan 13, 2012 11:01 AM (in response to PJJPtx)

    well you could, but that's not what you're trying to do (and you'd have to do that a different way anyway).  What I suggest you do is pass an alias to the folder (rather than the folder name) and then get the pdfs directly.  that would look something like:

     

    try

      -- write logs

    end try

     

    set theFolder to (choose folder with prompt "Pick the folder containing the files to process:") as string

    my ProcessFiles(theFolder)

    my ProcessFolders(theFolders)

     

    try

      --write logs

    end try

     

    --...

     

    to ProcessFiles(theFolder)

              tell application "System Events"

                        set theFiles to every file of folder theFolder whose name does not start with "." and (file type is "PDF")

              end tell

     

              tell application "Finder"

                        repeat with thisFile in theFiles

      -- ...

                        end repeat

              end tell

    end ProcessFiles

  • larsen67 Calculating status...
    Currently Being Moderated
    Jan 15, 2012 5:10 AM (in response to PJJPtx)

    Shouldn't file type's be a 4 character code and your "PDF" should be "PDF " an important last space…

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.