groups - ff6347/extendscript GitHub Wiki
##Create Groups
This shows how to add a group.
// create adoc with some settings
var doc = app.documents.add({
documentPreferences: {
pageHeight: 200,
pageWidth: 200
}
});
var page = doc.pages[0]; // get the page
// add a rectangle
var rect = page.rectangles.add({
geometricBounds: [50, 0, 150, 100],
fillColor: doc.swatches[3]
});
// add aoval
var oval = page.ovals.add({
geometricBounds: [50, 100, 150, 200],
fillColor: doc.swatches[3]
});
var arr = [rect, oval]; // add them to an array
var group = page.groups.add(arr); // create a group of them
##Find Groups Here you can see how to find groups in a document.
This was written for this stackoverflow question.
// This script needs:
// - A document with one page.
// - Some groups with textframes in them on the first page.
var pg = app.activeDocument.pages[0]; // Get the first page of the current doc.
var groups = pg.groups; // Get all groups.
var tf_ingroup_counter = 0; // Init a counter for all textframes in groups.
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++; // Increment counter.
} // End loop textfraems in group.
}
} // End loop groups.
// Alert all you have found:
alert(
"I found on page " + pg.name +"\n" + pg.textFrames.length +
" text frames\nOh and there are also " +
tf_ingroup_counter+ " hidden in groups."
);
Jongware also had an answer to this question that turned out to be even better.
allframes = app.activeDocument.allPageItems;
textframes = [];
for (i=0; i<allframes.length; i++)
{
if (allframes[i] instanceof TextFrame)
textframes.push(allframes[i]);
}
alert (textframes.length);