Skip to main content
John_Kordas
Inspiring
October 22, 2009
Answered

JS CS3 Fastest way to create a group

  • October 22, 2009
  • 2 replies
  • 771 views

I have a selection of about 1050 elements and to create a group I'm using:


var myNewGroup = new Array;

for (i=app.selection.length-1; i>=0; i--) {
    myNewGroup.push (app.selection);
         }
  var myGroup = app.activeDocument.groups.add(myNewGroup);

This works but it's very slow. I have tried the following:


var myNewGroup = app.selection;

var myGroup = app.activeDocument.groups.add(myNewGroup);

The app.selection approach gives me an Invalid parameter error. What I don't understand is that the working code creates an array and adds each element to the array then a group is created from that array.

With the second approach app.selection is an array so why does this group?

This topic has been closed for replies.
Correct answer RorohikoKris-u5pUJw

I had a quick check - your initial attempt with app.selection seems to work for me - no .slice(0) needed.

But one thing to be aware of: don't run the same script twice in quick succession. What happens is this:

a) you run the script -> all is well; selection becomes a group

b) you run the script again -> invalid parameter (because now your selection only has a single element in it - the group)

Could this be the issue you see? Re-select and make sure your selection is not already grouped before trying.

I'd change it so it becomes a variation of:

var myNewGroup = app.selection;

if (myNewGroup.length > 1)

{

  var myGroup = app.activeDocument.groups.add(myNewGroup);

}

2 replies

RorohikoKris-u5pUJwCorrect answer
Inspiring
October 22, 2009

I had a quick check - your initial attempt with app.selection seems to work for me - no .slice(0) needed.

But one thing to be aware of: don't run the same script twice in quick succession. What happens is this:

a) you run the script -> all is well; selection becomes a group

b) you run the script again -> invalid parameter (because now your selection only has a single element in it - the group)

Could this be the issue you see? Re-select and make sure your selection is not already grouped before trying.

I'd change it so it becomes a variation of:

var myNewGroup = app.selection;

if (myNewGroup.length > 1)

{

  var myGroup = app.activeDocument.groups.add(myNewGroup);

}

John_Kordas
Inspiring
October 22, 2009

Thanks again Kris,

The document I'm trying this on is supplied not created by me so I should of run a test on a doc a created first, so I did. I made a new A4 doc and place a number of text elements on the page then selected them and ran the script again. Sure enough the selection became a group.

So I went back to the supplied doc and selected a couple of the frame and check under the Object menu , Content and to my surprise the options Graphic, Text, and Unassigned were all grayed out (could not select any).

Yet when I ran:

$.write(app.selection[1].constructor.name+"\n");

the console returns TextFrame and for

$.write(app.selection[1].contentType+"\n");

the console returns 1952412773 (TEXT_TYPE).


So there seems to be an issue with the file not the script. Thanks for you help Kris much appreciated.

Inspiring
October 22, 2009

Hmm... Try

var myNewGroup = app.selection.slice(0);

var myGroup = app.activeDocument.groups.add(myNewGroup);

AFAIK app.selection is a 'pseudo-array' - it tastes and smells like an array, but it ain't one. The .slice(0) trick is something I tend to use to force these to convert to a 'real' array.

Let us know whether it works!

Cheers,

Kris

John_Kordas
Inspiring
October 22, 2009

Thanks Kris for the quick reply. Unfortunately I'm still getting an Invalid parameter on the

var myGroup = app.activeDocument.groups.add(myNewGroup);

Any other suggestions?