1

I've got a Python 3 script going that will overlay a particular transparent PDF file on top of another for the purpose of watermarking.

Copying and pasting the file location of each PDF into the script each time I want to watermark something is a bit of a headache though. The whole thing is not very flexible despite its great power.

from pdfrw import PdfReader, PdfWriter, PageMerge

ipdf = PdfReader("/users/me/Desktop/meetingminutes.pdf")
wpdf = PdfReader("/users/me/Desktop/private.pdf")

wmark = PageMerge().add(wpdf.pages[0])[0]

for page in ipdf.pages:
    PageMerge(page).add(wmark).render()

PdfWriter().write('/users/me/Desktop/meetingminutes.pdf', ipdf)

I would ideally like to be able to right click on a given PDF file in Finder and apply the watermark script to it as a service.

I've done a little bit of tinkering already but the most I've been able to figure out on my own is that I need to run the script as it is (right now) I can make an Automator App with the "Run AppleScript" droplet as the first step.

on run {input, parameters}
    do shell script "/usr/local/bin/python3 /users/me/Desktop/script.py

    return input
end run

It'd be wonderful to scale this up to work on any given PDF. The actual "watermark" image PDF would be at a fixed location on the hard drive and the files on which the service is applied would not be renamed or moved/copied elsewhere (ideally).

1 Answers1

1

In Automator, use the "Run Shell Script" action. Normally, you would set the shell drop-down list value to "/usr/bin/python", to use the built-in python 2.7. However, if you select a unix shell, but make sure that your script contains "#! /usr/bin/ env python", or an explicit path to your python3 installation, then it should work.

If you select the other drop-down list "Pass input" to "as arguments", then you can get the filenames of your selected items from sys.argv().

Note that Automator already comes with a watermark action that lets you put one PDF on top of another. (It's also a python script, if you dig deep enough into /System/Library/Automator/)

benwiggy
  • 35,635