Adobe 0046100128056 Scripting Guide - Page 52
Getting the active layer, Referring to layers by layer index, Using relative references, Given a layer
UPC - 718659087562
View all Adobe 0046100128056 manuals
Add to My Manuals
Save this manual to your list of manuals |
Page 52 highlights
CHAPTER 4: Working with Layers Scripting Layers 52 Getting the active layer The active layer is the layer on which new objects are created. You can get the active layer using scripting, as shown in the following script fragment. (For the complete script, see ActiveLayer.) //Given a document "myDocument"... var myDocument = app.documents.item(0); var myLayer = myDocument.activeLayer; Referring to layers by layer index You can get a reference to a layer using the index of the layer in the layers collection of a document. The script fragment below uses the layer index to iterate through layers. (For the complete script, see HideOtherLayers.) //Given a document "myDocument"... var myTargetLayer = myDocument.activeLayer; for(var myCounter = 0; myCounter < myDocument.layers.length; myCounter++){ //If the layer is not the target layer, hide it. if(myDocument.layers.item(myCounter).name != myTargetLayer.name){ myDocument.layers.item(myCounter).visible = false; } } Note that you can use negative numbers to refer to the layers in the layers collection of a document. Layer -1 refers to the last (bottom) layer in the collection. Referring to layers by layer name You also can get a reference to a layer using the name of the layer, as shown in the following script fragment. (For the complete script, see LayerName.) var myLayer = app.documents.item(0).layers.item("Text Layer"); Using relative references Given a layer, you can refer to the layer above using the previousItem method, or refer to the layer below using the nextItem method, as shown in the following script fragment. (For the complete script, see RelativeLayerReferences.) Both methods take a reference layer as a parameter. //Given a document "myDocument"... var myLayer = myDocument.layers.item(4); myDocument.activeLayer = myLayer; var myNextLayer = myDocument.layers.nextItem(myLayer); var myPreviousLayer = myDocument.layers.previousItem(myLayer); var myString = "The layer below the target layer is " + myNextLayer.name + "\r"; myString += "The layer above the target layer is " + myPreviousLayer.name; alert(myString); The previousItem and nextItem methods return an invalid layer reference if the specified (next or previous) layer does not exist, rather than generating an error.