Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Group existing objects using Scripting API ?

Community Beginner ,
May 04, 2009 May 04, 2009

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.

TOPICS
Scripting
7.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , May 04, 2009 May 04, 2009

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 )

Translate
Adobe
Advocate ,
May 04, 2009 May 04, 2009

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;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 04, 2009 May 04, 2009

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 ?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
May 04, 2009 May 04, 2009

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 );

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 04, 2009 May 04, 2009

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
May 04, 2009 May 04, 2009

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 )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 04, 2009 May 04, 2009

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 24, 2017 Aug 24, 2017

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 24, 2017 Aug 24, 2017

this ordinary task is easier to do these days...starting with CS6.

        app.executeMenuCommand('selectall');

        app.executeMenuCommand('group');

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 24, 2017 Aug 24, 2017
LATEST

Handy solution! Then it's easy to access that group with the last index in groupItems array.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 24, 2017 Aug 24, 2017

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines