Pregunta

Similar to my earlier problems with finding a textFrame on a page based on its geometricBounds or by part of its name, I now am running into the problem of finding textFrames if they are inside groups. If I use an array of all textFrames, such as:

var textFramesArray = document.textFrames.everyItem().getElements();

it will not find any textFrames that are inside of a group. How can I figure out how to reference a textFrame if it's inside a group? Even if the group has to be un-grouped, that's fine, but I cannot even figure out how to find groups on the page!

¿Fue útil?

Solución

Groups on a page are page.groups ... but you don't need this anyway. Fabian's answer is good, but it doesn't take groups-in-groups into account -- nor clipping masks, nor text frames inside tables and footnotes (etc.).

Here is an alternative approach: allPageItems is pretty much guaranteed to return all page items, of all kinds and persuasion, inside groups or other frames or whatnot. You can inspect, then process, each of them in turn, or build an array of text frames to work with at leisure:

allframes = app.activeDocument.allPageItems;
textframes = [];
for (i=0; i<allframes.length; i++)
{
    if (allframes[i] instanceof TextFrame)
        textframes.push(allframes[i]);
}
alert (textframes.length);

Otros consejos

Try this:

// this script needs:
// - a document with one page
// - some groups with textframes in it on the first page

var pg = app.activeDocument.pages[0];
var groups = pg.groups;

var tf_ingroup_counter = 0;
for(var g = 0; g < groups.length;g++){
    var grp = groups[g];

        for(var t = 0; t < grp.textFrames.length;t++){
            var tf = grp.textFrames[t];
                if(tf instanceof TextFrame){
                    tf_ingroup_counter++;
                    }             
        }
    }

alert("I found on page " + pg.name +"\n" + pg.textFrames.length 
            +" textframes\nOh and there are also "
            +tf_ingroup_counter+ " hidden in groups");

Your task to get all the text frames in the layer or the document. Whether those text frames are in a group or not. This is done through the property of allPageItems. For instance use this:-

var items=app.activeDocument.allPageItems;

this will give you all the text frames in the item and within group also. Now you can do any manupulations. You can check the items on the debug console which will give all the types of the objects.And then you can check for the textframe

items[i].constructor.name =='TextFrame'

and now you can store each object in type array.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top