Skip to main content
Participating Frequently
February 5, 2021
Question

How to loop through all texframes of a selected artboard?

  • February 5, 2021
  • 5 replies
  • 1065 views

Hi,  this seems pretty straight forward but just couldn't get it to work. I just want to loop through all textFrames in a selected artboard but my javascript code is returning all textFrames in the whole activeDocument. Would someone be able to shed some light?

 

var doc = app.activeDocument;

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

     doc.artboards.setActiveArtboardIndex(i);  //selected an artboard
     doc.selection = null; //clear selection
     doc.selectObjectsOnActiveArtboard();

 

    //loop thru' all textFrames in a selected artboard

    for(var j = 0; j < doc.textFrames.length; j++){

        text = doc.textFrames[j].contents;   // <== but this line is returning all textFrames in the whole doc

        alert(text);

   }

}

 

 

    This topic has been closed for replies.

    5 replies

    Community Expert
    February 8, 2021

    The issue is that the missing frames are a part of a group. We can add the functionality to traverse the groups and pick the textframes from it. Try the following code

    var doc = app.activeDocument;
    for (i=0; i<app.activeDocument.artboards.length; i++) {
    	doc.artboards.setActiveArtboardIndex(i);  //selected an artboard
    	doc.selection = null; //clear selection
    	doc.selectObjectsOnActiveArtboard();
    	for(var j = 0; j < app.selection.length; j++){
    		if(app.selection[j].typename == "TextFrame")
    			alert(app.selection[j].contents)
    		if(app.selection[j].typename == "GroupItem")
    			getTextInGroup(app.selection[j])
    	}
    }
    
    function getTextInGroup(inGrp)
    {
    	for(var i = 0; i < inGrp.textFrames.length; i++)
    		alert(inGrp.textFrames[i].contents)
    		
    	for(var j = 0; j < inGrp.groupItems.length; j++)
    		getTextInGroup(inGrp.groupItems[j])
    }
    

    -Manan

    -Manan
    Participating Frequently
    February 8, 2021

    Hi, I'm trying to return the value from the function but it's returning undefined values as well.  How to tell the function not to return undefined values? Tried this but didn't work - OMG, I'm getting deeper than I anticipated. Pls advise, thanks,

    function getTextInGroup(inGrp){
      for(var i = 0; i < inGrp.textFrames.length; i++)
          //alert(inGrp.textFrames[i].contents);  // alert skips the undefined values - good
    
          if (typeof(inGrp.textFrames[i].contents) != undefined){
             return(inGrp.textFrames[i].contents);  // this line is returning all undefined values
          }
    
      for(var j = 0; j < inGrp.groupItems.length; j++)
         getTextInGroup(inGrp.groupItems[j]);
    }

     

    Participating Frequently
    February 9, 2021

    Hi, I think I got it to work - checking the value after it returned from the function like that: -

    var doc = app.activeDocument;
    for (i=0; i<app.activeDocument.artboards.length; i++) {
    	doc.artboards.setActiveArtboardIndex(i);  //selected an artboard
    	doc.selection = null; //clear selection
    	doc.selectObjectsOnActiveArtboard();
    	for(var j = 0; j < app.selection.length; j++){
    		if(app.selection[j].typename == "TextFrame"){
    			alert(app.selection[j].contents);
    		}
    		if(app.selection[j].typename == "GroupItem"){
    			contxts = getTextInGroup(app.selection[j])
    			if (contxts !== undefined){  // test for undefined here
    			   //put my code here
    			}
    		}
    	}
    }
    
    function getTextInGroup(inGrp){
    	for(var i = 0; i < inGrp.textFrames.length; i++){
                return(inGrp.textFrames[i].contents);
             }
    		
    	for(var j = 0; j < inGrp.groupItems.length; j++){
    		getTextInGroup(inGrp.groupItems[j]);
             }
    }

    Thank you Manan for all your help.

    Community Expert
    February 7, 2021

    I don't think you attached the file. If you are having an issue with attaching the file, you can upload it to a cloud service like Dropbox and then share the public access link here.

    -Manan

    -Manan
    Participating Frequently
    February 7, 2021

    Strange, not sure why it won't allow me to attach the file (test.ai) even though the size is only about 1.2MB,

    Here's the link to dropbox: test.ai

    Thanks,

    Community Expert
    February 6, 2021

    Try the following

    var doc = app.activeDocument;
    for (i=0; i<app.activeDocument.artboards.length; i++) {
    	doc.artboards.setActiveArtboardIndex(i);  //selected an artboard
    	doc.selection = null; //clear selection
    	doc.selectObjectsOnActiveArtboard();
    	for(var j = 0; j < app.selection.length; j++){
    		if(app.selection[j].typename == "TextFrame")
    		text = app.selection[j].contents;   // <== but this line is returning all textFrames in the whole doc
    		alert(text);
    	}
    }

    The issue is that you need to iterate the selection and not the document textframes.

    -Manan

    -Manan
    Participating Frequently
    February 6, 2021

    Awesome @Manan Joshi - your code works but the results are missing 2 items.

     

    My document has a total of 7 artboards - for debugging, I deleted all artboards except the 2nd artboard, then when I ran the code below on the 2nd artboard I got two extra textFrames items. Was it because the extra 2 items wasn't of typename == "TextFrame"? If that's the case, how do I figure out what typenames are they? Thanks @Manan Joshi 

     

    //loop thru' all textFrames in activeDocument

    for(var j = 0; j < doc.textFrames.length; j++){

         text = doc.textFrames[j].contents;

         alert(text);

    }

     

    Community Expert
    February 6, 2021

    To identify the typename just alert it to. Something like the following should help

    var doc = app.activeDocument;
    for (i=0; i<app.activeDocument.artboards.length; i++) {
    	doc.artboards.setActiveArtboardIndex(i);  //selected an artboard
    	doc.selection = null; //clear selection
    	doc.selectObjectsOnActiveArtboard();
    	for(var j = 0; j < app.selection.length; j++){
    		alert(app.selection[j].typename);
    	}
    }

    If you still are not sure what is happening then you can share your sample document and we can have a look at it.

    -Manan

    -Manan
    Peru Bob
    Community Expert
    Community Expert
    February 5, 2021

    This is the Using the Community forum (which is the forum for issues using the forums).

    Please tell us what Adobe application you are using so that this can be moved to the proper forum for help.

    Participating Frequently
    February 6, 2021

    Oppss ... my bad, thought I posted on Adobe Illustrator 2021 forum.

    Participating Frequently
    February 5, 2021

    I did try this just in case you wonder but got error message saying "Error 21: undefined is not an object".

     

    var sel = doc.selection[0];

     

    //loop thru' all textFrames in a selected artboard

    for(var j = 0; j < sel.textFrames.length; j++){   // <== but this line crashed "Error 21: undefined is not an object"

         text = sel.textFrames[j].contents;

         alert(text);

    }

     

    Moving to Illustrator based on all other messages being about that program

    https://community.adobe.com/t5/user/viewprofilepage/user-id/17180511

    To find a forum for your program please start at https://community.adobe.com/