Script to Batch open PDFs in Preview and Export them again as PDFs

The subject might sound odd, but here’s why I’m needing to do this. I am batch generating PDF files from Filemaker Pro 14. The resulting PDFs open fine in Preview, but not with Acrobat Reader/Pro (resulting 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 known 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.

Richard

Mac 10.10.5, Preview 8.0, Filemaker Pro Advanced 14
iMac (Retina 5K, 27-inch, Late 2014), OS X Yosemite (10.10.5)

Try saving this as an application, and drag the PDFs over it:

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "Quartz"

on open fileList -- drag list of PDFs to be updated
	repeat with aFile in fileList
		set theURL to (current application's class "NSURL"'s fileURLWithPath:(POSIX path of aFile))
		set newName to (theURL's lastPathComponent()'s stringByDeletingPathExtension()'s stringByAppendingString:"+")
		set newURL to ((theURL's URLByDeletingLastPathComponent()'s URLByAppendingPathComponent:newName)'s URLByAppendingPathExtension:"pdf")
		set thePDFDoc to (current application's PDFDocument's alloc()'s initWithURL:theURL)
		(thisPDFDoc's writeToURL:newURL)
	end repeat
	display dialog "Finished" buttons {"OK"}
end open

Thanks, Shane.

When I run PDFs over the app, I get the error “The variable posixPath is not defined. (-2753)”. But, if I read your script correctly (and theres’ a good chance I’m wrong!) it will basically add a “.pdf” extension to each file after removing it first? I don’t think that will do what I need, which is for each PDF to be opened by the Preview app, and from there Exported as a PDF. There’s some “magic” in that process that then makes the PDFs open in Acrobat without error. I’ve manually tried deleting and then adding back the extension .pdf, but it makes no difference.

I appreciate your help

Sorry – I’ve editied it to fix the problem.

No, it does a bit more than that. Try the edited version.

Thanks for you patience! This time, I get “The variable thisURL is not defined. (-2753)”

Sorry again – I’ve edited it again.