Skip to main content
Participating Frequently
March 18, 2013
Answered

Ungroup selected items and delete them

  • March 18, 2013
  • 1 reply
  • 1672 views

Hello!

I have a code that removes the tags (if there are tags) from objects and then deletes them.

The problem is if tagged objects are grouped. Or even grouped objects in another group.

I need to ungroup the selected objects and then untagging and deleting them.

How can i do this?

My current code:

for (var iSel = app.activeDocument.selection.length-1; iSel >=0 ; iSel--){

     var currentSelection = app.selection[iSel];

          if (currentSelection.associatedXMLElement != null) {

               currentSelection.associatedXMLElement.untag();

               currentSelection.remove();

          }

          else {

               currentSelection.remove();

          }

}

This topic has been closed for replies.
Correct answer Laubender

You could work with the property "allPageItems" for every selected object and untag thiese first.
Afterwards you could delete all selected items, if you really need to.

Also locked items, if you want.

Code:

var sel = app.selection;

//UNTAG ALL SELECTED ITEMS:

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

    var currentObject = sel;

    var allObjectsInCurrentObject = currentObject.allPageItems;

    if(currentObject.associatedXMLElement != null){

        currentObject.associatedXMLElement.untag();

        };

    for(var a=0;a<allObjectsInCurrentObject.length;a++){

        if(allObjectsInCurrentObject.associatedXMLElement != null){

            allObjectsInCurrentObject.associatedXMLElement.untag();

            };

        };

    };

//REMOVE ALL SELECTED ITEMS:

for(var n=sel.length-1;n>=0;n--){

    //LOCKED ITEMS WILL BE UNLOCKED:

    if(sel.locked){sel.locked = false};

    //THEN DELETED:

    sel.remove();

    };

Uwe

Participating Frequently
March 19, 2013

bump!

LaubenderCommunity ExpertCorrect answer
Community Expert
March 19, 2013

You could work with the property "allPageItems" for every selected object and untag thiese first.
Afterwards you could delete all selected items, if you really need to.

Also locked items, if you want.

Code:

var sel = app.selection;

//UNTAG ALL SELECTED ITEMS:

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

    var currentObject = sel;

    var allObjectsInCurrentObject = currentObject.allPageItems;

    if(currentObject.associatedXMLElement != null){

        currentObject.associatedXMLElement.untag();

        };

    for(var a=0;a<allObjectsInCurrentObject.length;a++){

        if(allObjectsInCurrentObject.associatedXMLElement != null){

            allObjectsInCurrentObject.associatedXMLElement.untag();

            };

        };

    };

//REMOVE ALL SELECTED ITEMS:

for(var n=sel.length-1;n>=0;n--){

    //LOCKED ITEMS WILL BE UNLOCKED:

    if(sel.locked){sel.locked = false};

    //THEN DELETED:

    sel.remove();

    };

Uwe

Community Expert
March 19, 2013

Really no need to un-group something here.

With "allPageItems" you also reach objects that are pasted inside other objects.

With one omission: page items that are sitting in not active states of MultiStateObjects (MSOs).

Uwe