简体   繁体   中英

Photoshop script error (layer count)

var Doc = activeDocument;
var newLayerSet = Doc.layerSets.add();         
var count = 0;

alert(Doc.layers.length); //A
for(var i=0; i<Doc.layers.length ; i++){
    if(Doc.layers[i].name.indexOf ("MMRE") != -1){
        Doc.layers[i].move(newLayerSet, ElementPlacement.INSIDE);
    }
    count++;
}   
alert(count); //B

It is script. I don't understanding program result.

alert(Doc.layers.length); //A'  result is 73.
but 'alert(count); //B result is 45. 

Is it possible? WHY?

For a start, you're adding a new group each time the script is run. Your script doesn't count layers in groups. You have to have a recursive function to find all the layers (including groups and sub groups). Working with groups is a pain :(

I've modified your code to allow for a recursive function:

var Doc = activeDocument;
var newLayerSet = Doc.layerSets.add();         
var count = 0;

var allLayers = new Array();
var theLayers = collectAllLayers(app.activeDocument, 0);
count = theLayers;

alert("A: " + allLayers.length); //A
alert("B: " + count); //B


// function collect all layers
function collectAllLayers (theParent, level)
{
  for (var m = theParent.layers.length - 1; m >= 0; m--)
  {
    var theLayer = theParent.layers[m];

    // apply the function to layersets;
    if (theLayer.typename == "ArtLayer")
    {
        if(theLayer.name.indexOf ("MMRE") != -1)
      {   
          theLayer.move(newLayerSet, ElementPlacement.INSIDE);
      }
    }
    else
    {
      allLayers.push(level + theLayer.name);
      collectAllLayers(theLayer, level + 1)
      count++;
    }
  }
  return count;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM