4 Replies Latest reply on Oct 21, 2009 8:56 PM by John.Kordas

    JS CS3 Fastest way to create a group

    John.Kordas Level 1

      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[i]);
               }
        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?

        • 1. Re: JS CS3 Fastest way to create a group
          RorohikoKris-u5pUJw Level 2

          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

          1 person found this helpful
          • 2. Re: JS CS3 Fastest way to create a group
            John.Kordas Level 1

            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?

            • 3. Re: JS CS3 Fastest way to create a group
              RorohikoKris-u5pUJw Level 2

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

              }

              • 4. Re: JS CS3 Fastest way to create a group
                John.Kordas Level 1

                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.