Skip to main content
Inspiring
April 2, 2019
Answered

How to keep the object being selected after ungroup

  • April 2, 2019
  • 3 replies
  • 991 views

Hi experts,

my script like this:

for(var i = 0; i < app.selection.length; i++) 

    { 

            if(app.selection.constructor.name == "Group")  {

            app.selection.ungroup();

        }

    }

my aim: keep the object being selected after ungroup.

could you someone show me how to.

thanks

regard

John

This topic has been closed for replies.
Correct answer Manan Joshi

You would need to make a collection of elements of the group before you ungroup and then after ungrouping select the elements in the collection.

for(var i = 0; i < app.selection.length; i++)  

{  

    if(app.selection.constructor.name == "Group")

    {

          var a = app.selection.allPageItems

          app.selection.ungroup()

          app.select(a)

    }

}

-Manan

3 replies

JohnwhiteAuthor
Inspiring
April 8, 2019

thank you Uwe, thanks so much.

John

JohnwhiteAuthor
Inspiring
April 8, 2019

Hi Uwe,

How to find the itemID?

something like 118845

John

Community Expert
April 8, 2019

Hi John,

what did you try to find the id numbers?

Did you perhaps do something like that? :

var ma = app.scriptMenuActions.everyItem().getElements();

for( var n=0 ; n<ma.length; n++ )

{

    $.writeln( n +"\t"+ ma.id +"\t"+ ma.area  +"\t"+ ma.title )

};

You will not get very far with that.

Tested on my German InDesign CC 2019 when no document was open I will see the result below.

Important note: The id numbers are negative.

0    -16769024    Skripte    Separator

1    -16769023    Skripte    Separator

2    -16769022    Skripte    Open Book Documents

3    -16769020    Schrift-Menü    URLs &in Hyperlinks konvertieren...

4    -16769018    Schrift-Menü    Fußnote und Endnote konvertieren...

5    -16769006    Skripte    Close Book Documents

The result is rather strange. Not only the length of the result.

Instead I did a loop with app.scriptMenuActions.itemByID( n )

Where I tested for n = 0 up to n = 200000.

Not every value of n is a valid id number.

Do that in chunks, otherwise you may not get any result at all if you like to store the results in an array.

And don't forget to test negative ID numbers as well.

Regards,

Uwe

Manan JoshiCommunity ExpertCorrect answer
Community Expert
April 2, 2019

You would need to make a collection of elements of the group before you ungroup and then after ungrouping select the elements in the collection.

for(var i = 0; i < app.selection.length; i++)  

{  

    if(app.selection.constructor.name == "Group")

    {

          var a = app.selection.allPageItems

          app.selection.ungroup()

          app.select(a)

    }

}

-Manan

JohnwhiteAuthor
Inspiring
April 2, 2019

thank you Manan

thank so much.

John

Community Expert
April 5, 2019

Hi John,

Alternative:

Use the menu action for ungroup if available.

Some code I found in my repository from 2012. Still working with InDesign CC 2019:

app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;

app.doScript(_UngroupAllGroupsOfSelection, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT, "Ungroup all groups of selection, if possible.");

function _UngroupAllGroupsOfSelection(){

if(app.selection.length===0){alert("You selected nothing. Select something you want to ungroup!"); return; };

//Check how many page items are in the selection:

var counter = 0;

for(var n=0;n<app.selection.length;n++){

    counter = counter + app.selection.allPageItems.length;

    };

//"counter" is the max number of "ungroup" actions we need:

for(var n=0;n<counter;n++){

    //Invoke menu action: "Ungroup" until the menu action is not available any more:

    try{app.scriptMenuActions.itemByID(118845).invoke()}catch(e){break};

    };

}; //END function "_UngroupAllGroupsOfSelection()"

The ungrouped elements are automatically selected.

Regards,
Uwe