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

Lock a layer with custom button

Bonurbe
Registered: Mar 3 2008
Posts: 10

Hi,
 
I'm looking at creating a custom button that would enable to lock a specific layer automatically.
 
Here's the code I have to lock the specific layer. Any idea if that would be sufficient?
 
var ocgArray = this.getOCGs();
for (var i=0; i < ocgArray.length; i++) {
if (ocgArray[i].name == "skyline") ocgArray[i].locked = true;
}
app.addToolButton({
cName: "myToolButton",
cExec: "app.alert('Button was pressed')",
cTooltext: "Skyline Button",
cEnable: true,
nPos: 0
});
 
Thanks,
Bruno

My Product Information:
Acrobat Pro 9.4.3, Macintosh
try67
Expert
Registered: Oct 30 2008
Posts: 2398
This code is a bit strange. Do you want to add a tool button, or do you want to run this code from a button in a form, for example?
If the former, then you need to place the first part, the part that locks the layer, inside a function and call it using the cExec parameter. Also, move the alert to the body of the function.

If the latter, then what's the point of adding a tool button?

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

Bonurbe
Registered: Mar 3 2008
Posts: 10
I want to add a tool button. My knowledge of JavaScript is very limited and I'm reading the AcroJSGuide right now, so this is kind of a test for me.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
In that case, the problem with your code is that it will get executed when the application opens, not when you click the button. That's why you need to place it inside a function, and then call that function from the button, like so:
  1. function lockLayer() {
  2. var ocgArray = this.getOCGs();
  3. for (var i=0; i < ocgArray.length; i++) {
  4. if (ocgArray[i].name == "skyline") ocgArray[i].locked = true;
  5. }
  6. }
  7.  
  8. app.addToolButton({
  9. cName: "myToolButton",
  10. cExec: "lockLayer()",
  11. cTooltext: "Skyline Button",
  12. cEnable: "app.doc!=null", // only enable the button if there's an open file
  13. nPos: 0
  14. });

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

Bonurbe
Registered: Mar 3 2008
Posts: 10
Excellent! Thanks a lot try67 for your great help.