Copy link to clipboard
Copied
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);
}
}
Copy link to clipboard
Copied
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/
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Oppss ... my bad, thought I posted on Adobe Illustrator 2021 forum.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Attached is the test.ai file - it has only one artboard (the 2nd artboard), the problem artboard. Using this method: -
alert(app.activeDocument.textFrames.length); // got 15 items
but when using this method: -
for(var j = 0; j < app.selection.length; j++){
if(app.selection[j].typename == "TextFrame"){
k++;
text = app.selection[j].contents;
alert(k); //got 13 items
}
}
Couldn't figure out why the 1st method yields 15 items whereare the 2nd method yeilds only 13 items ?? Can you shed some light on this? Thank you.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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,
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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]);
}
Copy link to clipboard
Copied
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.