Because my company and most of our clients are only half way to a fully digital process, I need to "sign" many pdf documents a week by adding an image of my signature and initials. This saves me hours of a "print" - "sign" - "scan" routine, but can still be tedious: open, import image, move image around, copy, paste, etc.....
I've developed a small routine in AppleScript which I call using a KM macro, and others may find interesting. There's a particular twist that I learned courtesy of Dr Drang (here) - and which may be of interest to others trying to script apps:
In a sandboxed app, if you try to access external resources (such as an image file) with no user interaction, then this resource has to be within the "container" library of the app, otherwise your script will do nothing. It won't fail, it won't throw an error. It'll just do nothing and let you wonder what you've done.
If you have the user select the resource (as in the pdfpenpro example script "insert imprint image" that was the basis of my own script), then it can be anywhere ...
Here's the script:
--------------------------------------------------------------------------------
# Auth: Paul Atlan
# dCre: 2019-04-21
# dMod: 2019-04-21
# Appl: PDFpenPro & Finder & KM
# Task: Initial every page and sign last page of selected document
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @PDFpenPro, @OCR, @Selected, @Files, @Finder, @Work
--------------------------------------------------------------------------------
-- finder and KM interaction based on initial script by @ccstone here https://forum.keyboardmaestro.com/t/how-to-script-pdfpenpro-to-ocr-selected-finder-items/5553/2
-- signature stuff helped by sandboxing advice from Dr. Drang here: https://leancrew.com/all-this/2013/02/one-step-watermarking-service/
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Finder" to set finderSelectionList to selection as alias list
if length of finderSelectionList = 0 then
display dialog "No files were selected in the Finder!"
end if
-- update these to your own Initials, Signature and Stamp
-- these files need to be in the Containers library because of sandboxing
set thePathParaphe to POSIX path of "/Users/paul/Library/Containers/com.smileonmymac.PDFpenPro/Data/Documents/paraphe.png"
set thePathSignature to POSIX path of "/Users/paul/Library/Containers/com.smileonmymac.PDFpenPro/Data/Documents/signature.png"
set thePathTampon to POSIX path of "/Users/paul/Library/Containers/com.smileonmymac.PDFpenPro/Data/Documents/tampon.png"
try
tell application "PDFpenPro"
if not running then run
activate
repeat with theFile in finderSelectionList
open theFile
tell document 1
set imprintCount to 0
set pageCount to count pages
repeat with pageNumber from 1 to pageCount - 4
set xpos2 to 0.9 * (width of page pageNumber)
set ypos2 to 0.05 * (height of page pageNumber)
make new imprint with properties {path:thePathParaphe, x position:xpos2, y position:ypos2} at beginning of imprints of page pageNumber
end repeat
set pageNumber to pageCount - 3
set xpos0 to 0.5 * (width of page pageNumber)
set xpos1 to 0.62 * (width of page pageNumber)
set ypos0 to 0.05 * (height of page pageNumber)
set ypos1 to 0.13 * (height of page pageNumber)
make new imprint with properties {path:thePathSignature, x position:xpos0, y position:ypos0} at beginning of imprints of page pageNumber
make new imprint with properties {path:thePathTampon, x position:xpos1, y position:ypos1} at beginning of imprints of page pageNumber
set pageNumber to pageCount - 2
set xpos0 to 0.5 * (width of page pageNumber)
set xpos1 to 0.62 * (width of page pageNumber)
set ypos0 to 0.05 * (height of page pageNumber)
set ypos1 to 0.13 * (height of page pageNumber)
make new imprint with properties {path:thePathSignature, x position:xpos0, y position:ypos0} at beginning of imprints of page pageNumber
make new imprint with properties {path:thePathTampon, x position:xpos1, y position:ypos1} at beginning of imprints of page pageNumber
repeat with pageNumber from pageCount - 1 to pageCount
set xpos2 to 0.9 * (width of page pageNumber)
set ypos2 to 0.05 * (height of page pageNumber)
make new imprint with properties {path:thePathParaphe, x position:xpos2, y position:ypos2} at beginning of imprints of page pageNumber
end repeat
end tell
end repeat
end tell
on error e number n
set e to e & return & return & "Num: " & n
if n ≠ -128 then
try
tell application (path to frontmost application as text) to set ddButton to button returned of (display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} default button "OK" giving up after 30)
if ddButton = "Copy Error Message" then set the clipboard to e
end try
end if
end try
The KM Macro is simplicity itself: simply link a keyboard trigger to an "execute applescript" action, from file, and link to the above script.
You need to update the signature, stamp and initial filenames and location, and choose where on the document they appear (my signature is usually on the 3rd and 4th to last pages, everything else is initialed.)
Thanks to @ccstone for the framework to select a file and act on it, and Dr Drang for the sandboxing tip.