Skip to main content
Participant
June 19, 2019
Answered

Arrange layer order by bounds area?

  • June 19, 2019
  • 2 replies
  • 863 views

Hi guys, as the title suggests, I'm trying to move the layer order by the their bounds area, this is where I got to right now, but getting an error. Not sure where I'm going wrong. Any suggestions & explanations is much appreciated!

var doc = app.activeDocument;

var layers = doc.artLayers;

function arrangeByArea()  {   

   

  for (var i = 0; i < doc.artLayers.length; i++)

  for (var y = 1; y < doc.artLayers.length -1; y++)

           var layerArea1 = (layers.bounds[2] - layers.bounds[0]) * (layers.bounds[3] - layers.bounds[1]);

           var layerArea2 = (layers.bounds[2] - layers.bounds[0]) * (layers.bounds[3] - layers.bounds[1]);

          if (layerArea1 >= layerArea2) {

               layers.move(layers, ElementPlacement.PLACEAFTER);

    

    }

  }

arrangeByArea();

This topic has been closed for replies.
Correct answer r-bin

var doc = app.activeDocument;

var layers = doc.artLayers;

function arrangeByArea()

    {

    for (var i = 0; i < layers.length-1; i++)

        {

        var layerArea1 = (layers.bounds[2] - layers.bounds[0]) * (layers.bounds[3] - layers.bounds[1]);

        var layerArea2 = (layers[i+1].bounds[2] - layers[i+1].bounds[0]) * (layers[i+1].bounds[3] - layers[i+1].bounds[1]);

        if (layerArea1 >= layerArea2)

            {

            layers.move(layers[i+1], ElementPlacement.PLACEAFTER);

            i = -1; // restart the cycle

            }                  

        }

    }

arrangeByArea();

alert("Done");

I will not explain. Sorry. I find it difficult in English.

UPD.

Need to change >= to >.
Otherwise, with the same area, the script will loop endlessly.

2 replies

Chuck Uebele
Community Expert
Community Expert
June 20, 2019

This is my version: a script that I've used to arrange layers from the smallest area to the largest. I put the area of each layer into an array, the sort the array, then move the layers.

#target photoshop

var docRef = activeDocument

var sizeArray = new Array()

for(var i=0;i<docRef.layers.length;i++){

    docRef.activeLayer = docRef.layers

    var area = (Number(docRef.activeLayer.bounds[2]) -Number(docRef.activeLayer.bounds[0]))*(Number(docRef.activeLayer.bounds[3]) -Number(docRef.activeLayer.bounds[1]))

    var area2 = area + 1000000000000

    sizeArray = area2+':'+docRef.activeLayer.name

  

    sizeArray.sort()

   

    }

var layLen = docRef.layers.length

for(var j=0;j<sizeArray.length;j++){

    var layName = sizeArray.split(':')[1];

   

    docRef.activeLayer = docRef.layers.getByName (layName)

    docRef.activeLayer.move(docRef.layers[layLen-1],ElementPlacement.PLACEAFTER)

    }

Legend
June 20, 2019

There will be problems if the layers have the same name.

Chuck Uebele
Community Expert
Community Expert
June 20, 2019

That's true, but since I wrote the script for my own use, I never have layers with the same name.

r-binCorrect answer
Legend
June 19, 2019

var doc = app.activeDocument;

var layers = doc.artLayers;

function arrangeByArea()

    {

    for (var i = 0; i < layers.length-1; i++)

        {

        var layerArea1 = (layers.bounds[2] - layers.bounds[0]) * (layers.bounds[3] - layers.bounds[1]);

        var layerArea2 = (layers[i+1].bounds[2] - layers[i+1].bounds[0]) * (layers[i+1].bounds[3] - layers[i+1].bounds[1]);

        if (layerArea1 >= layerArea2)

            {

            layers.move(layers[i+1], ElementPlacement.PLACEAFTER);

            i = -1; // restart the cycle

            }                  

        }

    }

arrangeByArea();

alert("Done");

I will not explain. Sorry. I find it difficult in English.

UPD.

Need to change >= to >.
Otherwise, with the same area, the script will loop endlessly.