Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

macsense

macrumors newbie
Original poster
Feb 27, 2008
4
7
I have several PDF files that are corrupted in some way; however, they will open in Preview. I have discovered that I can fix them (for what I need) simply by opening the file in Preview, selecting Duplicate from the File menu, renaming the file the same as the original and then replacing the original file when asked.

Is there a way I can use Automator to do all of this for me? It needs to be duplicated from within Preview to fix the problem with the PDF's as simply duplicating the file in the Finder doesn't fix the problem.

I'm running Sierra 10.12.5. Thank you.
 
You can try this :

Code:
-- Used by me 05/06/2017 -- let's try something
-- Modified by YK 2017/01/21
-- Modified 2016-07-18 by Takaaki Naganoya
-- Original By Shane Stanley

use scripting additions
use framework "Foundation"
use framework "Quartz"
use framework "QuartzCore"

set inFile to (choose file of type {"pdf"} with prompt "Choose your PDF file:")

-- make URL of the original PDF
set inNSURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of inFile)
set theDoc to current application's PDFDocument's alloc()'s initWithURL:inNSURL

(theDoc's writeToURL:inNSURL)

Note: Test this on copies of your actual files!

Info : http://macscripter.net/viewtopic.php?id=45476
 
Last edited:
Bizarrely, Preview has almost no AppleScript and Automator support. So you will need to use something like kryten2's lovely little AppleScript that uses Quartz directly to re-write the PDF file.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.