Copy link to clipboard
Copied
G'day
I'm having awful problems trying to get the following JavaScript to work on my mac, running El Capitan.
Using an old 'Add Watermark' Java script, the free barcode font Code 128 draws as the font Arial. So I'm trying to use an Annotation, with no success after two days trying.
I get an error that the Java Script does not understand the 'do script' message.
Any guidance, please.
Regards
Santa
set theNewString to ((character id 209) & "this is a test" & (character id 211) as text)
# values are Text, Font, Font Size, Start Page, End Page, Text type?, Horizontal Alignment, Vertical Offset, Horizontal Offset, Vertical Height, Rotation, Current Page
set theResult1 to my Add_WaterMarkText(theNewString, "Code 128", 48, 1, 1, 1, 0, 3, 10, 0, 0, 1)
# This handler uses Java Script
#
on Add_WaterMarkText(cText, cFont, nFontSize, nS, nE, nTA, nHorizontalAlignment, nVerticalAlignment, nHorizontalValue, nVerticalValue, nR, currentPage)
set Params to ""
set Params to Params & "cPage:" & currentPage - 1 & ", " & return
set Params to Params & "cType:Text, " & return
set Params to Params & "cContents:" & cText & ", " & return
set Params to Params & "cTextFont:" & cFont & ", " & return
set Params to Params & "cTextSize:40, " & return
set Params to Params & "cpoint:[300, 100], " & return
set Params to Params & "cWidth:0, " & return
set Params to Params & "cAlignment:0, " & return
tell application "Adobe Acrobat"
activate
tell document 1
do script "this.addAnnot({" & Params & "});"
end tell
end tell
end Add_WaterMarkText
Replace all colons with equals signs (=).
Also, double-check the order of the parameters of addField.
Copy link to clipboard
Copied
I'm really up the proverbial creek without a paddle.
I've tried modifying the script to the following, and have no idea if a 'c' is necessary before each item, as in my first attempt. All I know is that the clipboard contains exactly the script in the form it's supposed to be in, as far as I can tell, the script is NOT crashing, and nothings happening, no text boxes are being drawn. Do I need some different code to draw a text box rather than an Annotation?.
Please, any advice?
tell application "Adobe Acrobat"
activate
set p to 1
tell document 1
tell page 1
set cbox to {}
if cbox = {} then set cbox to crop box
if cbox = {} then set cbox to bounds
if cbox = {} then set cbox to media box
if cbox = {} then set cbox to art box
if cbox = {} then set cbox to trim box
if cbox = {} then set cbox to bleed box
set p to 4
set pageTop to (item 2 of cbox) + 36
set p to 5
set item 2 of cbox to pageTop
set p to 6
set pageBottom to (item 4 of cbox)
set p to 7
set item 4 of cbox to pageBottom
set p to 8
set bounds to cbox
set p to 9
set media box to cbox
set p to 10
set crop box to cbox
set p to 11
set trim box to cbox
set p to 12
set bleed box to cbox
set p to 13
set art box to cbox
set p to 14
set theNewString to ((character id 209) & "this is a test" & (character id 211) as text)
#
# values are Text, Font, Font Size, Start Page, End Page, Text type?, Horizontal Alignment, Vertical Offset, Horizontal Offset, Vertical Height, Rotation, Current Page
set p to 15
set theResult1 to my Add_WaterMarkText(theNewString, "Times", 16, 1, 1, 1, 0, 3, 10, 0, 0, 1)
try
do script theResult1
on error errmsg
display dialog errmsg
end try
set theResult1 to my Add_WaterMarkText(theNewString, "MRV Code128bXSA", 16, 1, 1, 1, 0, 3, 10, 0, 0, 1)
try
do script theResult1
on error errmsg
display dialog errmsg
end try
end tell
end tell
end tell
# This handler uses Java Script
#
on Add_WaterMarkText(cText, cFont, nFontSize, nS, nE, nTA, nHorizontalAlignment, nVerticalAlignment, nHorizontalValue, nVerticalValue, nR, currentPage)
# set theType to "Text" as text
set Params to "page:" & currentPage - 1 & ", " & return
set Params to Params & "type:\"" & "Text" & "\", " & return
set Params to Params & "contents:\"" & cText & "\", " & return
set Params to Params & "textFont:\"" & cFont & "\", " & return
set Params to Params & "textSize:" & nFontSize & ", " & return
set Params to Params & "rect:[1, 1, 300, 100], " & return
set Params to Params & "width:0, " & return
set Params to Params & "alignment:0, " & return
set Params to Params & "hidden:false, " & return
set Params to Params & "textColor:color.black," & return
set Params to Params & "opacity:1," & return
set javaScript to "this.addAnnot({" & return & Params & "});"
set the clipboard to javaScript
return javaScript
end Add_WaterMarkText
The ClipBoard...
this.addAnnot({
page:0,
type:"Text",
contents:"Ñthis is a testÓ",
textFont:"MRV Code128bXSA",
textSize:16,
rect:[1, 1, 300, 100],
width:0,
alignment:0,
hidden:false,
textColor:color.black,
opacity:1,
});
Copy link to clipboard
Copied
If you're trying to add a barcode that can be expressed using a font, use a text field instead of an annotation or watermark.
Copy link to clipboard
Copied
Thank you both.
I finally got the posted script to run, only to find it created a 'dialog balloon' icon, that when clicked on, revealed the text in Arial format. So I've decided to try the text box approach.
The script I've come up with fails with the same error as before, doesn't understand the 'do script' message.
I've pasted the Clipboard into the debugger, and added 'var f ' in front, and the error I eventually get is that it needs a Doc set, which I don't know how to do, despite researching 'Doc'. I'm presuming that means the script should be OK, but why don't it work? I'm hopeless at JavaScript, so any advice is greatly appreciated.
I would also like to make sure I'm using the correct font name, by following this procedure below, but can't work out how to name a text box 'Text1'. Any help on this as well, please? I've tried all sorts of things, but no explanation tells me how to name a text box in Acrobat Pro DC! Is it possible to address the text field by a number?
Thanks in advance,
Santa
1.Create a text field in a PDF document. Using the UI, set the text font for this field to the desired font.
2.Open the JavaScript Debugger Console and execute the script
this.getField("Text1").textFont
The above code assumes the name of the field is Text1.
3.The string returned to the console is the font name needed to programmatically set the text font.
tell application "Adobe Acrobat"
activate
set p to 1
tell document 1
tell page 1
set cbox to {}
if cbox = {} then set cbox to crop box
if cbox = {} then set cbox to bounds
if cbox = {} then set cbox to media box
if cbox = {} then set cbox to art box
if cbox = {} then set cbox to trim box
if cbox = {} then set cbox to bleed box
set p to 4
set pageTop to (item 2 of cbox) + 36
set p to 5
set item 2 of cbox to pageTop
set p to 6
set pageBottom to (item 4 of cbox)
set p to 7
set item 4 of cbox to pageBottom
set p to 8
set bounds to cbox
set p to 9
set media box to cbox
set p to 10
set crop box to cbox
set p to 11
set trim box to cbox
set p to 12
set bleed box to cbox
set p to 13
set art box to cbox
set p to 14
set theNewString to ((character id 209) & "this is a test" & (character id 211) as text)
#
# values are Text, Font, Font Size, Start Page, End Page, Text type?, Horizontal Alignment, Vertical Offset, Horizontal Offset, Vertical Height, Rotation, Current Page
set p to 15
set theResult1 to my Add_WaterMarkText(theNewString, "MRVCode128bXSA", 16, 1, 1, 1, 0, 3, 10, 0, 0, 1)
try
do script theResult1
on error errmsg
display dialog errmsg
end try
end tell
end tell
end tell
# This handler uses Java Script
#
on Add_WaterMarkText(cText, cFont, nFontSize, nS, nE, nTA, nHorizontalAlignment, nVerticalAlignment, nHorizontalValue, nVerticalValue, nR, currentPage)
set Params to "page:" & currentPage - 1 & ", " & return
# set Params to Params & "type:\"" & "Text" & "\"," & return
set Params to Params & "richText:true," & return
set Params to Params & "value:\"" & cText & "\"," & return
set Params to Params & "textFont:\"" & cFont & "\"," & return
set Params to Params & "textSize:" & nFontSize & "," & return
set Params to Params & "rect:[100, 472, 272, 400]," & return
set Params to Params & "alignment:\"" & "Left" & "\"," & return
set Params to Params & "UserName:\"" & "Text1" & "\"," & return
set Params to Params & "multiline:false," & return
set Params to Params & "lineWidth:0," & return
# set Params to Params & "display:display.visible," & return
set Params to Params & "textColor:color.black," & return
set javaScript to "this.addField({" & Params & "});"
set the clipboard to javaScript
return javaScript
end Add_WaterMarkText
The Cipboard...
this.addField({page:0,
richText:true,
value:"Ñthis is a testÓ",
textFont:"MRV Code128bXSA regular",
textSize:16,
rect:[100, 472, 272, 400],
alignment:"Left",
UserName:"Text1",
multiline:false,
lineWidth:0,
textColor:color.black,
});
Copy link to clipboard
Copied
This is not how you use the addField method. Please read its documentation carefully before using it.
You must specify 4 parameters, and 4 parameters only. The method will return a Field object (if successful) and then you'll be able to set the rest of the properties as you wish.
The field name must be a string. If can be "12" or "Text1" or whatever. I would recommend keeping it simple. Call it "Barcode1" or something like that.
The correct name for the font is hard to come by. Follow the instructions you posted to do it correctly. Any other name is not going to work.
Also, you have to remember that JS is case-sensitive, so the property "UserName" is not going to work. It's "userName".
And one more thing: If you specify richText to be true then you must provide a an array of spans to set the value, using the richValue property. I don't think this is necessary in your case. Keep it simple!
Copy link to clipboard
Copied
> the error I eventually get is that it needs a Doc set
I assume you have a document open. What is the exact wording of the error? Please, never summarise errors, only copy/paste. And include the exact command you paste. There is no scope for guessing!
Copy link to clipboard
Copied
Thank you both once again. sorry about the tardiness of my reply, by we in Oz had to sleep.
Try67, I modified my script, but still get an error 'Can't understand script message', and a debug error (below)
Test Screen Name, here's the result of my Debug. I don't know what the missing ';' really wants? I've tried putting a ';' & return in front, and now the debugger does not throw an error, but it also doesn't work. The 'run' button stays red
var f = this.addField("Text1","Ñthis is a testÓ", [0, 1298, 144, 1262], 0);
f.richText:true;
f.textSize:16;
f.alignment:"Left";
f.multiline:false;
f.lineWidth:0;
f.display:display.visible;
f.textColor:color.black;
SyntaxError: missing ; before statement
1:
SyntaxError: missing ; before statement
1:
My Code...
tell application "Adobe Acrobat"
activate
set p to 1
tell document 1
tell page 1
set cbox to {}
if cbox = {} then set cbox to crop box
if cbox = {} then set cbox to bounds
if cbox = {} then set cbox to media box
if cbox = {} then set cbox to art box
if cbox = {} then set cbox to trim box
if cbox = {} then set cbox to bleed box
set p to 4
set pageTop to (item 2 of cbox) + 36 as integer
set p to 5
set item 2 of cbox to pageTop
set p to 6
set pageBottom to (item 4 of cbox)
set p to 7
set item 4 of cbox to pageBottom
set p to 8
set bounds to cbox
set p to 9
set media box to cbox
set p to 10
set crop box to cbox
set p to 11
set trim box to cbox
set p to 12
set bleed box to cbox
set p to 13
set art box to cbox
set p to 14
set theNewString to ((character id 209) & "this is a test" & (character id 211) as text)
#
# values are Text, Font, Font Size, Start Page, End Page, Text type?, Horizontal Alignment, Vertical Offset, Horizontal Offset, Vertical Height, Rotation, Current Page
set p to 15
set theResult1 to my Add_TextBoxText(theNewString, "MRVCode128bXSA", 16, 1, 1, 1, 0, pageTop, 144, 36, 0, 1)
try
do script theResult1
on error errmsg
display dialog errmsg
end try
end tell
end tell
end tell
# This handler uses Java Script
#
on Add_TextBoxText(cText, cFont, nFontSize, nS, nE, nTA, nHorizontalAlignment, nVerticalAlignment, nHorizontalValue, nVerticalValue, nR, currentPage)
set Params to "var f = this.addField(\"" & "Text1" & "\",\"" & cText & "\", [" & nHorizontalAlignment & ", " & nVerticalAlignment & ", " & nHorizontalValue & ", " & nVerticalAlignment - nVerticalValue & "], " & currentPage - 1 & ");" & return
set Params to Params & "f.richText:true;" & return
# set Params to Params & "f.textFont:font.\"" & cFont & "\";" & return
set Params to Params & "f.textSize:" & nFontSize & ";" & return
set Params to Params & "f.alignment:\"" & "Left" & "\";" & return
set Params to Params & "f.multiline:false;" & return
set Params to Params & "f.lineWidth:0;" & return
set Params to Params & "f.display:display.visible;" & return
set Params to Params & "f.textColor:color.black;" & return
set javaScript to Params
set the clipboard to javaScript
return javaScript
end Add_TextBoxText
Added this to stop debugger throwing an error, but does not do anything now.
set Params to ";" & return
set Params to Params & "var f = this.addField(\"" & "Text1" & "\",\"" & cText & "\", [" & nHorizontalAlignment & ", " & nVerticalAlignment & ", " & nHorizontalValue & ", " & nVerticalAlignment - nVerticalValue & "], " & currentPage - 1 & ");" & return
Copy link to clipboard
Copied
Replace all colons with equals signs (=).
Also, double-check the order of the parameters of addField.
Copy link to clipboard
Copied
Oops, I realised I had left in the 'rickText' line. Now removed, and a textFont added. Debugger no longer errors, but errors, and 'do script' still throws a 'don't understand' error.
The Debugger (I've tried reducing the 1802, & 1766 to well within the page size, but still get the same error.
var f = this.addField("Text1","Ñthis is a testÓ", [0, 1802, 144, 1766], 0);
RangeError: Invalid argument value.
Doc.addField:0:
Try67, I'll re-script the colons, but my problems start before those, with the first line.
Even with all the 'f.' statements commented out, I still get an error. How on earth do I format the first line to be recognised, please?
Adobe Acrobat got an error: "var f = this.addField(\"Text1\",\"Ñthis is a testÓ\", [0, 974, 144, 938], 0);
" doesn’t understand the “do script” message.
var f = this.addField("Text1","Ñthis is a testÓ", [0, 1622, 144, 1586], 0);
tellapplication "Adobe Acrobat"
activate
set p to 1
tell document 1
tell page 1
set cbox to {}
if cbox = {} then set cbox to crop box
if cbox = {} then set cbox to bounds
if cbox = {} then set cbox to media box
if cbox = {} then set cbox to art box
if cbox = {} then set cbox to trim box
if cbox = {} then set cbox to bleed box
set p to 4
set pageTop to (item 2 of cbox) + 36 as integer
set p to 5
set item 2 of cbox to pageTop
set p to 6
set pageBottom to (item 4 of cbox)
set p to 7
set item 4 of cbox to pageBottom
set p to 8
set bounds to cbox
set p to 9
set media box to cbox
set p to 10
set crop box to cbox
set p to 11
set trim box to cbox
set p to 12
set bleed box to cbox
set p to 13
set art box to cbox
set p to 14
set theNewString to ((character id 209) & "this is a test" & (character id 211) as text)
#
# values are Text, Font, Font Size, Start Page, End Page, Text type?, Horizontal Alignment, Vertical Offset, Horizontal Offset, Vertical Height, Rotation, Current Page
set p to 15
set theResult1 to my Add_TextBoxText(theNewString, "MRVCode128bXSA", 16, 1, 1, 1, 0, pageTop, 144, 36, 0, 1)
try
do script theResult1
on error errmsg
display dialog errmsg
end try
end tell
end tell
end tell
# This handler uses Java Script
#
on Add_TextBoxText(cText, cFont, nFontSize, nS, nE, nTA, nHorizontalAlignment, nVerticalAlignment, nHorizontalValue, nVerticalValue, nR, currentPage)
set Params to ""
set Params to Params & "var f = this.addField(\"" & "Text1" & "\",\"" & cText & "\", [" & nHorizontalAlignment & ", " & nVerticalAlignment & ", " & nHorizontalValue & ", " & nVerticalAlignment - nVerticalValue & "], " & currentPage - 1 & ");" & return
set Params to Params & "f.textSize:" & nFontSize & ";" & return
set Params to Params & "f.textFont:font.\"" & cFont & "\";" & return
set Params to Params & "f.alignment:\"" & "Left" & "\";" & return
set Params to Params & "f.multiline:false;" & return
set Params to Params & "f.lineWidth:0;" & return
set Params to Params & "f.display:display.visible;" & return
set Params to Params & "f.textColor:color.black;" & return
set javaScript to Params
set the clipboard to javaScript
return javaScript
end Add_TextBoxText
Copy link to clipboard
Copied
carefully check your addField parameters against the documentation; both the type and order are important. Or is the problem that you haven't found the documentation?
Copy link to clipboard
Copied
G'day Test Screen Name. No, I haven't found any direct documentation of setting fields, only this addField scripting - Adobe Acrobat Windows and the Acrobat DC SDK Documentation, where I can't find how to set the script for a field.
Any pointers, please?
Sorry to be a nuisance.
Regards
Santa
Copy link to clipboard
Copied
I feel stupid. On re-checking my only link to an example, I realised I had transposed the page number and rect. Fixed it, and ran just the first line, with everything else commented out, and it ran with no error, but also no box drawn. Inserted the Clipboard into the Debugger, and got this...
var f = this.addField("Text1", "Ñthis is a testÓ", 0, [1, 1144, 144, 1108]);
TypeError: f is null
The error I get with then uncommenting just one line is...
I've reset every colon to '=', but still the error. Any answers, please?
Copy link to clipboard
Copied
A big 'THANK YOU' to you both. With modifications pointed out by both of you, my script now works, and it uses the free barcode font available from The code 128. (The french site).
Regards
Santa
tell application "Adobe Acrobat"
activate
set p to 1
tell document 1
tell page 1
set cbox to {}
if cbox = {} then set cbox to crop box
if cbox = {} then set cbox to bounds
if cbox = {} then set cbox to media box
if cbox = {} then set cbox to art box
if cbox = {} then set cbox to trim box
if cbox = {} then set cbox to bleed box
set p to 4
set pageTop to (item 2 of cbox) + 36 as integer
set p to 5
set item 2 of cbox to pageTop
set p to 6
set pageBottom to (item 4 of cbox)
set p to 7
set item 4 of cbox to pageBottom
set p to 8
set bounds to cbox
set p to 9
set media box to cbox
set p to 10
set crop box to cbox
set p to 11
set trim box to cbox
set p to 12
set bleed box to cbox
set p to 13
set art box to cbox
set p to 14
set theNewString to ((character id 209) & "this is a test" & (character id 211) as text)
#
# values are Text, Font, Font Size, Start Page, End Page, Text type?, Horizontal Alignment, Vertical Offset, Horizontal Offset, Vertical Height, Rotation, Current Page
set p to 15
set theResult1 to my Add_TextBoxText(theNewString, "Code128", 28, 1, 1, 1, 1, pageTop, 144, 36, 0, 1)
try
do script theResult1
on error errmsg
display dialog errmsg
end try
end tell
end tell
end tell
# This handler uses Java Script
#
on Add_TextBoxText(cText, cFont, nFontSize, nS, nE, nTA, nHorizontalAlignment, nVerticalAlignment, nHorizontalValue, nVerticalValue, nR, currentPage)
set Params to ""
set Params to Params & "var f = this.addField(\"" & "Text1" & "\", " & "\"" & "text" & "\", " & currentPage - 1 & ", [" & nHorizontalAlignment & ", " & nVerticalAlignment - 10 & ", " & nHorizontalValue & ", " & nVerticalAlignment - nVerticalValue - 10 & "]" & ");" & return
set Params to Params & "f.value = \"" & cText & "\";" & return
set Params to Params & "f.userName = \"" & "Text1" & "\";" & return
set Params to Params & "f.textSize = " & nFontSize & ";" & return
set Params to Params & "f.textFont = \"" & cFont & "\";" & return
set Params to Params & "f.alignment = \"" & "left" & "\";" & return
set Params to Params & "f.multiline = false;" & return
set Params to Params & "f.lineWidth = 0;" & return
set Params to Params & "f.display = display.visible;" & return
set Params to Params & "f.textColor = color.black;" & return
set javaScript to Params
set the clipboard to javaScript
return javaScript
end Add_TextBoxText
Copy link to clipboard
Copied
I'm sure you understand this but to avoid confusing people, what you posted isn't a JavaScript, it's an AppleScript, run from the Finder. It creates a single line that contains JavaScript. The first example tries to use do script to run it in Acrobat.
YOu ask whether the c is needed in cPage etc. Check the JavaScript API reference for the correct names, but a c like this is Not unusual. Lrt's simplify to eliminate AppleScript issues. You build a command. does it work if you paste into the JavaScript console?