0

I have this code and it works, but it does not see the text inside the layer group and does not iterate over it.

var itemsAry=['emil','lardtub','100','jsbus', 'rasmus','null','1000000'];

var layers = app.activeDocument.layers;  
for (var i =0; i<layers.length; i++) {  

    if (layers[i].kind == "LayerKind.TEXT") {
        
        layers[i].textItem.contents=itemsAry[i]
    }
}
4
  • Can you give more inputs on app.activeDoument.layers?
    – Akhil M
    May 30, 2021 at 14:18
  • this is a call to layers and this is a call to group with layers app.activeDocument.layerSets
    – tvlss
    May 31, 2021 at 7:49
  • 1
    Have you tried to search the stackoverflow? There's at least a dozen of posts with the full layerstack iteration scripts both with DOM and AM approaches May 31, 2021 at 9:52
  • @SergeyKritskiy it's true, it's there. But if you don't know what to look for - it's not that obvious. :)
    – Ghoul Fool
    Jun 1, 2021 at 19:54

1 Answer 1

0

Whilst looping through all layers in a document is on SO, it can be confusing from looping over a number of layers to the awkwardness of looping over a psd with groups in it.

Basically, you need a recursive function to loop over the groups.

Point of note, make sure to switch off any dialog boxes at the start of the script when changing text. Otherwise you have to validate every text change confirmation.

// Switch off any dialog boxes
displayDialogs = DialogModes.NO; // OFF

var itemsAry=['emil','lardtub','100','jsbus', 'rasmus','null','1000000'];


// group layer vegetables
var theLayers = collectAllLayers(app.activeDocument, 0);


// 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.kind == "LayerKind.TEXT") 
            {
            
                theLayer.textItem.contents=itemsAry[m];
            }
      }
      else
      {
         collectAllLayers(theLayer, level + 1)
      }
   }
}

// Make sure dialogs back on
displayDialogs = DialogModes.ALL; // ON

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.