Update GroupItems for app.activeDocument

The app.activeDocument.groupItems collection contains a list of all document groups. If the script duplicates an existing group or creates a new one using the groupItems.add() method, the contents of the GroupItems collection are not updated: the number remains the same as before you ran the script, you cannot access the new group by index. Layers have local GroupItems collections, they are updated immediately.

The group copy is not displayed in the Layers panel, which is also an old Illustrator bug

If you save the copied group to a variable, you can manipulate through the variable. We are only talking about the bug of the general collection GroupItems of the document.

The first way you can fix the bug of displaying a new group in Layers panel and it will forcibly update the GroupItems of the document. To do this, you need to switch the boolean visibility property of the layer with the object.

var dup = activeDocument.groupItems[0].duplicate();
dup.layer.visible = false;
dup.layer.visible = true;
alert(‘Doc groups: ‘ + activeDocument.groupItems.length);
activeDocument.groupItems[1].translate(50,0);
The panel and the group counter were updated. The script found the group by index and moved it

At the end, we moved the copy using the translate() method. The second way is to move some document object after changing GroupItems and immediately return it back.

var src = activeDocument.groupItems[0];
var grp = activeDocument.groupItems.add();
src.translate(50,0);
src.translate(-50,0);

In this example, we took the first group in the document and after adding a new group in the document, moved the original group to the right by 50px and right back. These moves leave no extra steps in the document history.

You can also add a blank path to the document after the group operation and delete it. This will also update the Layers panel and GroupItems content.

var grp = activeDocument.groupItems.add();
var tmp = activeDocument.pathItems.add();
tmp.remove();

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
AiScripts

Sergey Osokin. I’m a illustrator & Script Developer (Ai, Ps). Writing about bugs and tricks.