Skip to main content
Inspiring
November 9, 2024
Answered

How to ungroup all groups in a document?

  • November 9, 2024
  • 2 replies
  • 915 views

Why this script doesn't end? The idea is to ungroup all the groups in the doc. 

    var doc = app.activeDocument;
    var exit;

    while (true) {
        exit = true;

        app.executeMenuCommand("selectall");
        var sel = doc.selection;

        for (var i = 0; i < sel.length; i++) {
            var item = sel[i];
            if (item.typename === "GroupItem") {
                app.executeMenuCommand("ungroup");
                alert('ungrouped!');
                exit = false;
                break;
            }
        }

        while (doc.selection.length > 0) {
            app.executeMenuCommand('deselectall');
            app.redraw();
        }

        if (exit) break;
    }

    alert('exit while loop');
This topic has been closed for replies.
Correct answer andyf65867865

The reason is that in Illustrator, clipping masks are also a group.

2 replies

renél80416020
Inspiring
November 9, 2024

Désolé @andyf65867865  mais votre script n'est pas correct, il comporte plusieurs fautes ou maladresses.

(entre autres, la boucle for peut être parcourue de nombreuses fois...)

 

var compound = true;
 app.executeMenuCommand("selectall");
 app.executeMenuCommand("ungroup");
     if (compound) {
      app.executeMenuCommand("noCompoundPath");
     }
   selection = null;

 

René

Inspiring
November 10, 2024

@renél80416020Thanks for the comments, indeed one operator turns out to be unnecessary, forcing the for loop to run unnecessary times in the case of multiple outer groups. However, the compound path type is a separate type and it was not necessary to “parse” it. Also, note that the line `selection = null` should be followed with redraw otherwise it will not work correct. 

andyf65867865AuthorCorrect answer
Inspiring
November 9, 2024

The reason is that in Illustrator, clipping masks are also a group.