5 Replies Latest reply on Nov 6, 2015 12:43 PM by Vinicius Baptista

    Create group

    CREAT-IT

      Hi,

       

      I want to create a group of all elements in dartboard and name it "Sticker".

       

      Also I want to rename "Layer 1" to "Preview", how can this be done?

       

      Thanks in advance.

       

      Greetings,
      Robin (an Illustrator scripts beginner)

        • 1. Re: Create group
          williamadowling Level 4

          do you have multiple artboards? I'll assume no, for now.

           

          I'll assume that you want to group everything in the document into one group.

           

          I tested this and it works for me. Let me know if you have more than 1 artboard and we can change this up a bit to work for all the artwork on each artboard.

           

           

          *EDIT* looks like OP disappeared forever. but for anyone else looking for this solution, this should work. i edited the wrapper function since groupItem is already an element. So just to save confusion, i updated the wrapper function name. cheers.

           

          function groupAllItems(){
          
          
          var docRef= app.activeDocument;
          var elements = docRef.pageItems;
          var newGroup = docRef.layers[0].groupItems.add();
          newGroup.name = "Sticker";
          for(i=1;i<elements.length;i++){
               var curElement = elements[i];
               curElement.moveToBeginning(newGroup);
          }
          
          
          docRef.layers.getByName("Layer 1").name = "Preview";
          
          
          }
          groupAllItems();
          
          
          • 2. Re: Create group
            rafals18850831

            This script works, but unfortunately releases all compound paths,

            so after running the script all can become a mess a little...

             

            I'm looking for a solution but have no idea how to resolve this problem.

            • 3. Re: Create group
              Vinicius Baptista Level 2

              Hello dear, in my tests this method release the "clipping masks" and not "compound path", I think be better you select all items on active artboard with native method "selectObjectsOnActiveArtboard();" before and after create a loop through all selected items to group them, see this sample:

               

              function groupAll(){
              
              
              
              
              app.activeDocument.selectObjectsOnActiveArtboard(); 
              
              
              
              
              var docRef= app.activeDocument; 
              var newGroup = docRef.layers[0].groupItems.add(); 
              
              
              
              
              for(i=0;i<docRef.selection.length;i++){ 
                  docRef.selection[i].moveToBeginning(newGroup);
              };
              
              
              
              
              docRef.layers.getByName("Layer 1").name = "Preview";
              
              
              
              
              };
              
              
              
              
              groupAll();
              

               

              Congratulations on the initiative in learning scripting, go ahead and have fun.

               

              Best Regards

              -Vinícius Baptista

              • 4. Re: Create group
                williamadowling Level 4

                You're definitely right, vinícius. I always forget that doc.pageItems actually looks at the contents of groupItems and compound paths, rather than treating a groupItem as a pageItem as it does in any other context... =(

                • 5. Re: Create group
                  Vinicius Baptista Level 2

                  Hi, williamadowling, Relax, sometimes i do the same mistake lol, everytime when i'm writing some script using pageItems property, I have a lot of care with it. So I hope we  have helped our friend.

                   

                  See ya!