2017-09-01 76 views
0
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 

它是腳本。 我不理解程序結果。Photoshop腳本錯誤(層數)

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

這可能嗎?爲什麼?

+0

你修改'Doc.layers',它變得更短,而你遍歷它(我假設)。 – deceze

回答

0

首先,您每次腳本運行時都要添加一個新組。您的腳本不會對組中的圖層進行計數。您必須具有遞歸功能才能找到所有圖層(包括組和子組)。使用羣組工作是一種痛苦:(

我修改代碼以允許遞歸函數:

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; 
} 
相關問題