One way of getting layers from a layerset with AM

Photoshop Script Snippets - Note: Full Scripts go in the Photoshop Scripts Forum

Moderators: xbytor, DavideBarranca, Paul MR

Paul MR

One way of getting layers from a layerset with AM

Postby Paul MR » Sat Nov 03, 2012 6:18 pm

Here is one way of getting all layers from a layerset using layer colours as markers...

Code: Select all#target photoshop
app.bringToFront();
main();
function main(){
if(!documents.length) return;
//array of all layers
var allLayers = getNamesPlusIDs();
var LSets =[];
//get a list of all layer sets
for(var z in allLayers){
   if(allLayers[z][2].toString() == "true") LSets.push(allLayers[z]);
    }
//get all layers in the first layer set.
if(LSets.length>0) alert(getLayerSetLayers(Number(LSets[0][0]),allLayers).join('\r'));
}

function getLayerSetLayers(ID,arr){
//change all layer colours to none
setNoLayerColour(arr);
//set the layer set colour to red
putLayerColourByID(Number(ID),"red");
   var ref = new ActionReference();
   ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
   var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1;
   var Names=[];
try{
    activeDocument.backgroundLayer;
var i = 0; }catch(e){ var i = 1; };
   for(i;i<count;i++){
       if(i == 0) continue;
        ref = new ActionReference();
        ref.putIndex( charIDToTypeID( 'Lyr ' ), i );
        var desc = executeActionGet(ref);
        var layerName = desc.getString(charIDToTypeID( 'Nm  ' ));
        var Id = desc.getInteger(stringIDToTypeID( 'layerID' ));
        if(Number(ID) == Number(Id) ) continue;
        var Colour =  typeIDToStringID(desc.getEnumerationValue(stringIDToTypeID('color')));
        if(layerName.match(/^<\/Layer group/) ) continue;
        var layerType = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'layerSection' )));
        var isLayerSet =( layerType == 'layerSectionContent') ? false:true;
        if(Colour.toString() == "red") Names.push([[Id],[layerName],[isLayerSet]]);
   };
//reset layer colours to what they were
resetLayerColour(arr);
//return array of layers from the selected layer set
return Names;
};
function setNoLayerColour(arr){
for(var a in arr){
    putLayerColourByID(Number(arr[a][0]),"none");
    }
}
function resetLayerColour(arr){
for(var a in arr){
    putLayerColourByID(Number(arr[a][0]),arr[a][3].toString());
    }
}
function getLayerColourByID( ID ) {
    var ref = new ActionReference();
    ref.putIdentifier(charIDToTypeID('Lyr '), ID);
    return typeIDToStringID(executeActionGet(ref).getEnumerationValue(stringIDToTypeID('color')));
};
function putLayerColourByID(ID,Colour) {
// "none","red","orange","yellowColor","grain","blue","violet","gray"
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putIdentifier(charIDToTypeID('Lyr '), Number(ID));
    desc.putReference( charIDToTypeID('null'), ref );
        var desc2 = new ActionDescriptor();
        desc2.putEnumerated( charIDToTypeID('Clr '), charIDToTypeID('Clr '), stringIDToTypeID(Colour) );
    desc.putObject( charIDToTypeID('T   '), charIDToTypeID('Lyr '), desc2 );
    executeAction( charIDToTypeID('setd'), desc, DialogModes.NO );
};
function getNamesPlusIDs(){
   var ref = new ActionReference();
   ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
   var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1;
   var Names=[];
try{
    activeDocument.backgroundLayer;
var i = 0; }catch(e){ var i = 1; };
   for(i;i<count;i++){
       if(i == 0) continue;
        ref = new ActionReference();
        ref.putIndex( charIDToTypeID( 'Lyr ' ), i );
        var desc = executeActionGet(ref);
        var layerName = desc.getString(charIDToTypeID( 'Nm  ' ));
        var Id = desc.getInteger(stringIDToTypeID( 'layerID' ));
        var Colour = typeIDToStringID(desc.getEnumerationValue(stringIDToTypeID('color')));
        if(layerName.match(/^<\/Layer group/) ) continue;
        var layerType = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'layerSection' )));
        var isLayerSet =( layerType == 'layerSectionContent') ? false:true;
Names.push([[Id],[layerName],[isLayerSet],[Colour]]);
   };
return Names;
};



Mike Hale

One way of getting layers from a layerset with AM

Postby Mike Hale » Sat Nov 03, 2012 8:17 pm

Here is another way that I have been working off and on for a while. It will get the AM index of the artLayers in a set and has the option to include/exclude nested layerSets.
Code: Select allfunction getActiveLayerIndex() {
    var ref = new ActionReference();
    ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
    ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
    return executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ));
};
function getActiveLayerSection() {
    var ref = new ActionReference();
    ref.putProperty( charIDToTypeID("Prpr") , stringIDToTypeID("layerSection"));
    ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
    return typeIDToStringID(executeActionGet(ref).getEnumerationValue(stringIDToTypeID("layerSection")));
};
function getLayerLayerSectionByIndex( index ) {        
   var ref = new ActionReference();
   ref.putProperty( charIDToTypeID("Prpr") , stringIDToTypeID("layerSection"));
   ref.putIndex(charIDToTypeID("Lyr "), index);
   return typeIDToStringID(executeActionGet(ref).getEnumerationValue(stringIDToTypeID("layerSection")));
};
function getLayerNameByIndex( index ) {
    var ref = new ActionReference();
    ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Nm  " ));
    ref.putIndex( charIDToTypeID( "Lyr " ), index );
    return executeActionGet(ref).getString(charIDToTypeID( "Nm  " ));;
};
function skipNestedSets( layerIndex ){
   var isEnd = false;
   layerIndex = app.activeDocument.layers[app.activeDocument.layers.length-1].isBackgroundLayer ? layerIndex-2:layerIndex;
   while(!isEnd){
      layerIndex--;
      if( getLayerLayerSectionByIndex( layerIndex ) == 'layerSectionStart' ) layerIndex = skipNestedSets( layerIndex );
      isEnd = getLayerNameByIndex(layerIndex) == "</Layer group>" ? true:false;
   }
   return layerIndex-2;
};
function getChildIndex( skipNested ){
   var layerSetIndex = getActiveLayerIndex();
   layerSetIndex = app.activeDocument.layers[app.activeDocument.layers.length-1].isBackgroundLayer ? layerSetIndex-1:layerSetIndex;
   var isEndOfSet = false;
   var layerIndexArray = [];
   while(!isEndOfSet){
      layerSetIndex--;
      if( getLayerLayerSectionByIndex( layerSetIndex ) == 'layerSectionStart' && skipNested ){
         layerSetIndex = skipNestedSets( layerSetIndex );
      }
      isEndOfSet = getLayerNameByIndex(layerSetIndex) == "</Layer group>" ? true:false;
      layerIndexArray.push( layerSetIndex );
   }
   return layerIndexArray;
};
getChildIndex(false);// boolean true to skip nested layerSets

Paul MR

One way of getting layers from a layerset with AM

Postby Paul MR » Sat Nov 03, 2012 9:39 pm

I think you are nearly there Mike! I am getting an error on one layerset that has a nested layerset. It works fine getting all layers but skiping nested it errors on getting a layer name with an index of -1

Mike Hale

One way of getting layers from a layerset with AM

Postby Mike Hale » Sat Nov 03, 2012 10:29 pm

Can you outline the layer structure of the document that is giving you errors? It could be you have an edge case that I haven't run into.

Mike Hale

One way of getting layers from a layerset with AM

Postby Mike Hale » Sat Nov 03, 2012 10:29 pm

Can you outline the layer structure of the document that is giving you errors? It could be you have an edge case that I haven't run into.

Paul MR

One way of getting layers from a layerset with AM

Postby Paul MR » Sat Nov 03, 2012 11:39 pm

Its just a test psd select the layerset group 3 and run the code Mike.

Paul MR

One way of getting layers from a layerset with AM

Postby Paul MR » Sat Nov 03, 2012 11:42 pm

Here is another version using layer colours, this version assumes that no layer colours are used in the psd.

Code: Select all#target photoshop
app.bringToFront();
main();
function main(){
if(!documents.length) return;
//array of all layers
var allLayers = getNamesPlusIDs();
var LSets =[];
//get a list of all layer sets
for(var z in allLayers){
   if(allLayers[z][2].toString() == "true") LSets.push(allLayers[z]);
    }
//get all layers in the first layer set.
alert(getLayers(Number(LSets[2][0]),false).join('\r'));

function getLayers(ID,nest){//nest true = include nested layers
if(nest == undefined) nest =false;
createNamedSnapshot("temp");
selectLayerById(Number(ID));
putLayerColourByID(ID,"red");
var selectedLayerset = getLayerSetLayers(ID,allLayers);
if(!nest){
nested = false;
for(var z in selectedLayerset){
    if(selectedLayerset[z][2].toString() == "true"){
        nested = true;
        selectLayerById(Number(selectedLayerset[z][0]));
        putLayerColourByID(Number(selectedLayerset[z][0]),"blue");
        }
    }
selectedLayerset = getLayerSetLayers(ID,allLayers);
}
revertNamedSnapshot("temp");
removeNamedSnapshot("temp");
return selectedLayerset;
}
}
function selectLayerById(ID, add) {
    add = (add == undefined)  ? add = false : add;
   var ref = new ActionReference();
   ref.putIdentifier(charIDToTypeID('Lyr '), ID);
   var desc = new ActionDescriptor();
   desc.putReference(charIDToTypeID('null'), ref);
   if (add) {
      desc.putEnumerated(stringIDToTypeID('selectionModifier'), stringIDToTypeID('selectionModifierType'), stringIDToTypeID('addToSelection'));
   }
   desc.putBoolean(charIDToTypeID('MkVs'), false);
   executeAction(charIDToTypeID('slct'), desc, DialogModes.NO);
}
function getLayerSetLayers(ID,arr){
   var ref = new ActionReference();
   ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
   var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1;
   var Names=[];
try{
    activeDocument.backgroundLayer;
var i = 0; }catch(e){ var i = 1; };
   for(i;i<count;i++){
       if(i == 0) continue;
        ref = new ActionReference();
        ref.putIndex( charIDToTypeID( 'Lyr ' ), i );
        var desc = executeActionGet(ref);
        var layerName = desc.getString(charIDToTypeID( 'Nm  ' ));
        var Id = desc.getInteger(stringIDToTypeID( 'layerID' ));
        if(Number(ID) == Number(Id) ) continue;
        var Colour =  typeIDToStringID(desc.getEnumerationValue(stringIDToTypeID('color')));
        if(layerName.match(/^<\/Layer group/) ) continue;
        var layerType = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'layerSection' )));
        var isLayerSet =( layerType == 'layerSectionContent') ? false:true;
        if(Colour.toString() == "red") Names.push([[Id],[layerName],[isLayerSet]]);
   };
//return array of layers from the selected layer set
return Names;
};
function setNoLayerColour(arr){
for(var a in arr){
    putLayerColourByID(Number(arr[a][0]),"none");
    }
}
function resetLayerColour(arr){
for(var a in arr){
    putLayerColourByID(Number(arr[a][0]),arr[a][3].toString());
    }
}
function getLayerColourByID( ID ) {
    var ref = new ActionReference();
    ref.putIdentifier(charIDToTypeID('Lyr '), ID);
    return typeIDToStringID(executeActionGet(ref).getEnumerationValue(stringIDToTypeID('color')));
};
function putLayerColourByID(ID,Colour) {
// "none","red","orange","yellowColor","grain","blue","violet","gray"
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putIdentifier(charIDToTypeID('Lyr '), Number(ID));
    desc.putReference( charIDToTypeID('null'), ref );
        var desc2 = new ActionDescriptor();
        desc2.putEnumerated( charIDToTypeID('Clr '), charIDToTypeID('Clr '), stringIDToTypeID(Colour) );
    desc.putObject( charIDToTypeID('T   '), charIDToTypeID('Lyr '), desc2 );
    executeAction( charIDToTypeID('setd'), desc, DialogModes.NO );
};
function getNamesPlusIDs(){
   var ref = new ActionReference();
   ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
   var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1;
   var Names=[];
try{
    activeDocument.backgroundLayer;
var i = 0; }catch(e){ var i = 1; };
   for(i;i<count;i++){
       if(i == 0) continue;
        ref = new ActionReference();
        ref.putIndex( charIDToTypeID( 'Lyr ' ), i );
        var desc = executeActionGet(ref);
        var layerName = desc.getString(charIDToTypeID( 'Nm  ' ));
        var Id = desc.getInteger(stringIDToTypeID( 'layerID' ));
        var Colour = typeIDToStringID(desc.getEnumerationValue(stringIDToTypeID('color')));
        if(layerName.match(/^<\/Layer group/) ) continue;
        var layerType = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'layerSection' )));
        var isLayerSet =( layerType == 'layerSectionContent') ? false:true;
Names.push([[Id],[layerName],[isLayerSet],[Colour]]);
   };
return Names;
};
function removeNamedSnapshot(name) {
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putName( charIDToTypeID('SnpS'), name );
    desc.putReference( charIDToTypeID('null'), ref );
    executeAction( charIDToTypeID('Dlt '), desc, DialogModes.NO );
}
function revertNamedSnapshot(name) {
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putName( charIDToTypeID('SnpS'), name );
    desc.putReference( charIDToTypeID('null'), ref );
    executeAction( charIDToTypeID('slct'), desc, DialogModes.NO );
}
function createNamedSnapshot(name) {
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putClass( charIDToTypeID('SnpS') );
    desc.putReference( charIDToTypeID('null'), ref );
        var ref1 = new ActionReference();
        ref1.putProperty( charIDToTypeID('HstS'), charIDToTypeID('CrnH') );
    desc.putReference( charIDToTypeID('From'), ref1 );
    desc.putString( charIDToTypeID('Nm  '), name );
    desc.putEnumerated( charIDToTypeID('Usng'), charIDToTypeID('HstS'), charIDToTypeID('FllD') );
    executeAction( charIDToTypeID('Mk  '), desc, DialogModes.NO );
}

Paul MR

One way of getting layers from a layerset with AM

Postby Paul MR » Sun Nov 04, 2012 9:26 pm

Here is a better example that gets all layersets layers and demonstrates how to store and retrieve them.
This script is not suitable if you are using Layer Colours in your PSD.

Code: Select all/*
    N.B. This script requires that no layers have been coloured Red!
    This example extracts the layersets layers and omits nested layers
*/
#target photoshop
app.bringToFront();
activeDocument.suspendHistory('LayerSet Example', 'main()');
function main(){
if(!documents.length) return;
if(activeDocument.layerSets.length < 1) return;
//Get an array of all layers
var allLayers = getNamesPlusIDs();
var LSets =[];
//Get a list of all layer sets
for(var z in allLayers){
   if(allLayers[z][2].toString() == "true") LSets.push(allLayers[z]);
    }
//Used to store array of arrays using the layersets ID as it's position.
var LS=[];
for(var f in LSets){//store all layerset layers in array LS.
//Get all layers in layerset
var tmpLayer = getLayers(Number(LSets[f][0]),false);
LS[Number(LSets[f][0])] = tmpLayer;
}
/////////////////////////////////////////////////////////////////////////////////////////////
//All information has been retrieved, now show it.
/////////////////////////////////////////////////////////////////////////////////////////////
var win = new Window( 'dialog', 'Layer Test' );
g = win.graphics;
var myBrush = g.newBrush(g.BrushType.SOLID_COLOR, [0.99, 0.99, 0.99, 1]);
g.backgroundColor = myBrush;
win.p1= win.add("panel", undefined, undefined, {borderStyle:"black"});
win.g1 = win.p1.add('group');
win.g1.orientation = "row";
win.title = win.g1.add('statictext',undefined,'LayerSet Example');
win.title.alignment="fill";
var g = win.title.graphics;
g.font = ScriptUI.newFont("Georgia","BOLDITALIC",22);
win.g5 =win.p1.add('group');
win.g5.orientation = "row";
win.g5.alignment='fill';
win.g5.spacing=10;
win.g5.dd1 = win.g5.add('dropdownlist');
win.g5.dd1.preferredSize=[150,20];
win.g5.dd2 = win.g5.add('dropdownlist');
win.g5.dd2.preferredSize=[150,20];
for(var d in LSets){win.g5.dd1.add('item',LSets[d][1].toString());}
win.g5.dd1.selection = 0;
var tmpArr = LS[Number(LSets[0][0])];
for(var h in tmpArr){win.g5.dd2.add('item',tmpArr[h][1].toString());}
win.g5.dd2.selection = 0;
win.g10 =win.p1.add('group');
win.g10.bu1 = win.g10.add('button',undefined,'Done');
win.g10.bu1.preferredSize=[300,30];
win.g5.dd1.onChange=function(){
win.g5.dd2.removeAll(); 
var tmpArr = LS[Number(LSets[win.g5.dd1.selection.index][0])];
for(var h in tmpArr){win.g5.dd2.add('item',tmpArr[h][1].toString());}
win.g5.dd2.selection = 0;
}
win.g10.bu1.onClick=function(){
    win.close(0);
}
win.center();
win.show();
/////////////////////////////////////////////////////////////////////////////////////////////////
function getLayers(ID,nest){//nest true = include nested layers
if(nest == undefined) nest =false;
selectLayerById(Number(ID));
putLayerColourByID(ID,"red");
var selectedLayerset = getLayerSetLayers(ID,allLayers);
if(!nest){
nested = false;
for(var z in selectedLayerset){
    if(selectedLayerset[z][2].toString() == "true"){
        nested = true;
        selectLayerById(Number(selectedLayerset[z][0]));
        putLayerColourByID(Number(selectedLayerset[z][0]),"blue");
        }
    }
selectedLayerset = getLayerSetLayers(ID,allLayers);
}
selectLayerById(Number(ID));
putLayerColourByID(ID,"none");
return selectedLayerset;
}
}
function selectLayerById(ID, add) {
    add = (add == undefined)  ? add = false : add;
   var ref = new ActionReference();
   ref.putIdentifier(charIDToTypeID('Lyr '), ID);
   var desc = new ActionDescriptor();
   desc.putReference(charIDToTypeID('null'), ref);
   if (add) {
      desc.putEnumerated(stringIDToTypeID('selectionModifier'), stringIDToTypeID('selectionModifierType'), stringIDToTypeID('addToSelection'));
   }
   desc.putBoolean(charIDToTypeID('MkVs'), false);
   executeAction(charIDToTypeID('slct'), desc, DialogModes.NO);
}
function getLayerSetLayers(ID,arr){
   var ref = new ActionReference();
   ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
   var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1;
   var Names=[];
try{
    activeDocument.backgroundLayer;
var i = 0; }catch(e){ var i = 1; };
   for(i;i<count;i++){
       if(i == 0) continue;
        ref = new ActionReference();
        ref.putIndex( charIDToTypeID( 'Lyr ' ), i );
        var desc = executeActionGet(ref);
        var layerName = desc.getString(charIDToTypeID( 'Nm  ' ));
        var Id = desc.getInteger(stringIDToTypeID( 'layerID' ));
        if(Number(ID) == Number(Id) ) continue;
        var Colour =  typeIDToStringID(desc.getEnumerationValue(stringIDToTypeID('color')));
        if(layerName.match(/^<\/Layer group/) ) continue;
        var layerType = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'layerSection' )));
        var isLayerSet =( layerType == 'layerSectionContent') ? false:true;
        if(Colour.toString() == "red") Names.push([[Id],[layerName],[isLayerSet]]);
   };
//Return array of layers from the selected layer set
return Names;
};
function getLayerColourByID( ID ) {
    var ref = new ActionReference();
    ref.putIdentifier(charIDToTypeID('Lyr '), ID);
    return typeIDToStringID(executeActionGet(ref).getEnumerationValue(stringIDToTypeID('color')));
};
function putLayerColourByID(ID,Colour) {
// "none","red","orange","yellowColor","grain","blue","violet","gray"
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putIdentifier(charIDToTypeID('Lyr '), Number(ID));
    desc.putReference( charIDToTypeID('null'), ref );
        var desc2 = new ActionDescriptor();
        desc2.putEnumerated( charIDToTypeID('Clr '), charIDToTypeID('Clr '), stringIDToTypeID(Colour) );
    desc.putObject( charIDToTypeID('T   '), charIDToTypeID('Lyr '), desc2 );
    executeAction( charIDToTypeID('setd'), desc, DialogModes.NO );
};
function getNamesPlusIDs(){
   var ref = new ActionReference();
   ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
   var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1;
   var Names=[];
try{
    activeDocument.backgroundLayer;
var i = 0; }catch(e){ var i = 1; };
   for(i;i<count;i++){
       if(i == 0) continue;
        ref = new ActionReference();
        ref.putIndex( charIDToTypeID( 'Lyr ' ), i );
        var desc = executeActionGet(ref);
        var layerName = desc.getString(charIDToTypeID( 'Nm  ' ));
        var Id = desc.getInteger(stringIDToTypeID( 'layerID' ));
        var Colour = typeIDToStringID(desc.getEnumerationValue(stringIDToTypeID('color')));
        if(layerName.match(/^<\/Layer group/) ) continue;
        var layerType = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'layerSection' )));
        var isLayerSet =( layerType == 'layerSectionContent') ? false:true;
Names.push([[Id],[layerName],[isLayerSet],[Colour]]);
   };
return Names;
};


Paul MR

One way of getting layers from a layerset with AM

Postby Paul MR » Mon Nov 05, 2012 6:29 pm

Hi Mike, I have made a couple of changes and it now appears to be working for me, this is a superfast way of getting layers from layersets!

Code: Select allfunction getActiveLayerIndex() {
    var ref = new ActionReference();
    ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
    ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
    return executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ));
};
function getActiveLayerSection() {
    var ref = new ActionReference();
    ref.putProperty( charIDToTypeID("Prpr") , stringIDToTypeID("layerSection"));
    ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
    return typeIDToStringID(executeActionGet(ref).getEnumerationValue(stringIDToTypeID("layerSection")));
};
function getLayerLayerSectionByIndex( index ) {   
   var ref = new ActionReference();
   ref.putProperty( charIDToTypeID("Prpr") , stringIDToTypeID("layerSection"));
   ref.putIndex(charIDToTypeID("Lyr "), index);
   return typeIDToStringID(executeActionGet(ref).getEnumerationValue(stringIDToTypeID("layerSection")));
};
function getLayerNameByIndex( index ) {
    var ref = new ActionReference();
    ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Nm  " ));
    ref.putIndex( charIDToTypeID( "Lyr " ), index );
    return executeActionGet(ref).getString(charIDToTypeID( "Nm  " ));
};
function skipNestedSets( layerIndex ){
   var isEnd = false;
   layerIndex = app.activeDocument.layers[app.activeDocument.layers.length-1].isBackgroundLayer ? layerIndex-2:layerIndex;
   while(!isEnd){
      layerIndex--;
      if( getLayerLayerSectionByIndex( layerIndex ) == 'layerSectionStart' ) layerIndex = skipNestedSets( layerIndex );
      isEnd = getLayerNameByIndex(layerIndex) == "</Layer group>" ? true:false;
   }
   return layerIndex-1; //changed from 2 to 1
};
function getChildIndex( skipNested ){
   var layerSetIndex = getActiveLayerIndex();
   layerSetIndex = app.activeDocument.layers[app.activeDocument.layers.length-1].isBackgroundLayer ? layerSetIndex-1:layerSetIndex;
   var isEndOfSet = false;
   var layerIndexArray = [];
   while(!isEndOfSet){
      layerSetIndex--;
      if( getLayerLayerSectionByIndex( layerSetIndex ) == 'layerSectionStart' && skipNested ){
         layerSetIndex = skipNestedSets( layerSetIndex );
      }
  if(getLayerLayerSectionByIndex( layerSetIndex ) == undefined) break;
      isEndOfSet = getLayerNameByIndex(layerSetIndex) == "</Layer group>" ? true:false;
     if(!isEndOfSet ) layerIndexArray.push( layerSetIndex ); //"</Layer group>" now excluded
   }
   return layerIndexArray;
};
var idx = getChildIndex(false);// boolean true to skip nested layerSets
for(var a  in idx){
    alert(getLayerNameByIndex( Number(idx[a])));
    }


Mike Hale

One way of getting layers from a layerset with AM

Postby Mike Hale » Tue Nov 06, 2012 2:08 am

Paul MR wrote:... this is a superfast way of getting layers from layersets!

I thought it would be, which is why I have been working on it. I have been really busy the last few weeks. I did download your sample and saw where I think the problem was. I hadn't tested for a layerSet at the bottom of another layerSet( two layerSectionEnd in a row ). Thanks for fixing that.


Return to “Photoshop Scripting: Code Snippets”

Who is online

Users browsing this forum: No registered users and 20 guests