-- © Dieter May, Jun 2020 use scripting additions try tell application "Adobe Acrobat" to set pdfPath to file alias of active doc set pdfPath to POSIX path of pdfPath on error errMess display alert errMess return end try --- wenn hier etwas geändert wird, muss ~/Library/Caches/watermark-applescript.py gelöscht werden. set theProperties to "xOffset, yOffset, angle, scale, opacity = 70.0, 600.0, 35.0, 1.0, 0.1" set theFont to "Chalkduster" -- in Schriftsammlung mit Apfel-I den PostScript-Namen anzeigen set theFontSize to "120.0" -------------------- set tmpFolder to (path to "cach" from user domain as string) set pyScript to POSIX path of (tmpFolder & "watermark-applescript.py" as string) if not CheckExistence(pyScript) then writeToFile(pyScript, pythonScript(theProperties, theFont, theFontSize)) end if display dialog "Wasserzeichen" default answer "Kopie" buttons {"Abbrechen", "OK"} default button 2 copy the result as list to {buttonPressed, textReturned} set theCMD to "/usr/bin/python " & quoted form of pyScript & space & quoted form of pdfPath & space & quoted form of textReturned set outFile to (do shell script theCMD) --delay 1 set hfsFile to POSIX file outFile --tell application "Finder" to reveal hfsFile tell application "Adobe Acrobat" open hfsFile activate end tell on writeToFile(filePath, theData) try set f to open for access (filePath as text) with write permission set eof f to 0 write (theData) to f starting at eof as «class utf8» close access f on error theError try close access f end try return theError end try end writeToFile on CheckExistence(FileOrFolderToCheckString) try alias FileOrFolderToCheckString return true on error return false end try end CheckExistence on pythonScript(theProperties, theFont, theFontSize) return "#!/usr/bin/python # coding: utf-8 import sys import os import math import Quartz.CoreGraphics as Quartz from CoreText import (kCTFontAttributeName, CTFontCreateWithName, CTLineDraw, CTLineCreateWithAttributedString, kCTFontAttributeName, CTLineGetImageBounds) from CoreFoundation import (CFAttributedStringCreate, CFURLCreateFromFileSystemRepresentation, kCFAllocatorDefault) def createPDFDocumentFromPath(path): return Quartz.CGPDFDocumentCreateWithURL(Quartz.CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, path, len(path), False)) def createOutputContextWithPath(path): return Quartz.CGPDFContextCreateWithURL(Quartz.CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, path, len(path), False), None, None) def contextDone(context): if context: Quartz.CGPDFContextClose(context) del context def drawWatermarkText(writeContext, line, xOffset, yOffset, angle, scale, opacity): if line: rect = CTLineGetImageBounds(line, writeContext) imageWidth = rect.size.width imageHeight = rect.size.height Quartz.CGContextSaveGState(writeContext) Quartz.CGContextSetAlpha(writeContext, opacity) Quartz.CGContextTranslateCTM(writeContext, xOffset, yOffset) Quartz.CGContextTranslateCTM(writeContext, imageWidth / 2, imageHeight / 2) Quartz.CGContextRotateCTM(writeContext, angle * math.pi / 180) Quartz.CGContextTranslateCTM(writeContext, -imageWidth / 2, -imageHeight / 2) Quartz.CGContextScaleCTM(writeContext, scale, scale) Quartz.CGContextSetTextPosition(writeContext, 0.0, 0.0) CTLineDraw(line, writeContext) Quartz.CGContextRestoreGState(writeContext) if __name__ == '__main__': " & theProperties & " font = CTFontCreateWithName('" & theFont & "', " & theFontSize & ", None) text = sys.argv[2] filename = sys.argv[1] shortName = os.path.splitext(filename)[0] outFilename = shortName + '_' + text + '.pdf' pdf = createPDFDocumentFromPath(filename) writeContext = createOutputContextWithPath(outFilename) pages = Quartz.CGPDFDocumentGetNumberOfPages(pdf) if pdf: for i in range(1, (pages+1)): page = Quartz.CGPDFDocumentGetPage(pdf, i) if page: mbox = Quartz.CGPDFPageGetBoxRect(page, Quartz.kCGPDFMediaBox) Quartz.CGContextBeginPage(writeContext, mbox) Quartz.CGContextDrawPDFPage(writeContext, page) astr = CFAttributedStringCreate(kCFAllocatorDefault, text, { kCTFontAttributeName : font }) line = CTLineCreateWithAttributedString(astr) drawWatermarkText(writeContext, line, xOffset , yOffset, angle, scale, opacity) Quartz.CGContextEndPage(writeContext) del pdf contextDone(writeContext) print (outFilename) " end pythonScript