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

Organise object layer order based on object X-position

Explorer ,
Jun 13, 2020 Jun 13, 2020

I'm looking for a way to select a large number of objects and automatically sort them in the layer palette so the left-most object is at the top of the layer order, the next is arranged under it and so on... (until the right-most is at the bottom).

 

I've seen some very old scripts around but none seem to work.

 

...Can't seem to attach a file. It's basically dozens of vertical lines (separate paths created with a blend, made into a compound path, then cut out out into a shape using pathfinder. This last part breaks the order of the objects)

 

TOPICS
Scripting
1.8K
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 2 Correct answers

Community Expert , Jun 13, 2020 Jun 13, 2020

Hi,

Try following snippet

 

function test() {
    if (!app.documents.length) {
        alert("You must have a document open.");
        return;
    }
    var docRef = app.activeDocument;
    var sel = docRef.selection;
    if (!sel.length) {
        alert("You must make a selection.");
        return;
    }

    function sortHorizonally(items) {
        var topMost, curX, delIndex, curItem, tempItems = [];
        for (var x = 0, len = items.length; x < len; x++) {
            tempItems.push(ite
...
Translate
Guide , Jun 13, 2020 Jun 13, 2020

Sorry.  I didn't test it for selected items.  You are right, it doesn't work for selected items (I'm not sure why off the top of my head).  However, it should work for all paths in the document:

function sortLtoR(items){
  for (var i = (items.length - 1); i >= 0; i--){
      for (var j = 1; j <= i; j++){
          if (items[j-1].left < items[j].left){
              items[j].moveBefore(items[j-1]);
          }
      }
  }
}
sortLtoR(app.activeDocument.pathItems);

 

Translate
Adobe
Community Expert ,
Jun 13, 2020 Jun 13, 2020

Hi,

Try following snippet

 

function test() {
    if (!app.documents.length) {
        alert("You must have a document open.");
        return;
    }
    var docRef = app.activeDocument;
    var sel = docRef.selection;
    if (!sel.length) {
        alert("You must make a selection.");
        return;
    }

    function sortHorizonally(items) {
        var topMost, curX, delIndex, curItem, tempItems = [];
        for (var x = 0, len = items.length; x < len; x++) {
            tempItems.push(items[x]);
        }

        while (tempItems.length) {
            topMost = tempItems[0];
            curX = topMost.left
            delIndex = 0;
            for (var x = 1, len = tempItems.length; x < len; x++) {
                curItem = tempItems[x];
                if (curItem.left > curX) {
                    topMost = curItem;
                    curX = curItem.left;
                    delIndex = x;
                }
            }
            topMost.zOrder(ZOrderMethod.BRINGTOFRONT);
            tempItems.splice(delIndex, 1);
        }
    }
    sortHorizonally(sel);
}

test();

 

 

Main Script source : https://community.adobe.com/t5/illustrator/auto-arrange-per-y-value-position/td-p/10024991?page=1

Credit: @williamadowling 

 

But script at above link is not working as there is some error, while moving thread from old forum to new forum.

 

Let us know if above snippet works for you.

Best regards
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
Guide ,
Jun 13, 2020 Jun 13, 2020

Try this

 

// select items to sort
function sortLtoR(items){
    for (var i = (items.length - 1); i >= 0; i--){
        for (var j = 1; j <= i; j++){
            if (items[j-1].left < items[j].left){
                items[j].moveBefore(items[j-1]);
            }
        }
    }
}
sortLtoR(selection);

 

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 ,
Jun 13, 2020 Jun 13, 2020

Thanks this works!  Actually, sorry it doesn't. I looks like it only compares two at a time. It compares one to the previous and puts it in front of that one if it is futher left, then I think recalculates once per item in the selection? But doesn't order every selected object.

 

 

 

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
Guide ,
Jun 13, 2020 Jun 13, 2020

Sorry.  I didn't test it for selected items.  You are right, it doesn't work for selected items (I'm not sure why off the top of my head).  However, it should work for all paths in the document:

function sortLtoR(items){
  for (var i = (items.length - 1); i >= 0; i--){
      for (var j = 1; j <= i; j++){
          if (items[j-1].left < items[j].left){
              items[j].moveBefore(items[j-1]);
          }
      }
  }
}
sortLtoR(app.activeDocument.pathItems);

 

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 ,
Jun 13, 2020 Jun 13, 2020

Thank you! Does the job nicely.

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 ,
Jun 13, 2020 Jun 13, 2020

Thanks. I had tried this one previously and it threw an error. Line 29: undefined is not an object. 

 

Cheers!

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
Community Expert ,
Jun 14, 2020 Jun 14, 2020

Which script gives an undefined error?

The one shared by me?

Best regards
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 ,
Jun 14, 2020 Jun 14, 2020
LATEST

Yeah. Sorry I tried to reply to your post but for some reason it put it down here. 

 

I just ran it again and it did work.

 

My fault! I had a version of @williamadowling  's script that I'd tried changing from Y to X and that was throwing that error. Basically I tried to do what you did, but I'm terrible at scripting (and apparently terrible at keeping track of my scripts).

 

So your answer is also correct. 

Thanks very much.

 

 

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