Skip to main content
Inspiring
January 11, 2024
Answered

Does layer exist?

  • January 11, 2024
  • 2 replies
  • 521 views

Is there a in-built way in Photoshop scripting to return if a layer exists or not? Bearing in mind that there may be duplicate layer names so getByName isn't what I'm after.

 

 

function does_layer_exist(id)
{
  var originalSelectedLayer = app.activeDocument.activeLayer;
  try
  {
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putIdentifier(charIDToTypeID('Lyr '), id);
    desc1.putReference(charIDToTypeID('null'), ref1);
    executeAction(charIDToTypeID('slct'), desc1, DialogModes.NO);

    app.activeDocument.activeLayervar = originalSelectedLayer;
    return true;
  }
  catch(eek)
  {
    return false;
  }
}

 

 

This works, but I'm just wondering if there is an in-built method to call.

Curious, me, you see.

 

 

   

This topic has been closed for replies.
Correct answer r-bin
function does_layer_exist(id)
{
  //var originalSelectedLayer = app.activeDocument.activeLayer;
  try
  {
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
      ref1.putProperty (stringIDToTypeID ("property"), stringIDToTypeID ("layerID"));
    ref1.putIdentifier(charIDToTypeID('Lyr '), id);
    desc1.putReference(charIDToTypeID('null'), ref1);
    executeAction(charIDToTypeID('getd'), desc1, DialogModes.NO);

    //app.activeDocument.activeLayervar = originalSelectedLayer;
    return true;
  }
  catch(eek)
  {
    return false;
  }
}

2 replies

r-binCorrect answer
Legend
January 11, 2024
function does_layer_exist(id)
{
  //var originalSelectedLayer = app.activeDocument.activeLayer;
  try
  {
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
      ref1.putProperty (stringIDToTypeID ("property"), stringIDToTypeID ("layerID"));
    ref1.putIdentifier(charIDToTypeID('Lyr '), id);
    desc1.putReference(charIDToTypeID('null'), ref1);
    executeAction(charIDToTypeID('getd'), desc1, DialogModes.NO);

    //app.activeDocument.activeLayervar = originalSelectedLayer;
    return true;
  }
  catch(eek)
  {
    return false;
  }
}
c.pfaffenbichler
Community Expert
Community Expert
January 11, 2024

I suppose you could get the indices and check those without changing the selected Layer at all. 

 

// 2024, use it at your own risk;
if (app.documents.length > 0) {
alert (checkLayersByIdentifier (3))
};
////// collect layers with certain name //////
function checkLayersByIdentifier (theIdentifier) {
// get number of layers;
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'));
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putProperty (stringIDToTypeID ("property"), stringIDToTypeID ("layerID"));
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
if (theID == theIdentifier) {return true}
}
catch (e) {};
};
return false
};

 

Edit: Whether it’s more efficient you’d need to test; but maybe someone else has a better idea altogether. 

 

Inspiring
January 12, 2024

That is also a possibility as well. Just to give it some context. I'm working on a destructive script which wil clear out any unwanted (hidden) layers. Naturally if a layer is deleted it won't exist. Which got me thinking... How you easily determine if a layer (like a variable) still exists.

Legend
January 12, 2024

document.layers is an array, why not use that?