• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
2

Does layer exist?

Engaged ,
Jan 11, 2024 Jan 11, 2024

Copy link to clipboard

Copied

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.

 

 

   

TOPICS
Actions and scripting

Views

153

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

People's Champ , Jan 11, 2024 Jan 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 = originalSelecte
...

Votes

Translate

Translate
Adobe
Community Expert ,
Jan 11, 2024 Jan 11, 2024

Copy link to clipboard

Copied

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. 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 12, 2024 Jan 12, 2024

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 12, 2024 Jan 12, 2024

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jan 11, 2024 Jan 11, 2024

Copy link to clipboard

Copied

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;
  }
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines