This is a bit longer than usual, so let me add a table of contents here that allows you to jump straight to the section you are interested in.
- Differences (
- More Books
- How do we run this code in Acrobat?
- More Differences (
- Further Steps
- Application specific extensions
JSON, so when you have to perform date calculations for example, you can do this by just looking up what methods the
Date object provides.
I am only aware of a couple of resources that provide a fairly clean breakdown of just the core language (that does not mean that there are not more, but I have not seen them. If you know of one, please post in the comments):
- Kyle Simpson: You Don’t Know JS: Up & Going
Flanagan’s book is the definitive guide, with a large chapter about the core language, but it’s a bit dry and probably not well suited for somebody who is just starting out. For a programmer with a good foundation in any other programming language, this would be a great resource. Simpson’s book is a short introduction into the core language. You can run all the examples from both books in Acrobat if you keep a few simple rules in mind:
console object does not support the
log() method. Instead of
console.log(“abc”); you will have to use
This will work in most cases, but the
log() method is a bit more powerful than Acrobat’s
writeln(), so you may end up with a few examples for which you will have to modify the arguments to the
log call (even though I did not find any when I browsed through the examples in both books):
console.log() concatenates it’s arguments
This will print the line “abc def” – it will concatenate the individual strings. This also works with variables:
var car = "Dodge Charger"; console.log(“My first car was a", car);
This will print “My first car was a Dodge Charger”.
To implement this with Acrobat’s
var car = "Dodge Charger"; console.log(“My first car was a " + car);
I had to add a space at the end of the first string to get the same output as with log().
console.log() allows substitution strings
var n = 5; var myName = “John Doe”; console.log(“My name is %s and %d squared is %d”, byname, 5, 5*5);
This can be rewritten using Acrobat’s
In addition to the
console.log() function, you also need to change all instances of
prompt() as explained below.
For any other resource, you have to take the examples presented, and covert them to what Acrobat expects you to use. I’ve looked at two more books that seem to give a reasonably good introduction into the core language, but you will have to pick and choose which areas you need to skip.
You can open this as example_1.html and see what it does in your browser.
The only thing that is interesting for us is the text inside the
<script> tag – that is everything between
alert("This is a message");
How do we run this code in Acrobat?
More Differences (
alert("This is a message"); ReferenceError: alert is not defined 1:Console:Exec undefined
alert() function. Acrobat does however provide very similar functionality via the
app.alert() method. See the description in the SDK documentation for more information. We can use the simplest form of
app.alert() to replace the
alert() call in our example:
app.alert("This is a message");
After executing this line, a window pops up:
app.alert("This is a message"); 1
The first line again is the code I am executing, the second line shows the return value of what got executed. From the API documentation (see link above), we learn that a return code of “1” means that the “OK” button was pressed (which is actually the only button that was on our dialog, but the
app.alert() method allows to add more than just one button).
prompt() function, which again does not exist in Acrobat (this is example2.html):
And just as before, the code we are interested in is within the
var val = prompt("Enter a value"); alert("You entered " + val);
We already know what to do with the second line, to replace the
prompt() function call with something that Acrobat understands, we will use the
var val = app.response("Enter a value"); app.alert("You entered " + val);
This results in these two windows being displayed:
Any time a script references
Full disclosure: Some of the links to books on this page use my Amazon affiliate link, so when you order through one of these links, I will get a few cents.