Zulu

Q: Automator/applescript to batch open PDFs in Preview and Export as PDF

I am batch generating PDF files from Filemaker Pro 14.  The resulting PDFs open fine in Preview, but not with Acrobat Reader/Pro (1 or more blank pages and an error  "An error exists on this page. Acrobat may not display the page correctly. Please contact the person who created the PDF document to correct the problem.").  This is a Filemaker bug (http://forums.filemaker.com/posts/43f4da4247).  Unfortunately, my end users don't use Macs or Preview, so the files need to behave properly with Acrobat.  


I have discovered that one way round this problem is to open each PDF in Preview, then "Export as PDF" with the same file name to overwrite original file.  The files so generated can be opened without error messages in Acrobat.  As I may be generating several hundred PDFs at a time this way, I'm looking for a way to automate the opening of the PDFs and Export as PDF by Preview.  I have perused the discussion groups a little, and tried a few things with Automator/Applescript, but I have very little experience with these tools.  The resulting converted files could either replace the originals or populate another folder so the originals are preserved.  Can anyone help? 

 

Much appreciated.

 

Mac 10.10.5

Preview 8.0

Filemaker Pro Advanced 14

iMac (Retina 5K, 27-inch, Late 2014), OS X Yosemite (10.10.5)

Posted on Sep 27, 2015 3:03 PM

Close

Q: Automator/applescript to batch open PDFs in Preview and Export as PDF

  • All replies
  • Helpful answers

  • by Hiroto,

    Hiroto Hiroto Sep 29, 2015 2:09 AM in response to Zulu
    Level 5 (7,286 points)
    Sep 29, 2015 2:09 AM in response to Zulu

    Hello

     

    You may try something like the following rubycocoa script which will resave (overwrite) specified PDF files via PDFKit.framework.

     

     

    #!/bin/bash
    
    /usr/bin/ruby -w <<'EOF' - *.pdf
    require 'osx/cocoa'
    OSX.require_framework 'PDFKit'
    include OSX
    ARGV.each { |f| PDFDocument.alloc.initWithURL(NSURL.fileURLWithPath(f)).writeToFile(f) }
    EOF
    

     

     

     

    Notes on RubyCocoa.

     

    • Under OS X 10.10, you need to manually install RubyCocoa 1.2.0 which supports Ruby 2.0 or later.

     

    https://rubycocoa.github.io/

    https://github.com/rubycocoa/rubycocoa/releases

     

     

    • Under OS X 10.9, unless you have installed RubyCocoa 1.2.0 for Ruby 2.0, specify the ruby interpreter at

     

    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby

     

     

    • Under OS X 10.5 through 10.8, script should work as is.

     

     

    ---

    You may create an AppleScript wrapper as follows if you prefer to.

     

     

    set aa to choose file of type {"pdf"} with prompt "Choose PDF files to process." with multiple selections allowed
    set args to ""
    repeat with a in aa
        set args to args & a's POSIX path's quoted form & space
    end repeat
    
    do shell script "/usr/bin/ruby -w <<'EOF' - " & args & "
    require 'osx/cocoa'
    OSX.require_framework 'PDFKit'
    include OSX
    ARGV.each { |f| PDFDocument.alloc.initWithURL(NSURL.fileURLWithPath(f)).writeToFile(f) }
    EOF"
    

     

     

    Regards,

    H

  • by Hiroto,

    Hiroto Hiroto Sep 29, 2015 3:26 AM in response to Zulu
    Level 5 (7,286 points)
    Sep 29, 2015 3:26 AM in response to Zulu

    Hello

     

    In case, here's pyobjc version which will not require additional installation under OS X 10.10.

     

     

    #!/bin/bash
    
    /usr/bin/python <<'EOF' - *.pdf
    import sys
    from Quartz.PDFKit import PDFDocument
    from Foundation import NSURL
    for f in [ a.decode('utf-8') for a in sys.argv[1:] ]:
        PDFDocument.alloc().initWithURL_(NSURL.fileURLWithPath_(f)).writeToFile_(f)
    EOF
    

     

     

    And an AppleScript wrapper:

     

     

    set aa to choose file of type {"pdf"} with prompt "Choose PDF files to process." with multiple selections allowed
    set args to ""
    repeat with a in aa
        set args to args & a's POSIX path's quoted form & space
    end repeat
    
    do shell script "/usr/bin/python <<'EOF' - " & args & "
    import sys
    from Quartz.PDFKit import PDFDocument
    from Foundation import NSURL
    for f in [ a.decode('utf-8') for a in sys.argv[1:] ]:
        PDFDocument.alloc().initWithURL_(NSURL.fileURLWithPath_(f)).writeToFile_(f)
    EOF"
    

     

     

    Regards,

    H

  • by Pierre L.,Helpful

    Pierre L. Pierre L. Sep 29, 2015 9:45 AM in response to Zulu
    Level 5 (4,484 points)
    Sep 29, 2015 9:45 AM in response to Zulu

    You might also want to try the following GUI script as an alternative. Just select the PDF files you want to modify (which may be anywhere except on the desktop if you want them to be preserved) and launch the script from the Script Editor window. See this web page about using GUI Scripting.

     

    set volume with output muted

    tell application "Finder"

         set theSelection to selection as alias list

         if not (exists folder "My PDFs" of desktop) then

               make new folder at desktop with properties {name:"My PDFs"}

         end if

         set theDestinationFolder to folder "My PDFs" of desktop

    end tell

    tell application "Preview"

         launch

         activate

         repeat with thisFile in theSelection

               set theDocument to open thisFile

               tell application "System Events" to tell process "Preview"

                     click menu item "Export as PDF…" of menu 1 of menu bar item "File" of menu bar 1

                     repeat until sheet 1 of window 1 exists

                     end repeat

                     keystroke "d" using {command down}

                     keystroke return

                     delay 0.25

                     if (button "Replace" of sheet 1 of sheet 1 of window 1) exists then

                           click button "Replace" of sheet 1 of sheet 1 of window 1

                     end if

                     repeat until enabled of menu item 1 of menu 1 of menu bar item 2 of menu bar 1

                     end repeat

               end tell

               close theDocument

               tell application "Finder" to move file (name of thisFile) of desktop to theDestinationFolder with replacing

         end repeat

    end tell

    set volume without output muted


     

  • by Zulu,

    Zulu Zulu Sep 29, 2015 9:50 AM in response to Pierre L.
    Level 1 (5 points)
    Sep 29, 2015 9:50 AM in response to Pierre L.

    Hi Pierre,

     

    This does exactly what I want!  One little snag though - When exporting as PDF, I think Preview adds ".pdf" to the file name (which already has the extension in .pdf), so the name becomes "filename.pdf.pdf".   For that reason, the script then can't find the output file as its name has changed ( error "Finder got an error: Can’t get file \"2-17871_2015.9.27.13.31.pdf\" of desktop." number -1728 from file "2-17871_2015.9.27.13.31.pdf" of desktop).  Any help appreciated - this is so helpful!

  • by Zulu,

    Zulu Zulu Sep 29, 2015 9:55 AM in response to Hiroto
    Level 1 (5 points)
    Sep 29, 2015 9:55 AM in response to Hiroto

    Thanks!  I will give this a try later today, your help is much appreciated.

  • by Pierre L.,

    Pierre L. Pierre L. Sep 29, 2015 11:14 AM in response to Zulu
    Level 5 (4,484 points)
    Sep 29, 2015 11:14 AM in response to Zulu

    Thanks for your feedback. I've finally found out that the behaviour you are describing happens only when the “Show all filename extensions” is selected in the Finder advanced preferences. I'll see what can be done about that.

  • by Pierre L.,Solvedanswer

    Pierre L. Pierre L. Sep 29, 2015 12:08 PM in response to Zulu
    Level 5 (4,484 points)
    Sep 29, 2015 12:08 PM in response to Zulu

    This new version should do the trick:

     

    set volume with output muted

    tell application "Finder"

         set theSelection to selection as alias list

         if not (exists folder "My PDFs" of desktop) then

               make new folder at desktop with properties {name:"My PDFs"}

         end if

         set theDestinationFolder to folder "My PDFs" of desktop

    end tell

    tell application "Preview"

         launch

         activate

         repeat with thisFile in theSelection

               set theDocument to open thisFile

               tell application "System Events" to tell process "Preview"

                     click menu item "Export as PDF…" of menu 1 of menu bar item "File" of menu bar 1

                     repeat until sheet 1 of window 1 exists

                     end repeat

                     keystroke "d" using {command down}

                     set theValue to value of text field 1 of sheet 1 of window 1

                     if theValue ends with ".pdf.pdf" then

                           set value of text field 1 of sheet 1 of window 1 to text 1 through -5 of theValue

                     end if

                     keystroke return

                     delay 0.25

                     if (button "Replace" of sheet 1 of sheet 1 of window 1) exists then

                           click button "Replace" of sheet 1 of sheet 1 of window 1

                     end if

                     repeat until enabled of menu item 1 of menu 1 of menu bar item 2 of menu bar 1

                     end repeat

               end tell

               close theDocument

               tell application "Finder" to move file (name of thisFile) of desktop to theDestinationFolder with replacing

         end repeat

    end tell

    set volume without output muted

  • by Zulu,Helpful

    Zulu Zulu Sep 29, 2015 12:20 PM in response to Pierre L.
    Level 1 (5 points)
    Sep 29, 2015 12:20 PM in response to Pierre L.

    Pierre,

     

    A thousand thank yous!  This works like a dream and is going to make my workflow so much more streamlined.  And, as a bonus, I'm learning quite a bit about applescript through the process. 

     

    Thanks so much

     

     

    Richard

  • by Pierre L.,

    Pierre L. Pierre L. Sep 29, 2015 12:32 PM in response to Zulu
    Level 5 (4,484 points)
    Sep 29, 2015 12:32 PM in response to Zulu

    My pleasure, Richard. Thanks again for your feedback.

  • by Zulu,

    Zulu Zulu Oct 23, 2015 1:14 PM in response to Hiroto
    Level 1 (5 points)
    Oct 23, 2015 1:14 PM in response to Hiroto

    Awesome script, afraid I only just got to trying it today.  Thank you, Hiroto! 

  • by cooper923,

    cooper923 cooper923 Dec 10, 2015 10:13 AM in response to Hiroto
    Level 1 (0 points)
    Dec 10, 2015 10:13 AM in response to Hiroto

    HI Hiroto,

     

    I'm also running into this same problem on a Windows machine.  Do you have example python code to fix the PDFs that is not dependent on OS X and can run on any platform?  I don't mind installing additional python libraries.

     

    thanks!

  • by Hiroto,

    Hiroto Hiroto Dec 11, 2015 5:15 AM in response to cooper923
    Level 5 (7,286 points)
    Dec 11, 2015 5:15 AM in response to cooper923

    Sorry, I'm not really familiar with python resources. However, I'd be surprised if there's no open source python libiraries to process PDF files.

     

    That's being said, if you're experiencing problems in opening PDF files from FileMaker Pro 14 by the latest Adobe Reader under Windows environment, you should contact FileMaker and let them fix THEIR problem.

     

    Good luck,

    H

  • by cooper923,

    cooper923 cooper923 Dec 11, 2015 10:12 AM in response to Hiroto
    Level 1 (0 points)
    Dec 11, 2015 10:12 AM in response to Hiroto

    Thanks Hiroto.  Can you explain to me the underlying problem with the PDFs generated in Mac OS X FileMaker?  Maybe once I understand that, I can figure out how to solve this issue (I'm familiar with the python libraries for processing PDFs).

     

    thanks!

  • by Hiroto,

    Hiroto Hiroto Dec 11, 2015 11:08 AM in response to cooper923
    Level 5 (7,286 points)
    Dec 11, 2015 11:08 AM in response to cooper923

    I have no idea what is wrong with the pdf generated by FileMaker Pro 14. I used PDFKit framework of OS X in my rubycocoa and pyobjc scripts just because the original poster said resaving the pdf file using OS X's Preview application can fix the problem and I assumed that Preview application and PDFKit framework use the same underlying functions.

     

    By the way, searching the web via Google by the following keywords:

     

    "FileMaker Pro 14" PDF bug

     

    will return some hits, which include -

     

    FileMaker Pro 14 and Save As PDF issues

    https://community.filemaker.com/thread/150363

     

    According to the thread, the issue is caused by a blank portal on the layout.

     

    I don't know what the blank portal is, for I don't use FileMaker...

     

    Hope this may help.

    H