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

Assign javascript to all button actions

jasond
Registered: Jan 15 2009
Posts: 7

Hello, I created a script that's manually added to each button that will make an associated image (button icon) visible or invisible. How would I go about making the script document wide (once) so I don't have to keep pasting the same code on each button? There may be dozens of these buttons in a single pdf so manually adding them would be tedious.

curName=event.target.name.substring(6,10); // name of button hit, ie. "toggle1"
//turn on/off associated image
if (getField("img"+curName).display == 1) {
    getField("img"+curName).display = display.visible;
}
else {
    getField("img"+curName).display = display.hidden;
}

Any suggestions would be greatly appreciated.

Thanks,
Jason

My Product Information:
Acrobat Pro 9.0, Windows
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You can use JavaScript to apply a 'setAction()' to a field. The example in the Acrboat JavaScirpt refence shows how to add a button and assign a script to the button. Acrobat JavaScript can also provide one with all the fields and the type of field, so it should be possible to find all the button fields with "toggle" in the name starting at position 6.

George Kaiser

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
You should also consider making this into a document-level function (Advanced > Document Processing > Document JavaScripts > Add). You can then simply call the function from each of the buttons.George
jasond
Registered: Jan 15 2009
Posts: 7
George, I'm quite new to Acrobat Javascript - can I simply paste my existing code into the javascript window or do I have to alter the script to work from the document level? ie. would the document level script know which button is being pressed (event.target.name as above)??

Thanks,
Jason
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You can use the following scripts as a document level script to build the name and toggle the field display:

// document level function
function getToggleName() {
// get event target name
return "img" + event.target.name.substring(6); // field name
}


function ToggleField(sField) {
if (this.getField(sField).display == display.hidden) {
this.getField(sField).display = display.visible;
}
else {
this.getField(sField).display = display.hidden;
}
return true;
}


And then the button code bcomes:

ToggleField(getToggleName());

Calls the 2 functions. First the "getToggleName()" to build the name and return the field name to the "ToggleField()" function.

George Kaiser

jasond
Registered: Jan 15 2009
Posts: 7
That code works great, thank you! I modified it to hide icon fields before opening the target one. Next task is to create a button in the Acrobat menu to automatically insert the document js code, 1 toggle button and one icon.

Thanks again,
Jason
jasond
Registered: Jan 15 2009
Posts: 7
Just to follow up on this topic, I've been able to add the required fields and took it a little further to add an item to Acrobat's Document menu using an external javascript file. The problem I'm running into now is that the javascript file isn't being inserted as a document script. Any ideas for how I can fix this? Basically I'm trying to keep the users away from having to paste javascript code.

Thanks again!
Jason