Check if layers exist, remove layers, or move on
I created a simple script to remove layers.
app.activeDocument.layers.getByName('Engineering Part Numbers').remove();
It worked fine, but if the layers don't exist, I get an error "No such element". So I built up the script to check the layer, if it exists, remove it, else give me an alert. I don't really need the alert, but I thought I'd put it in there for testing.
I've looked around and couldn't find what I need, other than Photoshop or InDesign. But they don't seem to be the same as Illustrator. I'm assuming I am either not using the if/else incorrectly, or maybe a For statement with Var to define the layer. Just not certain how to do that, and I find the documentation very lacking. Unfortunately, I started off small and kept building, now I'm so lost, I don't know where to go.
This is the start of a much larger script if you were wondering.
I am using Windows 10 and Illustrator CC2017
if (app.activeDocument.layers.getByName('Engineering Part Numbers') == true) {
app.activeDocument.layers.getByName('Engineering Part Numbers').remove();
alert("Engineering Part Numbers is Gone");
} else {
alert("No Engineering Part Numbers Layer");
}
Thanks everyone.
