Skip to main content
Inspiring
December 5, 2018
Answered

get layer out of array by name

  • December 5, 2018
  • 2 replies
  • 2415 views

Hi everybody!

I know, it is a very basic thing.

But I searched the whole day for a solution. Now I hope somebody laughing out loud about and gives me a hint..

.

If I collect all layers in an array (some are on the hoghest level, others in layser sets) and wanted to get one of them out of the array searching for its name how I can do this?

var myDoc = app.activeDocument;

var allLayers = [];

var allLayers = collectAllLayers(myDoc, allLayers);

function collectAllLayers (myDoc, allLayers){

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

        var theLayer = myDoc.layers;

        if (theLayer.typename === "ArtLayer"){

            allLayers.push(theLayer);

        }else{

            collectAllLayers(theLayer, allLayers);

        }

    }

    return allLayers;

}

alert(allLayers[4]);

If I search by the key it works. But you never know on which position the layer is.

I need the layer as an object to move it somewhere else.

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

Ok, sorry if it was misleading.

My PS-document looks like:

top-layerSet 0

  •     layerSet 1
    • layer 1
    • layer 2
    • layer 3
    • layer 4
  •     layerSet 2
    • layer 5
    • layer 6
    • layer 7
    • layer 8
    • layer 9
    • layer 10
  •     layerSet 3
  •     layerSet 4
  •     layerSet 5
    • layer 11
  •     layerSet 6
  •     layerSet 7
  •     layerSet 8

I generate a document with layerSets and layers with a script. But some levels are placed elsewhere than they should be. Now I want to move (in this exmeple) layer 5 in layerSet 5, after layer 11.

First I collect all layers in an array and then I want to get one out of it by name. Actually, that's all.


Take the script from Chuck Uebele.

Change it.

var myDoc = app.activeDocument;   

   

var allLayers = [];   

var allLayers = collectAllLayers(myDoc);   

   

function collectAllLayers (group){   

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

        var theLayer = group.layers;   

        if (theLayer.typename === "ArtLayer"){   

            allLayers[theLayer.name] = theLayer;   

        }else{   

            collectAllLayers(theLayer);   

        }   

    }   

    return allLayers;   

}   

   

alert( allLayers["Layer 5"] ); 

var l1 = allLayers["Layer 5"];

var l2 = allLayers["Layer 11"];

if (l1 && l2) l1.move(l2, ElementPlacement.PLACEBEFORE);


This is not what you want?

2 replies

Chuck Uebele
Community Expert
Community Expert
December 5, 2018

I also made some changes to your script so it works to put the layers in the array. You're recursive function was wrong. You don't want to keep putting the myDoc into each cycle of the function. You need to change that to whatever is the group layer. You also don't need to keep sending the function the array. It's defined outside the function, so it will collect all the layers by just having that one push statement.

var myDoc = app.activeDocument; 

 

var allLayers = []; 

var allLayers = collectAllLayers(myDoc); 

 

function collectAllLayers (group){ 

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

        var theLayer = group.layers

        if (theLayer.typename === "ArtLayer"){ 

            allLayers.push(theLayer); 

        }else{ 

            collectAllLayers(theLayer); 

        } 

    } 

    return allLayers; 

 

alert(allLayers);

cmoke73Author
Inspiring
December 6, 2018

Thanks Chuck, but that was not the difficulty. My snippet worked as well. What I need at the end is something like...

var myLayer = allLayers["name"]; 

var myLayer = allLayers["name"];

...to get out of the array a certain layer by its name.

as I said, this works fine:

var myLayer = allLayers["4"];

Legend
December 6, 2018

cmoke73 

var myLayer = allLayers["name"];

Instead of

allLayers.push(theLayer);  

use

allLayers[theLayer.name] = theLayer;

P.S. Maybe: var myLayer = allLayers[name]; ???????

Chuck Uebele
Community Expert
Community Expert
December 5, 2018

I think what you'll have to do is create another loop that runs through your layer array and then check the layer's name by either it being in the array, or referencing it to the doc, then pushing that to a new array. having the layers in groups would be an issue though, as you're array won't know what group a layer is in. Can you drop layer's you don't want while creating the original array with a switch or if statement with the names listed that you don't want?