Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Can someone share script that reorders objects in a layer, alphabetically from Z-A.

Explorer ,
Apr 14, 2022 Apr 14, 2022

I do have some figures with names that needs to be ordered alphabetically in the layer (random name) from Z-A (sometimes can be 500+ objects), it can be torture to do it manualy.

Alphabetical order 1.JPGexpand image

 

Endgoal Example:

I select all the objects in the layer, run the scripts and every selected object (or all objects in the layer) gets alphabetically ordered from Z-A.

Alphabetical order 2.JPGexpand image

 

TOPICS
Scripting
209
Translate
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

Guide , Apr 15, 2022 Apr 15, 2022
  • I presume you're aware that when rearranging the layer panel, you're not just rearranging the names of the items, but rearranging the Z-order of the items. 
  • This is a bubble algorithm, so it may be slow if there are hundreds of items. 

 

var doc = app.activeDocument;
for (var i = 0; i < doc.layers.length; i++) {
    if (!doc.layers[i].locked && doc.layers[i].visible) {
        rearrange(doc.layers[i].pathItems);
    }
}
function rearrange(items){
  for (var i = items.length - 1; i > -1; i--){
     
...
Translate
Adobe
Guide ,
Apr 15, 2022 Apr 15, 2022
  • I presume you're aware that when rearranging the layer panel, you're not just rearranging the names of the items, but rearranging the Z-order of the items. 
  • This is a bubble algorithm, so it may be slow if there are hundreds of items. 

 

var doc = app.activeDocument;
for (var i = 0; i < doc.layers.length; i++) {
    if (!doc.layers[i].locked && doc.layers[i].visible) {
        rearrange(doc.layers[i].pathItems);
    }
}
function rearrange(items){
  for (var i = items.length - 1; i > -1; i--){
      for (var j = 1; j <= i; j++){
          if (alphanumCase(items[j - 1].name, items[j].name) < 0){
              items[j].moveBefore(items[j - 1]);
          }
      }
  }
}
// http://www.davekoelle.com/files/alphanum.js
function alphanumCase(a, b) {
    function chunkify(t) {
        var tz = new Array();
        var x = 0, y = -1, n = 0, i, j;
        while (i = (j = t.charAt(x++)).charCodeAt(0)) {
            var m = (i == 46 || (i >=48 && i <= 57));
            if (m !== n) {
                tz[++y] = "";
                n = m;
            }
            tz[y] += j;
        }
        return tz;
    }
    var aa = chunkify(a.toLowerCase());
    var bb = chunkify(b.toLowerCase());
    for (x = 0; aa[x] && bb[x]; x++) {
        if (aa[x] !== bb[x]) {
            var c = Number(aa[x]), d = Number(bb[x]);
            if (c == aa[x] && d == bb[x]) {
                return c - d;
            } else return (aa[x] > bb[x]) ? 1 : -1;
        }
    }
    return aa.length - bb.length;
}

 

Translate
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
Explorer ,
Apr 15, 2022 Apr 15, 2022
LATEST

Exactly what I needed.

I am aware of the rearange you mentioned.

 

Thank you! 🙂

Translate
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