Can you change the default state of a layer with javascript from a button?
I am looking to change the layer's set Default state from "off" to "on" so when saved it will remain "on".
I am looking to change the layer's set Default state from "off" to "on" so when saved it will remain "on".
Sure, let's take a simple example. You have a layer called "Layer1" and you want to set its default state using a field.
Create a (hidden) text field called "Layer1State" and add the following code as a doc-level script:
var ocgArray = this.getOCGs();
if (ocgArray!=null) {
for (var i=0; i<ocgArray.length; i++) {
if (ocgArray.name=="Layer1") {
ocgArray.state=this.getField("Layer1State").valueAsString=="1";
break;
}
}
}
Now you just need to set the value of the field as "1" if you want the layer to be visible when the file is opened, or to anything else if you want it to be hidden.
For example, if you want to save the current state of the layer when the file is saved, you can place this code under the document's Will Save event:
var ocgArray = this.getOCGs();
if (ocgArray!=null) {
for (var i=0; i<ocgArray.length; i++) {
if (ocgArray.name=="Layer1") {
this.getField("Layer1State").value = (ocgArray.state) ? "1" : "0";
break;
}
}
}
Already have an account? Login
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.