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

extend script: loop issue

Participant ,
Dec 03, 2024 Dec 03, 2024

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

 

paths.pngexpand image

TOPICS
Scripting
227
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

Community Expert , Dec 03, 2024 Dec 03, 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] = groupsCollectio
...
Translate
Adobe
Community Expert ,
Dec 03, 2024 Dec 03, 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.

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
Participant ,
Dec 03, 2024 Dec 03, 2024

Thank you very much for your help

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 ,
Dec 03, 2024 Dec 03, 2024
LATEST

You're welcome!

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