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

Loop through a certain named layer and all it's groups and sub groups

Participant ,
Mar 17, 2021 Mar 17, 2021

Hi,

 

I'm trying to write a loop to search through a specific layer name 'Artwork' (including all it's groups and sub groups) to identify any text below 6pt and if so, increase the size of the affected font up to 6pt.

 

I have set up a file where I have x3 pieces of text as follows (see screenshot):

 

1. 'Layer 2' text is in 'Layer 2'.

2. 'Top Level of 'Artork' layer' text is in the top level of the 'Artwork' layer.

3. 'Group ‘Artwork_Ref_2’ of ‘Main_Group’ of ‘Artwork layer' text is within a sub group of a group on the 'Artwork' layer.

 

I have managed to write a loop that will either change any text below 6pt on the entire document or just on the very top level of the 'Artwork' layer (see code example for the top level of the 'Artwork' layer), but can not get it to loop through all the groups and sub groups on the desired 'Artwork' layer. What am I doing wrong?

var idoc = app.activeDocument.layers["Artwork"];//Top level of 'Artwork' layer
for (i=0; i<idoc.textFrames.length; i++) {
  if (idoc.textFrames[i].textRange.characterAttributes.size < 6) {
    idoc.textFrames[i].textRange.characterAttributes.size = 6;
      }
}

 

 

Thanks in advance.Screenshot 2021-03-17 at 14.10.22.png 

TOPICS
Scripting
1.9K
Translate
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

correct answers 2 Correct answers

Guide , Mar 17, 2021 Mar 17, 2021
var doc = app.activeDocument;
var targetLayer = doc.layers["Artwork"];
for (var i = 0; i < targetLayer.textFrames.length; i++) {
  if (targetLayer.textFrames[i].textRange.characterAttributes.size < 6) {
    targetLayer.textFrames[i].textRange.characterAttributes.size = 6;
  }
}
function accessSubitems(subitems) {
  for (var j = 0; j < subitems.length; j++) {
    if (subitems[j].typename == "TextFrame" && subitems[j].textRange.characterAttributes.size < 6) {
      subitems[j].textRange.characterA
...
Translate
Community Expert , Mar 17, 2021 Mar 17, 2021

Here you go,

var _artWorkLayer = app.activeDocument.layers["Artwork"];//Top level of 'Artwork' layer

function main(_pageItem) {
    for (var i = 0; i < _pageItem.length; i++) {
        if (_pageItem[i].typename == 'TextFrame') {
            changeSize(_pageItem[i]);
        } else if (_pageItem[i].typename == 'GroupItem') {
            main(_pageItem[i].pageItems);
        }
    }
}

function changeSize(item) {
    if (item.textRange.characterAttributes.size < 6) {
        item.textRange.charac
...
Translate
Adobe
Community Expert ,
Mar 17, 2021 Mar 17, 2021

Hi,

Try following snippet, it will change size of all textframes inside the layer 'Artwork'.

var idoc = app.activeDocument.layers["Artwork"];//Top level of 'Artwork' layer


function main(_pageItems) {
    for (i = 0; i < _pageItems.length; i++) {
        if (_pageItems[i].typename == 'TextFrame') {
            changeSize(_pageItems[i]);
        } else if (_pageItems[i].typename == 'GroupItem') {
            main(_pageItems[i].pageItems);
        }
    }
}

function changeSize(item) {
    if (item.textRange.characterAttributes.size < 6) {
        item.textRange.characterAttributes.size = 6;
    }
}

main(idoc.pageItems);
Best regards
Translate
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
Participant ,
Mar 17, 2021 Mar 17, 2021

Thanks for the quick reply. I have just tried this and it doesn't change the size on the bottom text that is in a sub group. If I move the text out of the sub group to the top level of the first group, it does resize it. How would I amend the snippet you have provide to allow it to work on all objects in all groups and all sub groups of the 'Artwork' layer?

Translate
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
Participant ,
Mar 17, 2021 Mar 17, 2021

…to add to the above, I also moved it to the first subgroup ('Artwork_Ref_1') which sits above the 'Artwork_Ref_2' that holds the text and it seems to work from there. Does the loop not go deeper than the first sub group in the snippet you provided?

Translate
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 ,
Mar 17, 2021 Mar 17, 2021

Could you please attach your ai file? It will be easy to debug all scenarios in one go.

Best regards
Translate
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 ,
Mar 17, 2021 Mar 17, 2021

Try following one

 

var idoc = app.activeDocument.layers["Artwork"];//Top level of 'Artwork' layer

function main(_pageItem) {
    if (_pageItem.typename == 'TextFrame') {
        changeSize(_pageItem);
    } else if (_pageItem.typename == 'GroupItem') {
        for (i = 0; i < _pageItem.pageItems.length; i++) {
            main(_pageItem.pageItems[i]);
        }
    }
}

function changeSize(item) {
    if (item.textRange.characterAttributes.size < 6) {
        item.textRange.characterAttributes.size = 6;
    }
}

for (var j = 0; j < idoc.pageItems.length; j++) {
    main(idoc.pageItems[j]);
}

 

Best regards
Translate
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
Participant ,
Mar 17, 2021 Mar 17, 2021

Hi, thanks for all your help on this so far.

 

The above code does not seem to work either. I have attached the ai file to help you debug. What I need it to do is search all text in the 'Artwork' layer and see if it is below 6pt and size up if needed, but do not want it to touch any other items on the other layer in the document. Hope this makes sense.

Translate
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
Guide ,
Mar 17, 2021 Mar 17, 2021
var doc = app.activeDocument;
var targetLayer = doc.layers["Artwork"];
for (var i = 0; i < targetLayer.textFrames.length; i++) {
  if (targetLayer.textFrames[i].textRange.characterAttributes.size < 6) {
    targetLayer.textFrames[i].textRange.characterAttributes.size = 6;
  }
}
function accessSubitems(subitems) {
  for (var j = 0; j < subitems.length; j++) {
    if (subitems[j].typename == "TextFrame" && subitems[j].textRange.characterAttributes.size < 6) {
      subitems[j].textRange.characterAttributes.size = 6;
    }
    if (subitems[j].pageItems) {
      accessSubitems(subitems[j].pageItems);
    }
  }
}
for (var k = 0; k < doc.pageItems.length; k++) {
  if (doc.pageItems[k].parent == targetLayer && doc.pageItems[k].pageItems) {
    accessSubitems(doc.pageItems[k].pageItems);
  }
}
Translate
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 ,
Mar 17, 2021 Mar 17, 2021

Here you go,

var _artWorkLayer = app.activeDocument.layers["Artwork"];//Top level of 'Artwork' layer

function main(_pageItem) {
    for (var i = 0; i < _pageItem.length; i++) {
        if (_pageItem[i].typename == 'TextFrame') {
            changeSize(_pageItem[i]);
        } else if (_pageItem[i].typename == 'GroupItem') {
            main(_pageItem[i].pageItems);
        }
    }
}

function changeSize(item) {
    if (item.textRange.characterAttributes.size < 6) {
        item.textRange.characterAttributes.size = 6;
    }
}

for (var j = 0; j < _artWorkLayer.pageItems.length; j++) {
    if (_artWorkLayer.pageItems[j].typename == 'TextFrame')
        changeSize(_artWorkLayer.pageItems[j])
    else if (_artWorkLayer.pageItems[j].typename == 'GroupItem')
        main(_artWorkLayer.pageItems[j].pageItems);
}
Best regards
Translate
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
Participant ,
Mar 18, 2021 Mar 18, 2021

Both these solutions work. Thank you both for your help and input on this.

Much appreciated.

Translate
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 ,
Mar 18, 2021 Mar 18, 2021
LATEST

Great!

Best regards
Translate
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