Skip to main content
Inspiring
December 4, 2024
Answered

extend script: loop issue

  • December 4, 2024
  • 1 reply
  • 451 views

Hello, everyone,


I use the following method to loop the list of objects

 

var glen = app.activeDocumen.layers[0].groupItems;
for (var i = glen.length - 1; i >= 0; i--) {
}

 

but I want to loop the objects from left to right on artboard, instead of base on layer, Is it achievable? thanks

 

This topic has been closed for replies.
Correct answer m1b

Hi @rui huang, there are two extra steps: (a) put groups into an Array, (b) sort by left bounds. See this script below.

- Mark

 

 

 

(function () {

    var doc = app.activeDocument;

    // note: this gives us a GroupItems object, not an Array
    var groupsCollection = doc.groupItems;

    // you can't sort a GroupItems collection
    // so we'll populate an Array with the group items
    var groups = [];
    for (var i = 0; i < groupsCollection.length; i++)
        groups[i] = groupsCollection[i];

    // sort array by left bounds
    groups.sort(sortByLeft);

    // loop over groups, backwards
    for (var i = groups.length - 1; i >= 0; i--) {
        $.writeln('groups[i].left = ' + groups[i].left);
    }

})();

/**
* Sort by left bounds.
* @param {PageItem} a - item to sort.
* @param {PageItem} b - item to sort.
* @returns {Number} - the sort code.
*/
function sortByLeft(a, b) {

    return (

        // left bounds value rounded to 3 decimal places
        Math.round((a.visibleBounds[0] - b.visibleBounds[0]) * 1000) / 1000

        // same left, so we'll sort by top bounds value
        || a.visibleBounds[1] - b.visibleBounds[1]

    );

};

 

 

 

Note: that this will sort in ascending order of left position. But you are looping backwards, so you will start with the right-most first if you do it this way.

1 reply

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
December 4, 2024

Hi @rui huang, there are two extra steps: (a) put groups into an Array, (b) sort by left bounds. See this script below.

- Mark

 

 

 

(function () {

    var doc = app.activeDocument;

    // note: this gives us a GroupItems object, not an Array
    var groupsCollection = doc.groupItems;

    // you can't sort a GroupItems collection
    // so we'll populate an Array with the group items
    var groups = [];
    for (var i = 0; i < groupsCollection.length; i++)
        groups[i] = groupsCollection[i];

    // sort array by left bounds
    groups.sort(sortByLeft);

    // loop over groups, backwards
    for (var i = groups.length - 1; i >= 0; i--) {
        $.writeln('groups[i].left = ' + groups[i].left);
    }

})();

/**
* Sort by left bounds.
* @param {PageItem} a - item to sort.
* @param {PageItem} b - item to sort.
* @returns {Number} - the sort code.
*/
function sortByLeft(a, b) {

    return (

        // left bounds value rounded to 3 decimal places
        Math.round((a.visibleBounds[0] - b.visibleBounds[0]) * 1000) / 1000

        // same left, so we'll sort by top bounds value
        || a.visibleBounds[1] - b.visibleBounds[1]

    );

};

 

 

 

Note: that this will sort in ascending order of left position. But you are looping backwards, so you will start with the right-most first if you do it this way.

rui huangAuthor
Inspiring
December 4, 2024

Thank you very much for your help

m1b
Community Expert
Community Expert
December 4, 2024

You're welcome!