Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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

Reply
14 replies

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

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

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 (existsfolder "My PDFs" of desktop) then

makenewfolderatdesktopwith 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 openthisFile

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

clickmenu 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}

keystrokereturn

delay 0.25

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

clickbutton "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

closetheDocument

tell application "Finder" to movefile (name of thisFile) of desktoptotheDestinationFolder with replacing

end repeat

end tell

set volume without output muted


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!

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 (existsfolder "My PDFs" of desktop) then

makenewfolderatdesktopwith 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 openthisFile

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

clickmenu 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

keystrokereturn

delay 0.25

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

clickbutton "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

closetheDocument

tell application "Finder" to movefile (name of thisFile) of desktoptotheDestinationFolder with replacing

end repeat

end tell

set volume without output muted

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

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

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

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.