1 Reply Latest reply on Jun 19, 2013 2:43 PM by Laubender

    Group selected objects, then rename group?


      I'm having the hardest time figuring out how to perform the actions in the title of this discussion. I'd love to let the user select a bunch of objects on a page, and then have a script that:


      1. Groups the selection, and
      2. Renames the group (so that said name then appear in the Layers panel).


      Does anyone here have any experience or insight as to how I might do this? I've spent a good portion of the day misinterpreting the DOM - I won't even confuse the issue by pasting my code here!


      Thanks in advance...



        • 1. Re: Group selected objects, then rename group?
          Laubender Adobe Community Professional & MVP

          @Cliff – you did not post any code, so it is not possible to know where you misinterpreted the DOM or basic JavaScript (ExtendScript). That's not good…


          However, let's walk through a possible solution:


          Since there is no app.group() method we have to consider using the group.add() method to the document.

          We can feed that method with an array of our selected page items.*


          Since we do not want to make a group out of a selection with the length of one, we start the script with an if statement  asking the length of the selection. If the length is 0 or if the length is 1, the script will not run. We exit(). This also ignores any text selections, because if you selected any text, the length of the selection will be always 1. Also anchored objects are dismissed (you cannot select more than one anchored object at the same time).


          Another thing we have to consider:
          We have to decide, if a selected object should be grouped, if it's locked.

          If so, we could unlock all selected objects while assembling the objects for grouping or we could ignore it.


          The following snippet will group all selected objects. Even if they are locked (we have to unlock them):


          //Fill in the name of your group here:
          var nameOfGroup = "HereGoesTheName";
          //Some checks first.
          //In case you have selected nothing, stop the script:
          if(app.selection.length === 0){alert("You have selected NOTHING. Stop."); exit(0)};
          //In case you have selected one single object, be it text, a cell or a table or something else, stop the script:
          if(app.selection.length === 1){
              var selObjectConstrName = app.selection[0].constructor.name;
              if(selObjectConstrName === "Cell"){alert("You have selected a part of a \"Table\". Stop."); exit(0)};
              else{alert("You have selected a single "+"\""+selObjectConstrName+"\""+". Stop."); exit(0)};
          //If nothing of the above is true, we can go on:
          //Prepair a new empty array for the selected page items:
          var selectedItemsArray = new Array();
          //Iterate through all selected page items:
          for(var n=0;n<app.selection.length;n++){
              //Unlock the selected object:
              app.selection[n].locked = false;
              //Feed the array of page items:
              selectedItemsArray[n] = app.selection[n];
          //Add a group to the document; the method requires an array of page items:
          var myGroup = app.documents[0].groups.add(selectedItemsArray);
          //Give the group a name = the variable defined in line 2:
          myGroup.name = nameOfGroup;
          //Important: Deselect the group!
          //This prevents the user executing the script a second time on the now established group which is leading to an error:
          //You can remove this line, if you do not want a success message:
          alert(selectedItemsArray.length + " page items were grouped. Name of the group is: " +"\""+nameOfGroup+"\"");

          *there are alternatives: using a script menu action, but this might provoke other issues we have to deal with.


          Have fun,