@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:
app.select(null); //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,
Uwe