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.
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
...
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.
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.
Copy link to clipboard
Copied
document.layers is an array, why not use that?
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;
}
}