Copy link to clipboard
Copied
I'm attempting to write a JavaScript for a custom action to create several fields that I often use. I'm first experimenting by using the console window.
I've written the following:
var name = "KPOR";
var type = "checkbox";
var page = 0;
var location = [78.48, 702.72, 93.6, 689.04];
var KPOR = this.addField(name, type, page, location);
if (KPOR !== null) {
KPOR.fillColor = color.white;
KPOR.strokeColor = color.black;
}
The field was created correctly, however for some reason the console returned "G,0". I would love if someone could explain where that could be coming from.
To that end... is there any sort of index or documentation on the console and interpreting the errors/messages it returns? I don't know where else to turn when something like this happens.
Thank you!
The last line of code in your scripts sets the "color.black", which is the value of executing the line, which is [G,0]. Verify by running the last line by itself.
AFAIK there is no JS error reference, but they are usually self evident.
Copy link to clipboard
Copied
The last line of code in your scripts sets the "color.black", which is the value of executing the line, which is [G,0]. Verify by running the last line by itself.
AFAIK there is no JS error reference, but they are usually self evident.
Copy link to clipboard
Copied
Thank you! I ran that line by itself and it returned the same thing. Is there a way to avoid that?
Copy link to clipboard
Copied
There's no reason to, but you can put the code in an anonymous function (it will still return "undefined", ie nothing, though):
(function () {
var name = "KPOR";
var type = "checkbox";
var page = 0;
var location = [78.48, 702.72, 93.6, 689.04];
var KPOR = this.addField(name, type, page, location);
if (KPOR !== null) {
KPOR.fillColor = color.white;
KPOR.strokeColor = color.black;
}
})();
Copy link to clipboard
Copied
Here's an old tutorial on the console window:
The Acrobat JavaScript Console Window - YouTube
Copy link to clipboard
Copied
Let me add some more information to what Thom said.
Whenever you run code in the JavaScript console, the console will report the return value of your code. Oftentimes there is none, and you find "undefined" - something that kind of looks like an error message, but in this case it is not. It's just the JavaScript interpreter saying that there is nothing that gets returned.
You can play with this by running the following code snippets in the console:
3
Yes, this is valid JavaScript code, and the interpreter will print "3", because that is what this one character JavaScript returned.
3 + 4
In this case, you are actually performing a calculation, and the interpreter will report that the result is 7.
app.alert("This is a test");
This returns "1" - the big question now is why is it doing this? When you check the API documentation, you will find that app.alert() returns the button that the user pressed, and 1 stands for the OK button.
Your code created a return value and the console printed it.
Copy link to clipboard
Copied
Thank you Karl! JavaScript has been a total learn-as-you-go experience for me. I know just enough to understand the concept of a return value, but not enough to know what methods/properties will create them. Surprises around every corner!