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
println(), 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:
This will print “My first car was a Dodge Charger”.
To implement this with Acrobat’s
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
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
How do we run this code in Acrobat?
More Differences (
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:
After executing this line, a window pops up:
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
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
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.
Thanks for this post ! It will help me a lot for my little scripting project.
I keep in mind that you offer consulting services.
I was hoping that I would be able to automate the process using the GUI, but this is not possible.
Having a dedicated introduction to this variant of JS is extremely helpful as it prevents alot of wasted time learning about the unecessary part of JS.
Many thanks once again.