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
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
Posted on Sep 29, 2015 12:08 PM