Highlighted

Organise object layer order based on object X-position

Explorer ,
Jun 13, 2020

Copy link to clipboard

Copied

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)

 

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);

 

TOPICS
Scripting

Views

413

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

Organise object layer order based on object X-position

Explorer ,
Jun 13, 2020

Copy link to clipboard

Copied

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)

 

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);

 

TOPICS
Scripting

Views

414

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
Jun 13, 2020 0
Adobe Community Professional ,
Jun 13, 2020

Copy link to clipboard

Copied

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

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
Reply
Loading...
Jun 13, 2020 0
Engaged ,
Jun 13, 2020

Copy link to clipboard

Copied

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);

 

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
Reply
Loading...
Jun 13, 2020 0
Explorer ,
Jun 13, 2020

Copy link to clipboard

Copied

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.

 

 

 

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
Reply
Loading...
Jun 13, 2020 0
Engaged ,
Jun 13, 2020

Copy link to clipboard

Copied

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);

 

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
Reply
Loading...
Jun 13, 2020 0
Explorer ,
Jun 13, 2020

Copy link to clipboard

Copied

Thank you! Does the job nicely.

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
Reply
Loading...
Jun 13, 2020 0
Explorer ,
Jun 13, 2020

Copy link to clipboard

Copied

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

 

Cheers!

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
Reply
Loading...
Jun 13, 2020 0
Adobe Community Professional ,
Jun 14, 2020

Copy link to clipboard

Copied

Which script gives an undefined error?

The one shared by me?

Best regards

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
Reply
Loading...
Jun 14, 2020 0
Explorer ,
Jun 14, 2020

Copy link to clipboard

Copied

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.

 

 

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
Reply
Loading...
Jun 14, 2020 1