3 Replies Latest reply on Nov 10, 2014 2:48 PM by pixxxel schubser

    How do you recursively loop through sublayers and groups?

    iamwickedtall1 Level 1

      I've been fairly successful looping through an AI file, and was able to loop through layers and pull text Layers, to a specified layer. Problem being, that I also want to capture anything that's nested in groups, and depending on which way I put the try statements, it will only catch parts of the editorial in my file. The problem occurs within the traverseSubs function, I had both groupItems and layers in one loop, but it wasn't fully going through anything, so I set 2 different variables to loop through.

       

      actDoc = app.activeDocument;

      textLayerName = "Live Editorial"

      layLen = actDoc.layers.length;

      function catchText(grabTextFrom,whereTo) {

          try{textCatchLayer = actDoc.layers.getByName(whereTo)}

          catch(e){

              var textCatchLayer = actDoc.layers.add();

              textCatchLayer.name = textLayerName;

              textCatchLayer.printable = false;

              textCatchLayer.zOrder(ZOrderMethod.BRINGTOFRONT);

          }

          var tF = grabTextFrom.textFrames

          var tFL = tF.length;

          for ( i = 0; i < tFL; i++) {

             tF[i].duplicate(textCatchLayer, ElementPlacement.PLACEATEND);

          }

      }

       

      function traverseSubs(current) {

              catchText(current,textLayerName);

              for ( h = 0; h < current.groupItems.length; h++) {

                  try{if(current.groupItems.length > 0) traverseSubs(current.groupItems[h])}

                  catch(e) {$.write(e)}

              }

              for ( i = 0; i < current.layers.length; i++) {

                  try{ if(current.layers.length > 0) traverseSubs(current.layers[h])}

                  catch(e) {$.write(e)}

              }

      }

       

      for (j = 0; j < layLen; j++){

              curLay = actDoc.layers[j];

              if(curLay.name == textLayerName) continue;

              traverseSubs(curLay);

      }

       

      I also tried the below:

       

      tF = app.activeDocument.textFrames

      tFL = tF.length

        for(i=0;i<tFL;i++){

              tF[i].duplicate(app.activeDocument.layers.getByName("Live Editorial "), ElementPlacement.PLACEATEND);

      }

       

      But it only grabbed the same Text 5 times.