running script on each selected layer

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

bbarringer

running script on each selected layer

Post by bbarringer »

Hello all,

I have been browsing this forum for a few weeks now and have started to write my first script which has been successful until I try to run the script on multiple selected layers. I cannot for the life of me figure out why it is not working. I suspect it is a scoping issue of some sort but I don't know why. Here is a simple example of what is going on.

Code: Select all#target photoshop

var doc = app.activeDocument;
var layer = doc.activeLayer;

var sLayers = getSelectedLayersIdx();


function getSelectedLayersIdx(){
      var selectedLayers = new Array;
      var ref = new ActionReference();
      ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
      var desc = executeActionGet(ref);
      if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
         desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
          var c = desc.count
          var selectedLayers = new Array();
          for(var i=0;i<c;i++){
            try{
               activeDocument.backgroundLayer;
               selectedLayers.push(  desc.getReference( i ).getIndex() );
            }catch(e){
               selectedLayers.push(  desc.getReference( i ).getIndex()+1 );
            }
          }
       }else{
         var ref = new ActionReference();
         ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
         ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
         try{
            activeDocument.backgroundLayer;
            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);
         }catch(e){
            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));
         }
     var vis = app.activeDocument.activeLayer.visible;
        if(vis == true) app.activeDocument.activeLayer.visible = false;
        var desc9 = new ActionDescriptor();
    var list9 = new ActionList();
    var ref9 = new ActionReference();
    ref9.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    list9.putReference( ref9 );
    desc9.putList( charIDToTypeID('null'), list9 );
    executeAction( charIDToTypeID('Shw '), desc9, DialogModes.NO );
    if(app.activeDocument.activeLayer.visible == false) selectedLayers.shift();
        app.activeDocument.activeLayer.visible = vis;
      }
      return selectedLayers;
};




function writeEachLayersCount() {
   
    //make each layer the selected layer one at a time and write the count
    for (var i =0; i < sLayers.length; i++) {

            layer = doc.layers;
            createCount ();
           
   
        }
   
}




function createCount() {
   
    //create a new layer
    var newLayer = doc.artLayers.add();
   
    //make the new layer a textItem
    newLayer.kind = LayerKind.TEXT;
   
    //fill the new text layer with the character count
    newLayer.textItem.contents = layer.textItem.contents.length;
   
}



writeEachLayersCount ();

I have 3 text layers selected in my document labeled by what the output should be (1, 02, 003)
but when I run the script I get 3 new text layers all with the content of "3", which says to me that the active layer isn't changing.
Can you help me?

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

Mike Hale

running script on each selected layer

Post by Mike Hale »

From just a quick look whenever you add or delete layers it can change the index of other layer. That applies to both Action Manger and DOM. You can try using the id of the layer instead of the index if you want to stay with action manager or you could make DOM layer object references of the selected layers before adding new layers.
bbarringer

running script on each selected layer

Post by bbarringer »

Thanks Mike,
I will give that a try.