Copy link to clipboard
Copied
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
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...
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thank you very much for your help
Copy link to clipboard
Copied
You're welcome!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now