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

Is there a way to know the sequence number of a specific group within a layer?

Explorer ,
Aug 26, 2023 Aug 26, 2023

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?

 

2002854_1-1693038680942.pngexpand image

 

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

Enthusiast , Aug 27, 2023 Aug 27, 2023

 

 

(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) {
  
...
Translate
Adobe
Guide ,
Aug 26, 2023 Aug 26, 2023

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).

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
Explorer ,
Aug 26, 2023 Aug 26, 2023

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.

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 ,
Aug 26, 2023 Aug 26, 2023

there are ways of finding out

 

how are you getting a reference of such group? (to later try to get its position)

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
Guide ,
Aug 26, 2023 Aug 26, 2023

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);
  }
}

 

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
Explorer ,
Aug 26, 2023 Aug 26, 2023

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.

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 ,
Aug 26, 2023 Aug 26, 2023

it should be doable but it seems we're not understanding each other

 

can you try to explain what you need more explicitly?

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
Explorer ,
Aug 27, 2023 Aug 27, 2023

2002854_0-1693187751884.pngexpand image

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?

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
Enthusiast ,
Aug 27, 2023 Aug 27, 2023

 

 

(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);
})();

 

 

Screenshot(8).pngexpand image

If you are counting from 1 instead of 0, you should add +1 to the final number.

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
Explorer ,
Aug 28, 2023 Aug 28, 2023
LATEST

This is what I was looking for!
This is exactly what I was looking for.
Thank you so much for your answer.

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
Guide ,
Aug 27, 2023 Aug 27, 2023

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.

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
Explorer ,
Aug 27, 2023 Aug 27, 2023

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.

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