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)