Copy link to clipboard
Copied
The layer named "edit clipart" has a mix of "groups" and "layers". Is there any way to know the sequence number of a particular item in this state?
1 Correct answer
(function () {
var lay = app.activeDocument.layers['edit clipart'];
var tgtIdx = 2; // 3rd group in layer
var arr = [];
var i = 0;
for (i = 0; i < lay.pageItems.length; i++) {
arr.push(lay.pageItems[i]);
}
for (i = 0; i < lay.layers.length; i++) {
arr.push(lay.layers[i]);
}
arr.sort(function (a, b) {
return a.absoluteZOrderPosition - b.absoluteZOrderPosition;
}).reverse();
var tmpIdx = 0;
for (i = 0; i < arr.length; i++) {
if (tmpIdx == tgtIdx) {
...
Explore related tutorials & articles
Copy link to clipboard
Copied
Could you expound on the question? As I presently understand it, the question is how to find the index of a specified item in a collection (which begs the question of why want the index if the item is specified).
Copy link to clipboard
Copied
Yes. I want to file the "Group" and "Layer" on a specific layer (edit clipart) as SVG files, one by one, and load them in the web browser in this order.
If you don't know this order, it won't display properly in the web browser.
Copy link to clipboard
Copied
there are ways of finding out
how are you getting a reference of such group? (to later try to get its position)
Copy link to clipboard
Copied
I still fear we are talking at cross purposes, but here goes. Iterate the collection, comparing the specified (e.g. selected) item with each item in the collection. When they are the same item, you have your index:
var groups = app.activeDocument.layers["edit clipart"].groupItems;
for (var i = 0; i < groups.length; i++) {
if (app.selection[0] == groups[i]) {
alert(i);
}
}
Copy link to clipboard
Copied
The code you gave me only gets the groupItem from the layer called "edit clipart" so I know the order.
What I'm trying to figure out is how to get the sequence number from a mixture of groups and layers.
I want to automatically convert the items in the layer called "edit clipart" to svg one by one and display them on the web, and I need that sequence number.
If this is not provided by the illustrator script, I will have to go through another complicated process.
Thanks for the answer.
Copy link to clipboard
Copied
it should be doable but it seems we're not understanding each other
can you try to explain what you need more explicitly?
Copy link to clipboard
Copied
Inside the "edit clipart" layer, there are a total of 20 groups and layers. Is there any way to find out what sequence number the 3rd group item is in the "edit clipart" layer?
Copy link to clipboard
Copied
(function () {
var lay = app.activeDocument.layers['edit clipart'];
var tgtIdx = 2; // 3rd group in layer
var arr = [];
var i = 0;
for (i = 0; i < lay.pageItems.length; i++) {
arr.push(lay.pageItems[i]);
}
for (i = 0; i < lay.layers.length; i++) {
arr.push(lay.layers[i]);
}
arr.sort(function (a, b) {
return a.absoluteZOrderPosition - b.absoluteZOrderPosition;
}).reverse();
var tmpIdx = 0;
for (i = 0; i < arr.length; i++) {
if (tmpIdx == tgtIdx) {
tmpIdx = i;
break;
}
if (arr[i].typename === 'GroupItem') tmpIdx++;
}
alert('Target group index is ' + tmpIdx);
})();
If you are counting from 1 instead of 0, you should add +1 to the final number.
Copy link to clipboard
Copied
This is what I was looking for!
This is exactly what I was looking for.
Thank you so much for your answer.
Copy link to clipboard
Copied
You could try pushing the top level groups and sublayers into arrays, concatenating the arrays, then reverse sorting the concatenated array by absoluteZOrderPosition. The sorted array should be a combined collection of groups and sublayers in the order shown in the Layers panel. absoluteZOrderPosition does not work in CS6, so I cannot try it.
Copy link to clipboard
Copied
Yes, thanks for the answer.
"absoluteZOrderPosition" was one of the ways I could still find the order.
absoluteZOrderPosition This is the order of all items, not within a specific layer.
But it looks like I need to work on something like your answer to use this.

