Copy link to clipboard
Copied
Am I missing something, or is there no way to group existing objects using the scripting api ? You can add() to GroupItems but that doesn't help you with existing objects.
1 Correct answer
You probably don't have to duplicate the item, that's just how the examples were written.
Looking at the code, try changing this:
newItem = docSelection.duplicate();
newItem.moveToBeginning( newGroup )
to this:
docSelection.moveToBeginning( newGroup )
Explore related tutorials & articles
Copy link to clipboard
Copied
This is from the Javascript reference, this shows you how to add new paths to a new groupItem:
// Creates a new group item, adds a new path item, of triangle shape, to the
group, then
// adds a new text item to the group and sets the fill color of the text to
red
if ( app.documents.length > 0 ) {
var triangleGroup = app.activeDocument.groupItems.add();
// Create a triangle and add text, the new art is created inside the group
var trianglePath = triangleGroup.pathItems.add();
trianglePath.setEntirePath( Array( Array(100, 100), Array(300, 100),
Array(200, Math.tan(1.0471975) * 100 + 100) ) );
trianglePath.closed = true;
trianglePath.stroked = true;
trianglePath.filled = false;
trianglePath.strokeWidth = 3;
var captionText = triangleGroup.textFrames.add();
captionText.position = Array(100, 150);
captionText.textRange.size = 48;
captionText.contents = "A triangle";
var fillColor = new RGBColor;
fillColor.red = 255;
fillColor.green = 0;
fillColor.blue = 0;
captionText.characters.fillColor = fillColor;
}
Copy link to clipboard
Copied
This is what I need:
Create some objects and groups in various ways, for instance some of my groups come from placing an EPS file, some come from createOutlines on text.
I want to group those objects together.
Your method is only good for grouping certain items as you create them.
How could the scripting api authors miss such an obvious use case ?
Copy link to clipboard
Copied
Some more examples from the reference, hopefully what you need should be in here. These show you how to duplicate existing items and add them to a group:
// Duplicates and groups all items in the current selection,
// then applies the same brush to each item in the group
if ( app.documents.length > 0 ) {
docSelection = app.activeDocument.selection;
if ( docSelection.length > 0 ) {
newGroup = app.activeDocument.groupItems.add();
for ( i = 0; i < docSelection.length; i++ ) {
newItem = docSelection.duplicate();
newItem.moveToBeginning( newGroup );
}
brush4 = app.activeDocument.brushes[1];
brush4.applyTo( newGroup );
}
}
// Duplicates each path item in the selection, places the duplicate into a
// new group, then applies a graphic style to the new groups items
if ( app.documents.length > 0 ) {
var doc = app.activeDocument;
var selected = doc.selection;
var newGroup = doc.groupItems.add();
newGroup.name = "NewGroup";
newGroup.move( doc, ElementPlacement.PLACEATEND );
var endIndex = selected.length;
for ( i = 0; i < endIndex; i++ ) {
if ( selected.typename == "PathItem" )
selected.duplicate( newGroup, ElementPlacement.PLACEATEND );
}
for ( i = 0; i < newGroup.pageItems.length; i++ ) {
doc.graphicStyles[1].applyTo( newGroup.pageItems );
}
}
Copy link to clipboard
Copied
Ok, using GroupItem.duplicate() will probably work.I'll duplicate the groups and put them in a new group, then delete the originals. Still strange to me that there isn't just a GroupItems.add(Object) function.
Copy link to clipboard
Copied
You probably don't have to duplicate the item, that's just how the examples were written.
Looking at the code, try changing this:
newItem = docSelection.duplicate();
newItem.moveToBeginning( newGroup )
to this:
docSelection.moveToBeginning( newGroup )
Copy link to clipboard
Copied
Hmm... moveToBeginning seems to be undocumented. That is the only reference I find to it in the scripting guide. The other method (duplicate and remove) works as well, but if you group things across layers it will create a new layer for your group. I think it will work for me because it seems to keep the zorder when it creates the new layer.
Copy link to clipboard
Copied
After several hours of googling and trying (with ('PARAM') error codes). I've found the right way to group any objects.
// Make selection with all items you need to group. In my case I had to group all existing items on document, so I've selected all of them. You could use selections for certain layers as well. Actually you only need to find a way to select those items you need to group.
for ( i = 0; i < docRef.pageItems.length; i++ ) {
docRef.pageItems.selected = true;
}
var docSelection = new Array;
docSelection = app.activeDocument.selection;
// Create a new group.
var newGroup = app.activeDocument.groupItems.add();
// Item by item move selection to that new group.
if (docSelection.length > 0) {
for (i = 0; i < docSelection.length; i++) {
docSelection.moveToBeginning(newGroup);
}
}
Unfortunatelly the references in official Adobe scripting guides are not that detailed so some of us spent a lot of time for such an ordinary task.
Copy link to clipboard
Copied
this ordinary task is easier to do these days...starting with CS6.
app.executeMenuCommand('selectall');
app.executeMenuCommand('group');
Copy link to clipboard
Copied
Handy solution! Then it's easy to access that group with the last index in groupItems array.
Copy link to clipboard
Copied
By the way it is better to use moveToEnd() instead of moveToBegining() if you need to keep order of your layer objects. In my case I found out that moveToBeginning is actually invert location of selected items, those that were in the back appears in front after grouping.

