How to create a script on MacOS automator to Watermark each file in a folder with a different set of watermarks?

I am trying to create a script to watermark a group a PDF files in a folder, each PDF has to have a unique watermark.


All the PDFs are in one folder, all the watermarks are in .png located in another folder.

I currently have watermark script which I found on Apple Forums, but this was made to watermark all files with one watermark only.


What I need is a way to watermark each PDF file with it's own different watermark (there are 400 pdfs and 400 different watermarks)


Please help if you can, Thank You.

MacBook Pro 15″, macOS 10.12

Posted on Nov 28, 2020 9:11 AM

Reply
Question marked as Top-ranking reply

Posted on Nov 28, 2020 7:01 PM

It's been so long I forgot that tool.py was in the resource package.

And yes, I stopped using that tool.py as at some point the Watermark Automator Action started working again.


So, knowing that, as long as the OP has named the Watermark with the same name as the PDF, and they all have the same format, (and not a mix of jpeg's and png's - i.e. File_01.pdf and File_02.png), then all than needs to be done is to add one line to the loop I posted above. The Watermarked PDF will be output to the Desktop.


I tested this with only a few files:


#!/bin/bash

for f in $HOME/Documents/*					# <-- Point to dir with PDF's
do
	if [ "${f##*.}" = "pdf" ] ; then
		basename="${f##*/}"
		name="${basename%.*}" 
		pngfile="$HOME/Desktop/$name.png"	# <-- Point to dir with Watermarks
		if [ -f "$pngfile" ] ; then
			/System/Library/Automator/Watermark\ PDF\ Documents.action/Contents/Resources/tool.py --under --xOffset 15 --yOffset 55 --angle 0 --scale 0.75 --opacity 0.5 --input "$f" --output "$HOME/Desktop/$name.pdf" "$pngfile"
		else
			echo PNG file not found for "$f"
		fi
	fi
done




Similar questions

27 replies
Question marked as Top-ranking reply

Nov 28, 2020 7:01 PM in response to VikingOSX

It's been so long I forgot that tool.py was in the resource package.

And yes, I stopped using that tool.py as at some point the Watermark Automator Action started working again.


So, knowing that, as long as the OP has named the Watermark with the same name as the PDF, and they all have the same format, (and not a mix of jpeg's and png's - i.e. File_01.pdf and File_02.png), then all than needs to be done is to add one line to the loop I posted above. The Watermarked PDF will be output to the Desktop.


I tested this with only a few files:


#!/bin/bash

for f in $HOME/Documents/*					# <-- Point to dir with PDF's
do
	if [ "${f##*.}" = "pdf" ] ; then
		basename="${f##*/}"
		name="${basename%.*}" 
		pngfile="$HOME/Desktop/$name.png"	# <-- Point to dir with Watermarks
		if [ -f "$pngfile" ] ; then
			/System/Library/Automator/Watermark\ PDF\ Documents.action/Contents/Resources/tool.py --under --xOffset 15 --yOffset 55 --angle 0 --scale 0.75 --opacity 0.5 --input "$f" --output "$HOME/Desktop/$name.pdf" "$pngfile"
		else
			echo PNG file not found for "$f"
		fi
	fi
done




Nov 29, 2020 10:53 AM in response to lauromota

A few syntax errors will do it every time. UNIX paths do not commence with '$' symbols. Here is the updated script code that works for me using my user name and a PDF folder on my Desktop containing the pdf files, with the corresponding image file on my Desktop.


#!/bin/bash
shopt -s nullglob
for f in /Users/lauromota/Desktop/PDF/*.pdf; # <-- Point to PDF dir with *.pdf files in it
do
    # check filename for lower/upper-case extensions             
    if [ "${f##*.}" = "pdf" -o  "${f##*.}" = "PDF" ] ; then
        basename="${f##*/}"
        name="${basename%.*}" 
        pngfile="/Users/lauromota/Desktop/QRtest/$name.png" # <-- Point to dir with Watermarks
        if [ -f "$pngfile" ] ; then
            /Users/lauromota/Desktop/Script/tool.py --over --xOffset 58 --yOffset 58 --angle 0 --scale 0.11 --opacity 1 --input "$f" --output "/Users/lauromota/Desktop/$name.pdf" "$pngfile"
        else
            pngfile="/Users/lauromota/Desktop/QRtest/$name.PNG"    # <-- Point to dir with Watermarks
            if [ -f "$pngfile" ] ; then
                /Users/lauromota/Desktop/Script/tool.py --under --xOffset 15 --yOffset 55 --angle 0 --scale 0.75 --opacity 0.5 --input "$f" --output "/Users/lauromota/Desktop/$name.pdf" "$pngfile"
                else
                    echo PNG file not found for "$f"
            fi
        fi
    fi
done



Run this from the Terminal after making it executable, and see if a new watermarked PDF appears — before using it in Automator.

chmod +x this_script.sh
./this_script.sh


Nov 29, 2020 5:01 AM in response to lauromota

The tool.py you linked to made changes ("fixes") to the tool.py in the watermark action resource folder, but that should not be a problem.

While it ok to run that script in Automator, it won't show any errors generated by the py script

Save the script in TextEdit as a plain text file and in Terminal make it executable with chmod u+x, then drag the saved script to the Terminal window so you can see what the errors are.


The problem could be that the pdf's and/or png's have uppercase extensions (PDF, PNG), while the script is testing only for lowercase

This script will look for either upper or lower (change directories again to match yours):


#!/bin/bash

for f in $HOME/Documents/*					# <-- Point to dir with PDF's
do
	if [ "${f##*.}" = "pdf" -o  "${f##*.}" = "PDF" ] ; then
		basename="${f##*/}"
		name="${basename%.*}" 
		pngfile="$HOME/Desktop/$name.png"	# <-- Point to dir with Watermarks
		if [ -f "$pngfile" ] ; then
			/System/Library/Automator/Watermark\ PDF\ Documents.action/Contents/Resources/tool.py --under --xOffset 15 --yOffset 55 --angle 0 --scale 0.75 --opacity 0.5 --input "$f" --output "$HOME/Desktop/$name.pdf" "$pngfile"
		else
			pngfile="$HOME/Desktop/$name.PNG"    # <-- Point to dir with Watermarks
			if [ -f "$pngfile" ] ; then
				/System/Library/Automator/Watermark\ PDF\ Documents.action/Contents/Resources/tool.py --under --xOffset 15 --yOffset 55 --angle 0 --scale 0.75 --opacity 0.5 --input "$f" --output "$HOME/Desktop/$name.pdf" "$pngfile"
				else
					echo PNG file not found for "$f"
			fi
		fi
	fi
done

Nov 28, 2020 5:01 PM in response to VikingOSX

😁

yeah, been awhile since I’ve been here (and I got a kick out of seeing that this question linked to one of my old posts from 6 yrs ago)

Good to see some familiar names are still here 😎


...back to the OP, since he wants a different watermark per PDF, Automator isn’t the tool, the solution will be probably be a python script that can be called in a loop that matches the PDF name to the watermark. I saw that there were few python watermark scripts with a little googling, but didn’t test any.

Nov 28, 2020 3:03 PM in response to lauromota

If you have the script to do the watermark (bash, python, etc, not Automator), then you can use a do loop to run through the directory with the PDF files, and save in a variable the ext from PDF to PNG (with the png directory) and then run the script to do the watermark:


#!/bin/bash


for f in $HOME/Documents/* # <-- Point to dir with PDF's

do

if [ "${f##*.}" = "pdf" ] ; then

basename="${f##*/}"

name="${basename%.*}"

pngfile="$HOME/Desktop/$name.png" # <-- Point to dir with Watermarks

if [ -f "$pngfile" ] ; then # <-- Add code for Watermarking on next line

echo PDF File: "$f"

echo PNG File: "$pngfile"

else

echo PNG file not found for "$f"

fi

fi

done


Nov 29, 2020 5:03 AM in response to lauromota

Your Python source from pastebin is missing the following among the import statements:

import shutil


Without that, nothing gets copied as the following line would fail without that import reference.

shutil.copyfile(readFilename, writeFilename)


There are other non-fatal issues with that Python code that could be changed to speed it up.


I just ran this Python script here with your parameters, and using the following png, and the resulting PDF on the Desktop was stamped horizontally in the lower left corner:


The PNG image used is on a transparent background:


Nov 29, 2020 6:09 AM in response to lauromota

Unlike shell scripts, Python code is interpreted based on structured indentation. When copying/pasting Python code from a browser, you need a programmer's editor that is Python syntax aware, so that structure is preserved. TextEdit will destroy that Python code structure and then you have syntax errors.


Since it is free, I suggest BBEdit which is a timed trial, and after the trial, it is free to use. When you paste Python code into BBEdit, use the Edit menu : Paste : and Match Indentation menu item. Save as tool.py to your Desktop, but not as tool.txt. Then in the Terminal, make that Python script executable:


chmod +x ~/Desktop/tool.py


Nov 29, 2020 8:10 AM in response to VikingOSX

I downloaded the code he sent on DropBox . (Click the d/l button in the upper right from this link:

https://www.dropbox.com/s/btyqxkm3qzcgpbk/tool.py?dl=0)

I deleted all previous tool.py I had, and kept the one TonyT1 sent on Dropbox.


I just don't know how to make this script work. When I put it on automator, no files are generated.

#!/bin/bash
for f in $/Users/lauromota/Desktop/PDF*					# <-- Point to dir with PDF'sdo
	if [ "${f##*.}" = "pdf" -o  "${f##*.}" = "PDF" ] ; then
		basename="${f##*/}"
		name="${basename%.*}" 
		pngfile="$Users/lauromota/Desktop/QRtest/$name.png"	# <-- Point to dir with Watermarks
		if [ -f "$pngfile" ] ; then
			/Users/lauromota/Desktop/Script/tool.py --over --xOffset 58 --yOffset 58 --angle 0 --scale 0.11 --opacity 1 --input "$f" --output "$Users/lauromota/Desktop/$name.pdf" "$pngfile"
		else
			pngfile="$Users/lauromota/Desktop/QRtest/$name.PNG"    # <-- Point to dir with Watermarks
			if [ -f "$pngfile" ] ; then
				/Users/lauromota/Desktop/Script/tool.py --under --xOffset 15 --yOffset 55 --angle 0 --scale 0.75 --opacity 0.5 --input "$f" --output "$Users/lauromota/Desktop/$name.pdf" "$pngfile"
				else
					echo PNG file not found for "$f"
			fi
		fi
	fi
done


I just haven't been able to make it work correctly.

Nov 29, 2020 12:24 PM in response to VikingOSX

Looks like he changed my $HOME variable to $/Users/lauromota, and forgot to delete $ (good catch)

Note that you can use $HOME and it will expand to /Users/lauromota when the script is run.

If you're going to run it in Automator, consider changing :

echo PNG file not found for "$f"

to

echo PNG file not found for "$f" >> $HOME/Desktop/err.txt

So that you will know if a matching PNG file is not found


Also., you are assuming that all the pdf file extensions are lower case with /*.pdf in your for loop. As the script tests for lower and upper case extensions for pdf files, no need for /*.pdf, just use /* in the loop (any non-pdf files will be skipped)

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to create a script on MacOS automator to Watermark each file in a folder with a different set of watermarks?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.