How can I use Automator to File>Duplicate an open PDF file?

Discussion in 'Mac Programming' started by macsense, Jun 4, 2017.

  1. macsense macrumors newbie

    Joined:
    Feb 27, 2008
    #1
    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.
     
  2. kryten2, Jun 5, 2017
    Last edited: Jun 5, 2017

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    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
     
  3. benwiggy macrumors 68020

    Joined:
    Jun 15, 2012
    #3
    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.
     
  4. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #4
    Another solution might be to use pdftk on the command line:

    https://www.pdflabs.com/tools/pdftk-server/

    It can do some repairing of PDFs. Of course, if you really need it in an Automator action, you can use Run Shell Script.
     

Share This Page