Group all/selected items on page
Moving, scaling, rotating group of objects is always easier. InDesign CS5 is much better with editing grouped objects. Even with scripting, if you want to rotate 4 selected frames, you have to go through loop and rotate each of them, but not if objects are grouped! Let’s see how to group selected or all objects on current page or layer.
Grouping selected objects
First we create variable from selected objects. You can see that selection property is without square brackets. That is because we are passing all objects that are selected. Last line of code is just for selecting new created group. Notice that in parentheses we have negative value of item. It means that we are going to select last created group. Be aware that if you select objects that are not on same layer, group will be created on topmost selected layer.
var myObj = new Array; myObj = app.selection; app.activeWindow.activePage.groups.add(myObj); app.activeWindow.activePage.groups.item(-1).select();
If you want to clear selection, you can do it like this:
app.select(NothingEnum.NOTHING);
Grouping all objects on page
var myObj = new Array; myObj = app.activeWindow.activePage.pageItems; app.activeWindow.activePage.groups.add(myObj);
This is good if you don’t have layers, because if you do, this will group all objects from all layers even if layer IS locked! So be careful with this!
Grouping all objects on same layer
This is little bit tricky
Maybe there is another way, but for now I can’t find it!
Let’s start. We need two arrays, first for all items on current layer on all pages, and second where we will push items that are on current page. So, myLayerObj grabs all objects on current layer in entire document, myPage is just current page. Then we loop all objects and try to find all of them on current page. Found objects are pushed into myObj and the rest of process is same as in first example where we grouped selected objects.
var myLayerObj = new Array;
var myObj = new Array;
myLayerObj = app.activeDocument.activeLayer.allPageItems;
myPage = app.activeWindow.activePage.name;
for(var i = 0; i < myLayerObj.length; i++){
if(myLayerObj[i].parent.name == myPage)myObj.push(myLayerObj[i]);
}
app.activeWindow.activePage.groups.add(myObj);
app.activeWindow.activePage.groups.item(-1).select();
For InDesign CS5 you need to change line 6 like this:
if(myLayerObj[i].parentPage.name == myPage)myObj.push(myLayerObj[i]);
All this is tested and working on InDesign CS4 and CS5.
IndiSnip on Twitter!
IndiSnip on Facebook!
Contact IndiSnip by e-mail!
