• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How do you recursively loop through sublayers and groups?

Enthusiast ,
Nov 10, 2014 Nov 10, 2014

Copy link to clipboard

Copied

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.

TOPICS
Scripting

Views

575

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Valorous Hero ,
Nov 10, 2014 Nov 10, 2014

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 10, 2014 Nov 10, 2014

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 10, 2014 Nov 10, 2014

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines