Text and I don't get along

Hello, simple issue with executing a few lines of Javascript from within an applescript code. The javascript is being sent to Acrobat 7.0. The code works until I put a variable name in for the path. Here’s both versions.

(* Getting the Path *)
set the_file to (path to desktop as string) & “newpdf.pdf”
set the_file to POSIX path of the_file

(* This is the path hard coded and it works. Opens the file and adds the watermark*)
do script “var d = app.openDoc({cPath:‘/Users/mattsumner/desktop/newpdf.pdf’});
d.addWatermarkFromFile({cDIPath: ‘/Users/mattsumner/Desktop/watermark.pdf’, bOnScreen: false, nScale: -1, nOpacity: .45});”

(This is the path passed from a variable and it breaks.)

do script “var d = app.openDoc({cPath:'” & the_file & “'}); d.addWatermarkFromFile({cDIPath: ‘/Users/mattsumner/Desktop/watermark.pdf’, bOnScreen: false, nScale: -1, nOpacity: .45});”

(Copy and paste from Event Log after running the above*)

do script “var d = app.openDoc({cPath:‘/Users/mattsumner/Desktop/newpdf.pdf’}); d.addWatermarkFromFile({cDIPath: ‘/Users/mattsumner/Desktop/watermark.pdf’, bOnScreen: false, nScale: -1, nOpacity: .45});”

(Adobe Acrobat 7.0 Standard got an error: “var d = app.openDoc({cPath:‘/Users/mattsumner/Desktop/newpdf.pdf’}); d.addWatermarkFromFile({cDIPath: ‘/Users/mattsumner/Desktop/watermark.pdf’, bOnScreen: false, nScale: -1, nOpacity: .45});” doesn’t understand the do script message.)

The variable seems to be inserting the right text. The only thing I can think of is that when you hard-code the path into the script, it’s in plain text, whereas ‘POSIX path’ returns Unicode text, which, when concatenated into the plain-text string, produces a result in international text. Maybe Javascript or Acrobat doesn’t like this. But I’m only guessing.

Does it work if you instead get a plain-text version of the POSIX path?

set the_file to (path to desktop as string) & "newpdf.pdf"
set {text:the_file} to (POSIX path of the_file) as string

Presumably ‘do script’ is a command in the environment where you’re running the script.

First, many thanks to Nigel for posting the neato way to coerce text around - I’d been trying all kinds of “set ‘foo’ to ‘bar’ as text/string/Unicode text” with no luck and thought the problem might not be that. I had the same problem as Trash Man and Nigel’s solution works!!!

And, in case anyone’s interested, the Acrobat 8 javascript API manual lists “com.adobe.Acrobat.tiff” as a valid value for cConvID of the saveAs command. It’s not. It’s “com.adobe.acrobat.tiff” and that one lowercase/uppercase case makes a difference.

Cheers!

The code I posted is a hack for coercing any kind of text recognised or used by AppleScript 1.x.x (up to and including Tiger) to plain, single-byte ‘string’. I’m not sure that it works with AppleScript 2.0 (Leopard or later), where all text (‘string’, ‘text’, or ‘Unicode text’) is Unicode text while it’s being held or handled within a script.

I’d be interested to know if it does work though. What system are you using?

OSX 10.4.11, Intel Core 2 Duo

~k

(sorry, I forgot to subscribe to this topic - didn’t notice your reply till now)