Script "Add and remove textstamp"

Hi everybody

I have used for a long time the script where uses PDFpenPro. But, i was not happy with this solution. The license for this tool is expensive and the workflow is not realy the fastest. Now, i have found a great solution where i want share with you.

The base for it is the executable cpdf from github.com/coherentgraphics/cpdf-binaries … its free for personal use. You must download the binary and copy the cpdf to a place on your harddisk.
after this you should replace the path in the line “set apppath…” to the correct path.

I have in my (german) script solved following:

  • Add Textstamp, choose after enter text the position of text. (left, center or right). Dependendly on position, i have defined different colors (left = green, center=blue, right=red). And the default text is the current date (til 12am), after the day of tommorow. (Idea, mostly i use this feature to add the date of payment and my bank to process all payments til 12.00 in the same day)

  • Remove Textstam: All exist textstamps (created with this script/cpdf) can be removed later again with this button.

The script support one or multiple documents.

And when you go to systemsettings > keyboard > shortcuts > app-shortcuts , then you can define a real shortcut for devonthink and this script.


-- Script "Add and Remove Textstamps" für DevonThink
-- Fügt Textstempel zu PDF Dokumenten an drei wählbaren Positionen hinzu oder entfernt alle bestehenden Textstempel.
-- Verwendet das Script CPDF (https://github.com/coherentgraphics/cpdf-binaries) 
-- Hierzu muss das Binary cpdf in das Scriptverzeichnis kopiert werden (siehe auf Befehlszeile "set apppath")
-- Coded by Michael Huwiler, huwi.ch

-- Config

set apppath to quoted form of "/Users/huwi/Library/Application Support/DEVONthink Pro 2/Scripts/cpdf" -- Speicherort von cpdf (https://github.com/coherentgraphics/cpdf-binaries)
set pages to "1" -- "1" = Textstempel nur auf erster Seite einfügen, "" = Textstempel auf allen Seiten im PDF einfügen
set colorleftright to {"1 0 0", "0 0.5 0", "0 0 0.7"} -- Farben, Feld 1 = links, Feld 2 = rechts, Feld 3 = mitte
set positionleftright to {"-topright 40", "-topleft 40", "-top 40"}

set foregroundcolor to {65535, 65535, 65535}
set backgroundcolor to {65500, 0, 0}

-- Datum ermitteln / Bis 12 Uhr Datum von heute, ab 12 Datum von morgen

set myTime to time of (current date) -- Aktuelle Zeit in Sekunden

if myTime > 12 * 60 * 60 then
	-- Nachmittag > Datum von morgen
	set mydate to ((current date) + (24 * 60 * 60))
	
else
	-- Vormittag = Datum von heute
	set mydate to (current date)
end if

set {year:yr, month:mn, day:dy} to (mydate)
set dateString to pad(dy) & "." & pad(mn as integer) & "." & (yr as text)
--set dateString to "Bezahlt " & dateString

on pad(v)
	return text -2 thru -1 of ((v + 100) as text)
end pad

-- Losgehts!
tell application id "DNtp"
	
	set theSelection to the selection
	set anzdocs to (count of theSelection) as integer
	
	set tmp to "" as text
	
	set anz to number
	
	
	if (count of theSelection) is greater than 0 then
		
		set dialog_reply to display dialog "Textstempel hinzufügen:" with title "Textstamps hinzufügen/entfernen" default answer dateString buttons {"Abbruch", "Entferne bestehende Textstempel", "OK"} default button "OK" cancel button "Abbruch"
		set mytext to text returned of dialog_reply
		set mytext to quoted form of mytext
		set anz to 0
		
		if button returned of dialog_reply is equal to "OK" and mytext is not equal to "" then
			
			set optionList to {"Rechts", "Mitte", "Links"}
			set gewaehlteposition to choose from list optionList with title "Textstamps einfügen" with prompt "Wo einfügen?" default items "Rechts" without multiple selections allowed
			
			if gewaehlteposition is not false then
				-- Text eingegeben und OK, danach Positionswahl
				set positionwahl to item 1 of gewaehlteposition
				
				if positionwahl is equal to "Rechts" then
					set paramcolor to item 1 of colorleftright
					set paramposition to item 1 of positionleftright
				else if positionwahl is equal to "Links" then
					set paramcolor to item 2 of colorleftright
					set paramposition to item 2 of positionleftright
				else if positionwahl is equal to "Mitte" then
					set paramcolor to item 3 of colorleftright
					set paramposition to item 3 of positionleftright
				end if
				
				repeat with a from 1 to anzdocs
					set thePath to path of item a of theSelection
					
					if thePath ends with ".pdf" then
						display notification (a as text) & " von " & anzdocs with title "Textstamps hinzufügen" subtitle "Verarbeite Dokumente"
						set thePath to quoted form of thePath
						set thePID to do shell script apppath & " -add-text " & mytext & " " & paramposition & " -color \"" & paramcolor & "\" -line-spacing 1.5 -font \"Courier-Bold\" -font-size 16 " & thePath & " " & pages & " -o " & thePath & " > /dev/null 2>&1 & echo $!"
						
						-- warten bis shell ausführung beendet ist
						repeat
							do shell script "ps ax | grep " & thePID & " | grep -v grep | awk '{ print $1 }'"
							if result is "" then exit repeat
							delay 0.2
						end repeat
						--set tmp to tmp & " /// " & a & ": " & thePath & " (" & positionwahl & ")"
						set anz to anz + 1
						
					else
						--set tmp to tmp & " /// ---- " & a & ": " & thePath & " ---- "
					end if
					
				end repeat
				display notification "Textstempel (" & mytext & ") eingefügt. " & tmp with title "Textstamps hinzufügen" subtitle "Fertig! " & (anz as text) & " Dokumente verarbeitet"
				
			end if
		else if button returned of dialog_reply is equal to "Entferne bestehende Textstempel" then
			
			set sicher to display dialog "In den ausgewählten Dokumenten wirklich alle bestehenden Textstempel entfernen?" with title "Textstamps entfernen" with icon caution buttons {"Abbruch", "OK"} default button "OK" cancel button "Abbruch"
			if button returned of sicher is equal to "OK" then
				
				repeat with a from 1 to anzdocs
					display notification (a as text) & " von " & anzdocs with title "Textstamps entfernen" subtitle "Verarbeite Dokumente"
					set thePath to path of item a of theSelection
					
					if thePath ends with ".pdf" then
						set thePath to quoted form of thePath
						set thePID to do shell script apppath & " -remove-text " & thePath & " -o " & thePath & " > /dev/null 2>&1 & echo $!" -- evtl bestehender Text vorher entfernen
						-- warten bis shell ausführung beendet ist
						repeat
							do shell script "ps ax | grep " & thePID & " | grep -v grep | awk '{ print $1 }'"
							if result is "" then exit repeat
							delay 0.2
						end repeat
						set anz to anz + 1
					else
						--set tmp to tmp & " /// ---- " & a & ": " & thePath & " ---- "
					end if
					
				end repeat
				display notification "Bestehende Textstempel sind entfernt." with title "Textstamps entfernen" subtitle "Fertig! " & (anz as text) & " Dokumente verarbeitet"
			end if
			
		else
			set dialog_reply to display notification "Nix gemacht"
		end if
	else
		set dialog_reply to display dialog "Kein(e) Dokument(e) ausgewählt" buttons {" OK "}
	end if
end tell

have fun with this :wink:=

Best regards
michael

Hello,

somehow I have trouble installing cpdf.
I download a cpdf.dms file from GitHub, (I can’t open it), put it into Devonthink scripts.
Then I entered the path into the script and saved it there.
If I now open a PDF and activate the script, the small menu for the text stamp opens and gets a message that the text stamp has been set.
Only on the PDF no text stamp is noted?
What am I doing wrong?

Hallo,

irgendwie habe ich Schwierigkeiten, cpdf zu installieren.
Von GitHub lade ich eine cpdf.dms Datei runter, (die kann ich nicht öffnen), lege diese Datei in Scripts von Devonthink ab.
Dann habe den Pfad in das Script eingegeben und auch dieses dort gespeichert.
Wenn ich nun ein PDF öffne und das Script aktiviere, geht auch das kleine Menü für den Textstempel auf und bekomme per Mitteilung angezeigt, daß der Textstempel gesetzt wurde.
Nur auf dem PDF wird kein Textstempel notiert?
Was mache ich falsch?

Gruß Rippchen

Mich irritiert deine Bezeichnung cpdf.dms :wink:

Mach mal folgendes:

  • öffne github.com/coherentgraphics/cpdf-binaries
  • Klick auf “Clone or Download”, dann auf “Download as ZIP”
  • Entpacke das Archiv durch Doppelklick
  • Kopiere aus dem Ordner cpdf-binaries-master/OSX-Intel die Datei “cpdf” in den Scriptordner (oder sonst wohin, wichtig ist nur, dass der Pfad im Script dann angepasst wird :wink:

So müsste es laufen.

Your filename cpdf.dms irritates me :wink:

Do the following:

  • open github.com/coherentgraphics/cpdf-binaries
  • Click on “Clone or Download”, then on “Download as ZIP”.
  • Unpack the archive by double-clicking on it
  • Copy the file “cpdf” from the folder cpdf-binaries-master/OSX-Intel into the script folder (or anywhere else, the only important thing is that the path in the script is adjusted then :wink:

This is how it should work.

Best regards
huwi

Wonderful, it works. :mrgreen:
cpdf pasted into the script folder and copied the script.
Fine tool, saves a lot of fiddling.

Gruß Rippchen

Wunderbar, klappt. :mrgreen:
cpdf eingefügt in den Script Ordner und das Script kopiert.
Feines Tool, erspart ne Menge Fummelei.

Gruß Rippchen