Skip to main content
Participating Frequently
August 10, 2023
Answered

I need script to select the the specific layer if that is empty also

  • August 10, 2023
  • 2 replies
  • 244 views

I need a Script for select the xyz layer, if that layer is empty also, I just wanted to select that particular layer, after select that layer I want to move that layer to top and to make sure that layer has been selected when that script ended. if in case that xyz layer not available in artwork, i want to create the xyz layer and to keep that on top of all layers, and to select that layer when script ended. Can anybody help me to prepare this script.

This topic has been closed for replies.
Correct answer Manan Joshi

The following might work, change the name of the layer you need to find in the 1st line of the code

var layerName = "xy"
try{
    var lyr = app.documents[0].layers.getByName(layerName)
}catch(e){
    lyr = app.documents[0].layers.add()
    lyr.name = layerName
}
lyr.move(app.documents[0], ElementPlacement.PLACEATBEGINNING)
app.activeDocument.activeLayer = lyr

-Manan

2 replies

Manan JoshiCommunity ExpertCorrect answer
Community Expert
August 11, 2023

The following might work, change the name of the layer you need to find in the 1st line of the code

var layerName = "xy"
try{
    var lyr = app.documents[0].layers.getByName(layerName)
}catch(e){
    lyr = app.documents[0].layers.add()
    lyr.name = layerName
}
lyr.move(app.documents[0], ElementPlacement.PLACEATBEGINNING)
app.activeDocument.activeLayer = lyr

-Manan

-Manan
femkeblanco
Legend
August 10, 2023

Just to define the problem more clearly, is this what you want to do (notwithstanding "selecting" the layer)?

 

Test if a layer named "xyz" exists.
(1) If this layer exists:
    (a) If this layer is empty (no pageItems and no sublayers), move this layer to the top.
    (b) If this layer is not empty, do nothing.
(2) If this layer doesn't exist, add this layer to the top.