Skip to main content
Inspiring
Dec 4, 2024 5:54 AM UTC
Answered

extend script: loop issue

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 is closed to new replies. Start a new post to keep the conversation going.
m1b
m1bCorrect answer
Community Expert
Dec 4, 2024 6:23 AM UTC

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.

3 replies

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
Dec 4, 2024 6:23 AM UTC

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.

Translate
rui huangAuthor
Inspiring
Dec 4, 2024 7:14 AM UTC

Thank you very much for your help

Translate
m1b
Community Expert
Community Expert
Dec 4, 2024 7:17 AM UTC

You're welcome!

Translate