These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Scripting newbie trying to compile all code at document level...HELP!

subieguy2
Registered: Apr 30 2009
Posts: 15

I am sorry this is so specific...hope that's ok. I am having trouble figuring out how to move all this to the document level. Here is what I currently have at the document level...

// CALLOUT BUTTONS

// Sizes button
var initialPosition = [ 325, 700 ];
var w = 44.65;
var h = 10.21;
var vGap = 1;
var aRect = [initialPosition[0], initialPosition[1]-(h+vGap), initialPosition[0]+w, initialPosition[1]-h-(h+vGap)];

// 001_callout
var c001 = this.addField("001_callout", "button", 1, aRect);
c001.setAction("MouseUp", "this.gotoNamedDest('view_01');");

// 002_callout
var c002 = this.addField("002_callout", "button", 1, aRect);
c002.setAction("MouseUp", "this.gotoNamedDest('view_02');");

This works fine. It creates 2 buttons and names them. Then assigns them an action to go to a destination. I would like the script to check and see if the button exist first. If it does exist assign the actions. If it does not exist create it and then assign it the actions. Keep in mind that there could potentially be up to 130 buttons this script would create. Any suggestions on creating an array on this or whatever is more efficient would be great. Again I am a newbie and get by....but never claimed my scripts were efficient. :)

Then I have this script on a button (just to test the code). I would like to move it to the document level and assign this as an action for the first button "001_callout".

// Callout 1 layers - Turns off all layers except Callout 1 and Paste 1st (Data)

var CalloutOne = "Callout 1";
var PasteFirst = "Paste 1st";
var ocgArray = this.getOCGs(this.pageNum);

for ( var i=0; i < ocgArray.length; i++) {
if ( ocgArray[i].name == CalloutOne ) {
ocgArray[i].state = true;
}
else ocgArray[i].state = false;
}

for ( var z=0; z < ocgArray.length; z++) {
if ( ocgArray[z].name == PasteFirst ) {
ocgArray[z].state = true;
}
}

Over all what I want this to do is.....look to see if the button "xxx_callout" exist. If it DOES NOT exist....create it.
Once the button is created I want it to have actions assigned to it. I want it to go to "view_xx" and turn off all the layers on that page except for "Callout 1" and "Paste 1st".

Any help someone can provide would be greatly appreciated! Even if it is a small part of it...like writing an array to create the buttons instead of the long drawn out way.

PLEASE HELP!

THANKS IN ADVANCE!

My Product Information:
Acrobat 3D 8.1.2, Windows
subieguy2
Registered: Apr 30 2009
Posts: 15
No one has any suggestions or help?

I would settle for the name of a good book that will help me figure this out. please help!
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Simply moving the code to a document script is pretty easy. Just place it in all in a function, then call the function from a button script. In fact, when you create a document script Acrobat automatically creates a default function definition. So you could use this

To check to see if a button already exists call the "doc.getField()" fucntion.

There are a couple of points you might find helpful. 1) JavaScript code is not compiled. It's interpreted. 2) The activity this script performs, adding a group of navigation buttons to the PDF, is generally the type of thing that's done to prepare a document for distribution by running the code from an automation tool, or from the console window. It's not something that's done at runtime. Especially since it's not code that will work very well in Reader. Is there a particular reason you want to put this code in the document?

Here's an article on using the console window. This is the most important JavaScript developement tool in Acrobat.
http://www.acrobatusers.com/tutorials/2006/javascript_console

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

try67
Online
Expert
Registered: Oct 30 2008
Posts: 2399
I can't test it right now (maybe later), but you should be able to do it with the getField() method.
The only thing I'm not sure about is what will happen if you try to use it on a field name that doesn't exist, but it will probably either create an Exception (which you can catch), or return null. If any case you should be able to then create your field and assign its action.
By the way, you really should combine the two loops in your script to something like this:

for ( var i=0; i < ocgArray.length; i++) {
if ( ocgArray[i].name == CalloutOne ) {
ocgArray[i].state = true;
}
else {ocgArray[i].state = false;}
} else {
if ( ocgArray[z].name == PasteFirst ) {
ocgArray[z].state = true;
}
}
}

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com