Merge two Automator PDF scripts?

Discussion in 'Mac Programming' started by CliftonHighgrov, Aug 20, 2016.

  1. CliftonHighgrov macrumors regular

    CliftonHighgrov

    Joined:
    Oct 1, 2014
    Location:
    United Kingdom
    #1
    I have two scripts that I use for manipulating PDFs, and I'd like to merge them into a single one. However, I struggle to get the output from the first command to act as input for the second.

    Script one simply flattens the PDF with Ghostscript and modifies the file extension to differentiate the output from the original:
    Code:
    /usr/local/bin/gs -sDEVICE=pdfwrite -dPDFSETTINGS=/default -dNOPAUSE -dQUIET -dBATCH -sOutputFile="$@.flat.pdf" "$@"
    Script two then locks the flattened PDF so that it can only be edited by those with the password:
    Code:
    /usr/local/bin/pdftk "$@" output "$@.locked.pdf" owner_pw foo
    I'd like this to be a single script. Can someone assist?

    Also, the use of the plain text password in the second script is unfortunate, but I don't yet know how to pull the password from Keychain Access. I know the CLI tools are there, but haven't successfully implemented them yet :( . If someone offered a clear example of implementing this that'd be great.
     
  2. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #2
    Hi,

    I can't seem to get PDFTk to work here, for some reason, so I haven't been able to test this. However, Couldn't you just roll it all together into one script? Something (very roughly) like this...

    Code:
    theInputFile="$@"; theOutputFile=${theInputFile%.*}.flat.pdf ; /usr/local/bin/gs -sDEVICE=pdfwrite -dPDFSETTINGS=/default -dNOPAUSE -dQUIET -dBATCH -sOutputFile="$theOutputFile" "$theInputFile" ; echo /usr/local/bin/pdftk "$theOutputFile" output "${theInputFile%.*}.locked.pdf" owner_pw foo
    
     
  3. CliftonHighgrov thread starter macrumors regular

    CliftonHighgrov

    Joined:
    Oct 1, 2014
    Location:
    United Kingdom
    #3
    Hi,

    Finally got around to testing this and it works. So: thank you.

    There was one little thing: what is the purpose of the "echo" in the final command? I had to remove it to get the script to run correctly.

    I'm probably being greedy, but you wouldn't happen to know how to modify the script to prevent the creation of the intermediate, "${theInputFile%.*}.flat.pdf" file would you? Not a huge deal, as I can just prepend a clean-up command and send it to the Trash, but it'd be the cherry on top :)
     
  4. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #4
    Ah, yes. Sorry. I should have mentioned that. I put the echo there so it'd print the pdftk command rather than actually run it. I did that because my copy of pdftk doesn't seem to be working properly. You were right to remove it.[/QUOTE]

    Someone may correct me, but I don't think pdftk can write to the same file it read from. And if it will, then doing that sort of thing makes me nervous. I'd suggest sticking with the method I've shown. You can always add an extra bit of shell script to delete the intermediate file at the end if necessary.

    regards
     
  5. CliftonHighgrov thread starter macrumors regular

    CliftonHighgrov

    Joined:
    Oct 1, 2014
    Location:
    United Kingdom
    #5
    Aha, understood.

    I think I've learned from experience that pdftk cannot write to the same file it read from, it would explain some issues I had during script testing. So, I'll just use the clean-up script.

    Thanks for the assistance.
     

Share This Page