Getting a list of all layer names without looping them

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

d3mac123

Getting a list of all layer names without looping them

Post by d3mac123 »

I have a script that reads all layer names and put them in a listbox:
Code: Select all    for (var i=0;i<=docRef.layers.length-1;i++) {
            var item = w.export.renderAs.laylist.add('item', rename(docRef.layers.name).substr(0,14)); 
            if (docRef.layers.name.substr(0,14) == toren.substr(0,14)) {
                pos = i;
                //break;
            }
    }
w.export.renderAs.laylist.selection = w.export.renderAs.laylist.items[pos];


Everything works fine when the document has a few layers. In a document with 116 layers, it is taking 2.15minutes to execute the script.

I was wondering if there is such command that return an array with all layer names (the same way layer.length returns the number of layers), like ["abc","test","Layer 2"].

Any ideas on how to improve my code?
Thanks a lot!

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Paul MR

Getting a list of all layer names without looping them

Post by Paul MR »

It would speed it up if you used action manager code, this example should get over 100 layers in less than a second...
It will return the layerID, layer name and if it is a layerset

Code: Select allmain();
function main(){
if(!documents.length) return;
var w = new Window('dialog','test');
w.dd1 = w.add('dropdownlist');
w.bu1 = w.add('button',undefined,'Cancel');
var layerNames = getNamesPlusIDs();
for(var z in layerNames){w.dd1.add('item',layerNames[z][1].toString());}
w.dd1.selection=0;
w.show();
}

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' ));
        if(layerName.match(/^<\/Layer group/) ) continue;
        var layerType = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'layerSection' )));
        var isLayerSet =( layerType == 'layerSectionContent') ? false:true;
Names.push([[Id],[layerName],[isLayerSet]]);
   };
return Names;
};
d3mac123

Getting a list of all layer names without looping them

Post by d3mac123 »

Paul, this is FREAKING AWESOME!

I am just curious where to learn more about action manager code. I am a pretty skilled Javascript developer (my plugin for Photoshop, including some action manager code (like yours) gotten here that I cannot read) has more than 25,000 lines of code but I couldn't find yet a way to speed up some code with action manager because there is no manual or user guide for that. Any recommendations?

Thanks again! Really appreciated!
Alex
Paul MR

Getting a list of all layer names without looping them

Post by Paul MR »

There is a fair amount of info to be found on this site if you can find it
Apart from here and http://forums.adobe.com/community/photo ... iscussions ... iscussions
there isn't much information.

I think Mike wrote this bit of code so that you can examine the various keys from ExtendScript Toolkit...

Code: Select allvar ref = new ActionReference();
//'capp' , "Dcmn", "Lyr ","Chnl", "Actn","Path"
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
var c = desc.count

if(desc.typename == 'ActionReference'){
    var c = desc.count;
    for(var i=0;i<c;i++){ //enumerate reference. use getForm() to determine which get method to use
      $.writeln('AR '+zeroPad( i+1, 2 )+' = '+desc.getReference(i).getIndex());
    }
}

if(desc.typename == 'ActionList'){
    var c = desc.count;
    $.writeln(desc.count);
    for(var i=0;i<c;i++){ //enumerate list
     $.writeln('AL '+zeroPad( i+1, 2 )+' = '+desc.getType(i)/* added desc.getPath(i) for aliastype */ +' - ' + typeIDToStringID(desc.getClass (i)));
    }
}
if(desc.typename == 'ActionDescriptor'){
    var c = desc.count;
    for(var i=0;i<c;i++){ //enumerate descriptor's keys
      $.writeln('AD '+zeroPad( i+1, 2 )+' = '+IDTz(desc.getKey(i)) +' : '+desc.getType(desc.getKey(i)));
    }
}
function IDTz(id){
     try {
          var res = typeIDToStringID( id );
          if(res == '' ){
               var res = typeIDToCharID( id );
          }
     }catch(e){}
     return res;
}
function zTID( s ){
     if( s.length == 4 ) var res = charIDToTypeID( s );
     if( s.length > 4 ) var res = stringIDToTypeID( s );
     return res;
}

function zeroPad(num,pad) {
     var z = Math.pow(10,Number(pad))
     return num <= z ? ((Number( num) + z).toString().substr(1)): num
}



You can allways ask here as well.
d3mac123

Getting a list of all layer names without looping them

Post by d3mac123 »

I know I can count on you, Mike and others here in the forums. I am just trying to give you some relief

I will check the link and the code you sent. thanks again!

Alex