Benches macOS

8

How to run pdftk in a shell script

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

Hi all,

I would like to have a finder action "watermark pdf". I have found a good unix tool called pdftk, you find it under

http://www.pdftk.com .

It can apply a background picture to a pdf file just simple with this command.

pdftk input.pdf background watermark.pdf output output.pdf

But I want to have that as an finder action. So I tried automator.

First command get selected finder item

Second command run shell script

Than I need the shell script doint this. I have to pass the selected finder item to the command pdftk as first argument, watermark is a fixed file, output should be the name of the input file with an attached "-TM" in the same directory as the input file.

Anybody knows how?

Thanks.

Fernando

8 replies

Sign in or join Beachball Help to reply.

  1. 0

    deivy

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Fernando,

    you should ask this question on the Automator discussion.

    However, you basically need to actions, one, a Finder action that gets your selection(s).

    Second the shell script that creates the new file with the (fixed) water mark.

    Since you are talking about Automator, you should know how o pass the parameters. I believe however, that it is kind of easy to follow the steps.

    Truly, I've never used Automator I use mostly AppleScript.

    This by the way, would be as simple as an Automator action.

    deivy

  2. 0

    Fernando Schlottmann

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hi Deivy,

    thank You for you answer. Yes, I have asked this in the automator discussion, but the problem reduces to a shell scripting problem. I already know how to get the parameter from the finder action to the shell script. It's just about string handling in a shell script. The main question is: How do I combine two strings, one is $input and the other is "-TM" into my $output variable.

    Best regards,

    Fernando

  3. 0

    deivy

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    OK, Fernando,

    I went to the site and liked pdftk.

    You have it working with 10.4.2 I gather.

    Alright, to your question, if I understand correctly what you want, is in an AppleScript form

    Finder script

    tell application "Finder"

    set pdffile to choose file of type "PDF "

    set folderofPDFfile to (container of pdffile) as string

    set folderofPDFfile to POSIX path of folderofPDFfile as string

    set znome to text 1 thru -5 of (get name of pdffile)

    set znome to folderofPDFfile & znome & "-TM.pdf"

    --znome is (original path to file -.pdf + -TM.pdf).

    end tell

    Finder script ends

    pass znome and pdffile as parameters to this script.

    Shell script

    set zpdftk to "path to the pdftk command" as string --if you need

    do shell script zpdftk & pdffile & " path to background.pdf " & " path to watermark.pdf " & " cat output " & znome

    Shell script ends

    I assume you are done!

    deivy

  4. 0

    Fernando Schlottmann

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hi Deivy,

    thank You so much. I'll try it. I don't understand the single commands, but I'll see.

    Thanks,

    Fernando

  5. 0

    deivy

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Well if you want, you can try it as an AppleScript script then make it into an Automator action.

    here is the script:

    tell application "Finder"

    set pdffile to choose file of type "PDF "

    set folderofPDFfile to (container of pdffile) as string

    set folderofPDFfile to POSIX path of folderofPDFfile as string

    set znome to text 1 thru -5 of (get name of pdffile)

    set znome to folderofPDFfile & znome & "-TM.pdf"

    --znome is (original path to file -.pdf + -TM.pdf).

    end tell

    set zpdftk to "/usr/local/bin/pdftk" --if you need

    do shell script zpdftk & pdffile & " path to background.pdf " & " path to ¬

    watermark.pdf " & " cat output " & znome

    You have to put the path to the watermark.pdf file as a posix path in the script above.

    deivy

  6. 0

    deivy

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    let me correct the script. I've tried the following script (that is , this is tested)

    tell
    application "Finder"


    set
    pdffile
    to
    choose file
    of type {"PDF ",
    missing value , "PDF Document"}


    set
    folderofPDFfile
    to (
    container
    of
    pdffile )
    as
    string


    set
    folderofPDFfile
    to
    POSIX path
    of
    folderofPDFfile
    as
    string


    set
    znome
    to
    text 1
    thru -5
    of (
    get
    name
    of
    pdffile )


    set
    pdffile
    to
    POSIX path
    of
    pdffile


    set
    znome
    to
    folderofPDFfile &
    znome & "-TM.pdf"

    --znome is (original path to file -.pdf + -TM.pdf).


    end
    tell

    set
    zpdftk
    to "/usr/local/bin/pdftk" --if you need

    do shell script
    zpdftk & " " &
    pdffile & " /Users/deivy/Desktop/watermark.pdf " & " cat output " &
    znome

    However, what I get both via script or command line is one document with two pages, not 1 page with a watermark.

    deivy

  7. 0

    Fernando Schlottmann

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hi Deivy,

    you missed the argument "background" after inputfile and before watermarkfile. The command you have used just combines the two pdf-files into a new pdf file after one another.

    Thank you

    Fernando

  8. 0

    deivy

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    You are right. I did not see any mention to it in the examples.

    But pdftk --help did mention it.

    Also, I should take the "cat" if I only want to watermark.

    Thanks for letting us know about pdftk...

    deivy