Skip to main content
Known Participant
July 23, 2019
Answered

Delete a specific layer, depending on its name

  • July 23, 2019
  • 1 reply
  • 572 views

greetings everyone,

I'm struggling with a not so difficult problem (I guess).

I need to delete two layers from my document, one named "SIGNATURE" and the other one named with the title of the document.

The problem is that sometimes, the layer won't be present (that's why I've added the makeActiveByName function) but also sometimes, the layer will be into a sublayer (with a different sublayer name for each document ... ).

How can I ask to search and delete the layer trough all the artlayers, including the sub-layers ??

The best might be to move every sub-layer from layerset to the document.

Many thanks in advance

So the code is :

// Set the makeActiveByName function

function makeActiveByName( lyrName ){ 

     try{ 

          var desc = new ActionDescriptor(); 

               var ref = new ActionReference(); 

               ref.putName( charIDToTypeID( "Lyr " ), lyrName ); 

          desc.putReference( charIDToTypeID( "null" ), ref ); 

          desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 

          executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO ); 

          return activeDocument.activeLayer; 

     }catch(e){} 

}; 

// Create the Layer set "CALQUES"

var CALQUES = doc.layerSets.add(); 

CALQUES.name = 'CALQUES';

// Delete the Signature (if present)

var targetLayer = makeActiveByName("SIGNATURE");// returns undefined if layer not found 

if(undefined!=targetLayer){;// if layer was found 

doc.artLayers.getByName("SIGNATURE").remove();

}

// Delete the Title (if present)

var DocName = doc.name

var DocNameshort = DocName.left(6)

var targetLayer = makeActiveByName(DocNameshort);

if(undefined!=targetLayer){

doc.artLayers.getByName(DocNameshort).remove();

}

This topic has been closed for replies.
Correct answer r-bin

You have to use

targetLayer.remove();  


instead of

doc.artLayers.getByName("SIGNATURE").remove();  

or

doc.artLayers.getByName(DocNameshort).remove();


If you replace

charIDToTypeID( "slct" )


with

charIDToTypeID("Dlt ")

or

stringIDToTypeID("delete")


then you’ll have a function to delete a layer by name. Layer can be located anywhere, and it does not need to activate.
The lowest layer is deleted if there are several layers with this name.

1 reply

r-binCorrect answer
Legend
July 23, 2019

You have to use

targetLayer.remove();  


instead of

doc.artLayers.getByName("SIGNATURE").remove();  

or

doc.artLayers.getByName(DocNameshort).remove();


If you replace

charIDToTypeID( "slct" )


with

charIDToTypeID("Dlt ")

or

stringIDToTypeID("delete")


then you’ll have a function to delete a layer by name. Layer can be located anywhere, and it does not need to activate.
The lowest layer is deleted if there are several layers with this name.