Skip to main content
wckdtall
Inspiring
November 10, 2014
Question

How do you recursively loop through sublayers and groups?

  • November 10, 2014
  • 1 reply
  • 891 views

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.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)}

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

        }

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

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

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

        }

}

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

        curLay = actDoc.layers;

        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.duplicate(app.activeDocument.layers.getByName("Live Editorial "), ElementPlacement.PLACEATEND);

}

But it only grabbed the same Text 5 times.

This topic has been closed for replies.

1 reply

Silly-V
Legend
November 10, 2014

First, all of your variables should have the var statement when they are first declared, including the loops (var i=0 ...)  this will allow you to avoid the world of hurt later, as any variable which doesn't get declared with var becomes a property of the global object.

Second, is your objective to just grab all text frames to move to a specific layer?

wckdtall
wckdtallAuthor
Inspiring
November 10, 2014

Good point. Yes the goal is to grab all text and duplicate to a specific layer, but ignoring the destination layer so it doesn't loop endlessly.

pixxxelschubser
Community Expert
Community Expert
November 10, 2014

Hi iamwickedtall1,

if I understand you right – does this works for you?

var aDoc = app.activeDocument;

var tF = aDoc.textFrames;

var newLay = aDoc.layers.add();

newLay.move(aDoc, ElementPlacement.PLACEATEND);

var tFL = tF.length;

for (i = tFL-1; i >= 0; i--) {

    tF.duplicate(newLay, ElementPlacement.PLACEATBEGINNING);

    }

newLay.move(aDoc, ElementPlacement.PLACEATBEGINNING);

With this method you don't need a loop through your layers.

Otherwise please post an example document.

Have fun