Cycle through layers, comparing current layer object against every other layer object

Explorer ,
Jun 09, 2022 Jun 09, 2022

Copy link to clipboard

Copied

I want to loop through each layer in my document (which has only 1 object on each layer) to compare the selected item's width and height against all other layers' object's width and height. I have started with comparing the first layer[0] against the rest of the layers following - see script below. But how do I then loop it again, but start at the second layer [1], loop through to the end of the layers and also come back to include layer[0] in that loop. Then loop through again, starting at the third layer [2], loop through to the end of the layers and then include layer[0] and [layer1] in that loop, and so on till I've checked all layers.

 

 

var docRef=app.activeDocument;

// Select the first layer and the layer contents
docRef.activeLayer = app.activeDocument.layers[0]; // unhide the first layer
docRef.activeLayer.hasSelectedArtwork = true; // selects artwork in active layer

// Show all layers in the document
showAllLayers();

var objWidth = app.selection[0].width;
var objHeight = app.selection[0].height;
var total = 0;
var items = docRef.pageItems;
var n = items.length;

for (i=0; i<n; i++) {
    var item = items[i];

    // Compare width and height
    if (item.width == objWidth
    && item.height == objHeight )
    {
        item.selected = true;
         // For keeping track of which layers have same sized object
        if (item.selected == true) {
        total += ([i] + ", ");
        }
      }
}

alert("Layers with same sized object: " + total.toString());

// FUNCTIONS

function forEach(collection, fn)
{
    var n = collection.length;
    for(var i=0; i<n; ++i)
    {
        fn(collection[i]);
    }
}

function showAllLayers()
{
    forEach(docRef.layers, function(layer) {
        layer.visible = true;
    }); 
}

 

TOPICS
Scripting

Views

88

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Jun 09, 2022 Jun 09, 2022

Copy link to clipboard

Copied

Loop within a loop. You build an extra loop around your existing one that determines the start offset and counts how many times the nested function has run.

 

Mylenium

Likes

Translate

Translate

Report

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 ,
Jun 10, 2022 Jun 10, 2022

Copy link to clipboard

Copied

I'll try to work it out ...

Likes

Translate

Translate

Report

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
Advisor ,
Jun 10, 2022 Jun 10, 2022

Copy link to clipboard

Copied

While you'll need a nested loop, my immediate impression is that you should be looping through items rather than layers.  Can you provide further information on

  1. The hierarchy of layers and items.  Are there sublayers?  What are the types of items (path items, text frames, etc)?  Are there nested items (groups and compound paths)? 
  2. What is the end goal?  Do you want to know which items are same sized or which layers have items which are same sized? 

Likes

Translate

Translate

Report

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 ,
Jun 10, 2022 Jun 10, 2022

Copy link to clipboard

Copied

LATEST

Not sure why you would want to "also come back to include layer[0] in that loop" since "Layer[0]" was already compared against the rest in the first iteration. But if that's what you need some like this would work.

var doc = app.activeDocument;
var layers = doc.layers;

for (var i = 0; i < layers.length; i++) {
  for (var n = 0; n < layers.length; n++) {
    if (i != n) {
      // do you comparison here
      alert("Comparing Layer " + i + " to Layer " + n);
    }
  }
}

If you had 3 layers the comparisons would go as follows...

Layer 0 vs Layer 1

Layer 0 vs Layer 2

---

Layer 1 vs Layer 0 (already compared above)

Layer 1 vs Layer 2

---

Layer 2 vs Layer 0 (already compared above)

Layer 2 vs Layer 1 (already compared above)

 

If you want to save work and only compare layers once you can use the slightly modified version below.

var doc = app.activeDocument;
var layers = doc.layers;

for (var i = 0; i < layers.length; i++) {
  for (var n = i + 1; n < layers.length; n++) {
    alert("Comparing Layer " + i + " to Layer " + n);
  }
}

Now with 3 layers, the comparisons would go...

Layer 0 vs Layer 1

Layer 0 vs Layer 2

---

Layer 1 vs Layer 2

 

*This is much less work especially if you have lots of layers.

 

Hopefully, this helps. Cheers!

Likes

Translate

Translate

Report

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