Skip to main content
Inspiring
July 24, 2013
Answered

How to delete layer by name?

  • July 24, 2013
  • 1 reply
  • 1956 views

Hello,

I currently have a script that deletes unused/blank/empty layers:

var layers = app.activeDocument.layers.everyItem().getElements();

for(var i=layers.length-1;i>=0;i--){

  if(layers.pageItems.length==0){

    layers.remove();

  }

}

but in addition to unused layers, I need it to delete layers which have a layer name that begins with a bracket "[". Often there are template layers in our workflow which are not needed - they are named differently but always begin with a bracket. Example: [page 2] xxx-xxx-####

I've tried frankensteining in a piece of code that Jarek provided for another script which dealt with swatch names but I'm afraid I'm rather hopeless (I am planning on becoming less hopeless... so maybe you won't always be overcome with dread upon seeing my posts... but that doesn't solve my (or your) problem now).

As always, I appreciate your consideration and help.

This topic has been closed for replies.
Correct answer TᴀW

Strange. The first time I ran the script it deleted the layer which had a name that began with a bracket. Then, gleefully, I tried it again (having replaced the bracket-named layer) and I received an error:

Error string: Object is invalid

Source: if (layers.name[0] == "["){

It does this repeatedly... it'll work the first time I've run the script (even in a new document), then I'll either ctrl-z to get the old layer back or I'll manually insert a new layer, and when I run the script again I get that errror. Thoughts?

var layers = app.activeDocument.layers.everyItem().getElements();

for(var i=layers.length-1;i>=0;i--){

  if(layers.pageItems.length==0){

    layers.remove();

  }

if (layers.name[0] == "["){

   layers.remove();

}

}


After the first appearance of the line

layers.remove();

try adding the line

continue;

The reason here may be that if a layer is empty, then it is deleted, and

so it's no good trying to access its name since it doesn't exist any

more. A continue command will force the loop to move on to the next

iteration instead of a erroneously moving on to the next if statement.

Ariel

1 reply

TᴀW
Legend
July 24, 2013

Well, the name of the layer is simply myLayer.name, which is a string.

So to check if that begins with a bracket, simply:

if (myLayer.name[0] == "[")

Ariel

Visit www.id-extras.com for powerful InDesign scripts that save hours of work — automation, batch tools, and workflow boosters for serious designers.
tlumbisAuthor
Inspiring
July 24, 2013

Thanks Ariel. I've tried the script below and I receive an error that "myLayer" is undefined. I'm very much assuming that the error is due to my above referenced hopelessness. Any course correction would be appreciated 

var layers = app.activeDocument.layers.everyItem().getElements();

for(var i=layers.length-1;i>=0;i--){

  if(layers.pageItems.length==0){

    layers.remove();

  }

  if (myLayer.name[0] == "["){

myLayer.remove();

  }

}

TᴀW
Legend
July 24, 2013

Sure -- you should substitute myLayer with the variable appropriate to

your script. So in your case:

if (layers.name[0] == "["){

layers.remove();

}

is, I think, what you're looking for.

The way your script will now work is that it will delete any layer that

is empty, and any layer whose name begins with "[", which I think is

what you want.

Ariel

Visit www.id-extras.com for powerful InDesign scripts that save hours of work — automation, batch tools, and workflow boosters for serious designers.