Например, вот так:
function selectByID(id)
{
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID('Lyr '), id);
desc.putReference(charIDToTypeID('null'), ref);
executeAction(charIDToTypeID('slct'), desc, DialogModes.NO);
};
function traverseAllLayers(n)
{
try
{
activeDocument.backgroundLayer;
var layers = 0
}
catch (e)
{
var layers = 1;
};
while (true)
{
ref = new ActionReference();
ref.putIndex(charIDToTypeID('Lyr '), layers);
try
{
var desc = executeActionGet(ref);
}
catch (err)
{
break;
}
if (desc.getBoolean(charIDToTypeID("Vsbl")) && desc.getInteger(stringIDToTypeID("layerKind")) != 13) {
var myId = desc.getInteger(stringIDToTypeID( 'layerID' ));
selectByID(myId);
activeDocument.activeLayer.name = activeDocument.name
}
layers++;
}
};
app.activeDocument.suspendHistory("temp", "traverseAllLayers()");