Group page items having same name or number

Contributor ,
Feb 06, 2022 Feb 06, 2022

Copy link to clipboard

Copied

Hi,

 it is possible to loop selected layer page items (can be all typenames) and group the two, having same name or number ?

TOPICS
Scripting

Views

118

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Feb 06, 2022 Feb 06, 2022
Try this. (It will not work as expected if the items to be grouped are themselves group items.) function groupSameNames() { var counter = 0; var items = app.activeDocument.pageItems; var groups = app.activeDocument.groupItems; for (var i = items.length - 1; i > -1; i--) { if (counter > 0) { counter--; continue; } var group1 = app.activeDocument.groupItems.add(); group1.zOrder(ZOrderMethod.SENDTOBACK); group1.name = i...

Likes

Translate

Translate
Adobe Community Professional ,
Feb 06, 2022 Feb 06, 2022

Copy link to clipboard

Copied

for the beginning:

How to search / filter objects by name in Layers panel? 

 

(but use var pI = aDoc.activeLayer.pageItems; instead)

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Feb 06, 2022 Feb 06, 2022

Copy link to clipboard

Copied

Try this. (It will not work as expected if the items to be grouped are themselves group items.)

 

function groupSameNames() {
    var counter = 0;
    var items = app.activeDocument.pageItems;
    var groups = app.activeDocument.groupItems;
    for (var i = items.length - 1; i > -1; i--) {
        if (counter > 0) {
            counter--;
            continue;
        }
        var group1 = app.activeDocument.groupItems.add();
        group1.zOrder(ZOrderMethod.SENDTOBACK);
        group1.name = items[i].name;
        items[i].moveToEnd(group1);
        for (var j = i - 1; j > -1; j--) {
            if (group1.name == items[j].name) {
                items[j].moveToEnd(group1);
                counter++;
            }
        }
    }
    for (var i = groups.length - 1; i > -1; i--) {
        if (groups[i].pageItems.length == 1) {
            groups[i].selected = true;
            app.executeMenuCommand("ungroup");
            app.selection = null;
        }
    }
}
groupSameNames();
groupSameNames();  // 2nd call reverts order

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 07, 2022 Feb 07, 2022

Copy link to clipboard

Copied

Thank you @femkeblanco 

Your script fits exactly my needs!
Thank you!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Feb 07, 2022 Feb 07, 2022

Copy link to clipboard

Copied

LATEST

Bonjour à tous,

Femkeblanco, dans votre script si plusieurs calques, (erreur 1200)

Je propose une autre approche (sauf erreur de ma part)

 

// JavaScript Document,for Illustrator
// De elleere
// Groups objects with the same name. (layer or document)
// Mon, 7 February 2022 17:31:48 GMT
var layer = app.activeDocument.activeLayer;
    //groupSameNames(app.activeDocument);
    groupSameNames(layer);
// -------
function groupSameNames(relativeObj) {
  var nb, group, items = [];
      nb = relativeObj.pageItems.length;
        for(var j = 0; j < nb; j++) {
          items.push(relativeObj.pageItems[j]);
        }
        if (relativeObj.typename == "Document") {
          relativeObj.layers[0].visible = true;
          relativeObj.layers[0].locked = false;
          // Placement layers[0] or
          // relativeObj = relativeObj.activeLayer
        }
        while(items.length > 0) {
          var tabs = [];  // temporary table
          var name = items[0].name;
                for (var j = 0; j < items.length; j++) {
                  if (name == items[j].name) {
                    tabs.push(items[j])
                    items.splice(j,1);
                    j--;
                  }
                }
                if (tabs.length > 1 && name != "") {
                  group = relativeObj.groupItems.add();
                  group.zOrder(ZOrderMethod.SENDTOBACK);
                  group.name = name;
                    for (j = 0; j < tabs.length; j++) {
                      tabs[j].moveToEnd(group);
                    }
                }
    }
}
// -------

 

René

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines