Add watermark to PDF

Like you, it works like a dream using 10.4.11 & Applescript 1.10.7 but unfortunately when testing it on 10.5.7 using Applescript 2.0.1 it fails
throwing the same error as before.

tell application "Adobe Acrobat Professional"
	activate
	choose file with prompt "Select PDF to watermark"
		alias "Macintosh HD:Users:admin:Desktop:PDF TEST FILES:PDF TEST FILES.pdf"
	choose file with prompt "Select watermark art file"
		alias "Macintosh HD:Applications:water mark-2.pdf"
	do script "var d = app.openDoc({cPath:'/Users/admin/Desktop/PDF TEST FILES/PDF TEST FILES.pdf', bHidden:false});d.addWatermarkFromFile({cDIPath: '/Applications/water mark-2.pdf', nSourcePage: 0, nStart: 0, nEnd: 0, nHorizValue: 0, nVertValue: 0, nRotation: 45, nOpacity: .1, bOnTop: false, bOnScreen: false, nScale: 1.5});"
		"Adobe Acrobat Professional got an error: \"var d = app.openDoc({cPath:'/Users/admin/Desktop/PDF TEST FILES/PDF TEST FILES.pdf', bHidden:false});d.addWatermarkFromFile({cDIPath: '/Applications/water mark-2.pdf', nSourcePage: 0, nStart: 0, nEnd: 0, nHorizValue: 0, nVertValue: 0, nRotation: 45, nOpacity: .1, bOnTop: false, bOnScreen: false, nScale: 1.5});\" doesn't understand the do script message."

cheers

Major bummer, especially since my department is planning on getting a new mac with a new Acrobat for doing this kind of thing, and it looks like another solution must be found (or at least figure out why it’s different on the newer setup).

I’m very uncertain why and what the problem is in the first place, with a string on one system is not a proper string or class of text on another, and if it’s Acrobat that choking or AppleScript or the OS or the hardware (Intel or PPC) and all that mix of possible problems. I only know that the javascript syntax is correct (it does what it’s supposed to on 10.4 Intel with Acrobat Pro 8), but something about it’s text class isn’t completely correct. And maybe that’s not the problem and it’s something else about Acrobat’s implementation of ‘do script’ or maybe even it’s javascript API has changed.

Does putting one of these three before the ‘do script’ make it work?

	set {text:javaCommand} to javaCommand as Unicode text
	set {text:javaCommand} to javaCommand as string
	set javaCommand to javaCommand as Unicode text

If not then either it’s not a class issue, or AppleScript ain’t right, or Acrobat ain’t right, or I don’t understand coercion of classes. I’ve seen several posts here about the behavior of AppleScript changing from version 1.10.7 to 2.0.1 with regard to the Unicode text class, so I bet it’s there (but wouldn’t bet much since I really don’t know how the game is played).

looks like we have struck a dead end with this pandrake, as the new code on 10.5.7 still delivers the same error, I guess ill have to re-think how im going to achieve what I am after.

Thanks for all your suggestions and time, much appreciated

cheers
Budgie

No problemo - I’m self-serving as I’ll be where you’re at a soon as new equipment/software arrives. =(

Are both machines running the same version of Acrobat Pro?

[edit]
or a workaround might be to write the javascript to a temporary text file and pipe it in to Acrobat that way instead of with ‘do script’ in applescript? read it back in after it’s been saved as text and then read as text forcing it into something that ‘do script’ does accept?

Heck - does the ‘do script’ on 10.5 even work in Acrobat at all; like this?

tell application "Adobe Acrobat Professional"
	activate
	do script "var d = app.openDoc({cPath:'/Users/admin/Desktop/PDF TEST FILES/PDF TEST FILES.pdf', bHidden:false});"
end tell

I’m also wondering if the spaces in the posix path are weirding it out - thought the “quoted form of…” thing took care of that, tho.

yeah both 10.4.11 & 10.5.7 are running Adobe Acrobat Professional 8.1.3

keep me posted if you find a solution down the track,

shout ya a beer if your down my way one day :smiley:

Whoa. Just noticed you’re in New Zealand - my favorite dream destination! I’ve finished with becoming famous (or infamous), so my next goal is to become rich so I won’t have to swim it over and you can be certain I’ll take you up on that. In fact, I’ll be the one treating you to a beer since you’ve helped me (and probably others) flush out the bugs and workarounds and gotchas with the interplay of application and system versions not readily available for testing myself. It’s the beauty of this board, and takes me back to the beginning of infamy when I wrote news stories for the front page.

Hi Ray!

Anyhoo…

Therein lies the rub, methinks. I’m 99% certain that the problem is with the text class of javascript strings being sent to Acrobat 8’s do script action, which is supposed to be Unicode text but really it only works if it’s a [plain] text class. Or maybe either will work but it all has to be the same. Problem I was having was compiling the string of text - worked fine until I put in something that was Unicode text, which is what I got with 'quoted form of POSIX path of…" on my file and folder variables. All the rest of my strings were just text, and it worked fine even when literally writing out the POSIX paths and quoting them, but every time I coerced a Finder path (alias form or other) into a POSIX path it’d fail. This is with AppleScript 1.10.7 which is what I get with Tiger (OSX 10.4.11).

class of (quoted form of POSIX path of (choose file))
-->Unicode text

class of ("This is " & "just a string" as text)
-->string

In walks AppleScript 2.0.1 with OSX 10.5.7 (Leopard), and according to Nigel Garvey the big deal here is that all text (‘string’, ‘text’, or ‘Unicode text’) is Unicode text while it’s being held or handled within a script. The workaround I used with AppleScript 1.10.7 was to get rid of the Unicode text class on the POSIX paths allowing all strings to be [plain] text strings when fed to Acrobat. The post is here:

http://macscripter.net/viewtopic.php?pid=36059#p36059

I bet that the above script will use a different class for the string when sent as text to Acrobat’s do script action. While I couldn’t test whether or not his method for coercing a string’s class into proper shape will work on AppleScript 2.0.1, I’m betting you have answered that question. It does not (unless the method is applied to the final string, not just the file paths, and it works).

So that I keep all that I found searching this BBS for relevant posts, here’s a couple others mention the same problem - tho they aren’t panning out with initial tests either, and have more to do with AppleScript 1.10.7 and earlier, as well as Acrobat 6, not 8.

http://macscripter.net/viewtopic.php?pid=19702
http://macscripter.net/viewtopic.php?id=5280
http://macscripter.net/viewtopic.php?pid=24100

It’s really frustrating, because this works:

class of ("any string" as «class utf8»)

But neither of these work

class of ("any string" as «class ktxt»)
class of ("any string" as Unicode text as record) as «class ktxt»

It’s supposed to work, or at least it used to work for other folk when trying to coerce Unicode text into a ktxt class. Maybe it’s now a different «class xxxx» code for regular 8-bit text or something that works with AppleScript 2.0.1. There’s also a trick of writing the string to a file, and then reading it back in as a certain class.

set x to read (choose file) as «class ktxt»
class of x

However, even tho that doesn’t error on me it results in a weird class for x that I don’t know is going to work for sending a string to Acrobat, or if the class of the string will be maintained by AppleScript 2.0.1 when it does.

Dunno if it’s a solution, but I can think of a way you can test different attempts at solving your dilemma of watermarking a PDF using do script and javascript (which will help me solve the same dilemma in about a year when I’m faced with new machines). There’s gotta be a way to coerce AppleScript 2.0.1 strings into 8-bit text, since it appears that’s what Acrobat 8 wants for it’s ‘do script’ action.

tell application "Adobe Acrobat Professional"
	activate
	--file to watermark
	set aFile to (choose file with prompt "Select PDF to watermark")
	set aFile to (quoted form of POSIX path of aFile) as string
	--watermark file
	set waterMarkFile to (choose file with prompt "Select watermark art file")
	set waterMarkFile to (quoted form of POSIX path of waterMarkFile) as string
	
	--assemble javascript, first open the file into which the watermark will be placed
	set javaCommand to "var d = app.openDoc({cPath:" & aFile as string
	set javaCommand to javaCommand & ", bHidden:false});" as string
	--add the watermark (broken up for readability)
	set javaCommand to javaCommand & "d.addWatermarkFromFile({cDIPath: " as string
	set javaCommand to javaCommand & waterMarkFile as string
	set javaCommand to javaCommand & ", nSourcePage: 0, nStart: 0, nEnd: 0" as string
	set javaCommand to javaCommand & ", nHorizValue: 0, nVertValue: 0, nRotation: 45" as string
	set javaCommand to javaCommand & ", nOpacity: .1, bOnTop: false, bOnScreen: false, nScale: 1.5});" as string
	
	--try different coercions
	--simple
	set javaCommandA to javaCommand as text
	display dialog (class of javaCommandA as string)
	do script javaCommandA
	
	--Nigel's method
	set {text:javaCommandB} to javaCommand as text
	display dialog (class of javaCommandB as string)
	do script javaCommandB
	
	--other direct method?
	set javaCommandC to javaCommand as «class ktxt» --this doesn't work at all on AS 1.10.7
	do script javaCommandC
	
	--write to/read in method
	set tempJavaFile to (choose file name default name "tempJavaText")
	set tempFileReference to (open for access tempJavaFile with write permission)
	write javaCommand to tempFileReference --as text??
	close access tempFileReference
	set javaCommandD to read tempJavaFile --as text?? as «class ktxt»??
	do script javaCommandD
	
end tell

Model: iMac (Intel)
AppleScript: 1.10.7
Browser: Safari 525.13
Operating System: Mac OS X (10.4)

looks like were in for a bit of a session if your shouting to :lol:

Just ran all the suggestions on 10.4.11 all work well bar the one that you pointed out that doesn’t, switched over to 10.5.7 iMac Intel AppleScript 2.0.1 and ran all the suggestions again all came back with the original error, I agree with you this is @#$$% frustrating.

Ill keep you posted if I manage to dig up something of use, again cheers for your time and input, some very valuable info has gone down here.

Thanx muchas for testing those - looks like AppleScript TNG needs a little help with getting it’s string theory adapted to misleading requirements (Adobe sez Unicode is needed, but apparently it’s Unicode that fails). Days like these I wish I studied CS instead of art and literature. I’m hopeful that someone creative will find a way to coerce strings into the proper form for Acrobat, especially since I’ve noticed that all Adobe products have favored Javascript over AppleScript.

Meh. I’d rather be frustrated now rather than later. Beat the rush. Hope there’s a solution soon for your workflow. I might be looking into the Adobe boards for solutions plus alternatives to javascript soon but on first blush it looks like we’ve got no choice. Plus, either is a lot cleaner than a GUI script.

Thanks again, Budgie. =)
Captain out!

I have posted at Adobe Acrobat’s scripting forum, heres the link just in case a solution or leads to a solution relating to this issue arise.

http://forums.adobe.com/thread/446762?tstart=0

Sweeeeeeeeet. Thanks for the poke. I’ve let this issue slide a bit as I’m nearing a massive deadline for a catalog and the PDF stuff is still a ways away. I’ll keep an eye out, too; soon as there’s something that works on AS 2.0 I’ll test it on AS 1.7 to see what’s to be done for both.

Thanks again, mon. Mahsive cool of ya.

I’m not sure if this is relevant. please forgive me if it’s not.

This works for me with 10.5.7 and acrobat pro 9.1.2:


tell application "Finder"
	set myFolder to (container of mypath) as string
	set myFolderPOSIX to POSIX path of ((container of (path to me)) as alias)
end tell

set myDesktop to path to desktop as string
set FormattedPDF to myDesktop & "formatted.pdf"
set WMscript to "this.addWatermarkFromFile('" & myFolderPOSIX & "watermark.pdf');"

tell application "Adobe Acrobat Pro"
	activate
	open FormattedPDF
	do script WMscript
end tell

Hope this helps,
Jeremiah

Indeed it helps, Jeremiah! Many thanks. It’s encouraging, since it looks like Acrobat’s problem with Unicode text is solved in version 9. This bodes well for my impending introduction of 10.5 and Acrobat Pro, but it leaves me with a need to upgrade all instances of Acrobat 8 if I upgrade the Macs from 10.4 which I don’t want to do if I don’t have to. Bah!

I have been doing some digging, and think I have found a solution that works for both Tiger and Leopard, tho I can only test it on Tiger. It uses an “osaxen” type application from has:

http://osaxen.com/files/textcommands1.1.3.html

Neato! Much easier than trying to figure out textutil and -stdout or various terminal commands and encodings or resorting to file writing and reading… Here’s an example of what should work on both Tiger and Leopard with Acrobat Professional 8.

set aPDFfile to (choose file of type {"com.adobe.pdf"})

set pdfPosixPath to quoted form of POSIX path of aPDFfile
--the posix path string is always Unicode text
--even when combined with ascii strings (8-bit) in Tiger
--solution was to use hack by Nigel, but doesn't work on Leopard
set pdfPosixPath to my NigelFix(pdfPosixPath) --comment out if not needed

--the following string cannot be classed as Unicode text for Acrobat Pro 8
set javaCmd to "d = app.openDoc({cPath:" & pdfPosixPath & ", bHidden:false});" as string
--javaCmd is combination of ascii strings and Unicode text in Tiger
--javaCmd is all Unicode text in Tiger
--fails when passed to Acrobat 8's do script

--faceless app called "TextCommands" will do the trick
--to the entire string, apparently
set javaCmd to my txtCmdsCnvt(javaCmd)

tell application "Adobe Acrobat Professional"
	do script javaCmd
end tell

--Nigel's solution to Unicode text => ascii text problem in Acrobat8's do script command
on NigelFix(unicodeText)
	set {text:unicodeText} to unicodeText as string
	return unicodeText
end NigelFix

--TextCommands installed in Scripting Additions
on txtCmdsCnvt(unicodeText)
	tell application "TextCommands"
		return convert from unicode unicodeText
	end tell
end txtCmdsCnvt

Did you know Automator has an action to watermark PDF’s? You just need to set it up with an image to use as the watermark. It has several controls to help you resize, rotate, and lighten the watermark image.

Note: the action has a flaw. There are controls which allow you to rotate the watermark image which don’t work… or so it seems. When you enter a rotation amount automator seems to lock-up and stop working. What I found is just type the rotation amount into the box even though you can’t see what you are typing because of the seemingly lock-up situation. Then you can press command-s to save the automator workflow and then command-q to quit automator. Then when you re-open your automator action you will see that the rotation angle you entered has been saved. So you need to do this routine a couple times until you get the action setup how you want.

Ya, I looked into it originally and it seemed easy enough to process files from a Folder Action attached to a NAS folder with Automator, tho not entirely straightforward. To be honest I’ve not delved into Automator much at all - in the context of scripting I’m a Automator newbie and see it as more macro/user oriented than batch/production oriented.

My case is where I have one Mac to serve custom file processing on PDFs and other formats to the entire company, not just the Macs. Watermarking is only one automated workflow, and the others are only possible with Acrobat Pro, and especially its javascript API, that become even more elaborate if I can mix it and other applications together with AppleScript (even stuff on a couple of very old Macs in concert with one relatively new Mac). Makes it cheaper and easy-to-train solutions for users who don’t need to know the in’s and out’s of graphic art and AppleScript, just drop dot-files into dat-folder. Plus, I’m planning on scaling the watermarking feature to include variable data, which is more easily achieved with text watermarking than image watermarking.

Thanks for the tip on making the prefab watermarking bug. I can see it of use for local Mac use if it doesn’t change the RIP or security of the PDF other than adding a watermark (I presume it uses either print2pdf or quartz something??).

Yep, it does the trick, but the rendering can be weird sometimes : PDF in Automator are treated by Apple Quartz. It results in bad transparency if you deal with complex PDFs… I tried everything with Indesign PDF presets (make a PDF 1.3 or 1.4 etc. and other settings…) without getting 100% of good PDFs when I add watermaks in Automator.

I’d like doing it with javascript and Acrobat, unfortunately the website to get “TextCommands” is down for instance.

Ok I found this and It works in Acrobat 9.4.1 :

tell application "Adobe Acrobat Pro"
		tell active doc
		set The_String to "watermark"
		my Add_WaterMarkText(The_String, 1, 1, 1, 1, 3, 0, 0, 0)
		do script the result
	end tell
end tell
-- 
on Add_WaterMarkText(cText, nS, nE, nTA, nHA, nVA, nHV, nVV, nR)
	set Params to "cText: '" & cText & "'," & return
	if class of nS is integer then set Params to Params & "nStart: " & (nS - 1) & "," & return
	if class of nE is integer then set Params to Params & "nEnd: " & (nE - 1) & "," & return
	-- 0 = left, 1 = centre, 2 = right 
	if class of nHA is integer then set Params to Params & "nHorizAlign: " & 0 & "," & return
	-- 3 = top, 4 = bottom 
	if class of nVA is integer then set Params to Params & "nVertAlign: " & 4 & "," & return
	-- offset in points 
	if class of nHV is integer then set Params to Params & "nHorizValue: " & nHV & "," & return
	-- offset in points 
	if class of nVV is integer then set Params to Params & "nVertValue: " & nVV & "," & return
	if class of nR is integer then set Params to Params & "nRotation: " & 90 & "," & return
	set JavaScript to "this.addWatermarkFromText({" & Params & "});"
	return JavaScript
end Add_WaterMarkText

But how to do the same from a File ?
Thank you !

Do somebody know when it is available again ?
Thank you !

Hey !
The script below works for Snow Leopard 10.6.8 and Acrobat Pro 9.4.5.


set watermark_pdf to "path:to:your:watermark.pdf"
	set watermark_pdf to quoted form of POSIX path of watermark_pdf
	set watermark_javascript to "this.addWatermarkFromFile({
 cDIPath: " & watermark_pdf & ",
 nHorizAlign: app.constants.align.left,
 nVertAlign: app.constants.align.bottom,
 nHorizValue: 0, nVertValue: 0,
 nRotation: 0,
 nScale: -0.33
 });"
		
	tell application "Adobe Acrobat Pro"
		do script watermark_javascript
	end tell



it’s a beautiful thing alitaliano, great stuff, glad to see it’s finally fixed

Budgie

Hey,
it doesn’t work anymore in Acrobat X (with 10.7 Lion) :rolleyes:, any solution ?
Thank you !