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

get layer out of array by name

Engaged ,
Dec 05, 2018 Dec 05, 2018

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.

TOPICS
Actions and scripting
2.4K
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

People's Champ , Dec 06, 2018 Dec 06, 2018

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;   

}   

...
Translate
Adobe
Community Expert ,
Dec 05, 2018 Dec 05, 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?

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 ,
Dec 05, 2018 Dec 05, 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);

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
Engaged ,
Dec 06, 2018 Dec 06, 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"];

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
People's Champ ,
Dec 06, 2018 Dec 06, 2018

cmoke73 

var myLayer = allLayers["name"];

Instead of

allLayers.push(theLayer);  

use

allLayers[theLayer.name] = theLayer;

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

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
Engaged ,
Dec 06, 2018 Dec 06, 2018

Nope.

that´s "undefined".

I didn´t mentioned, but it was also part of my trials.

just like:

var myLayer = allLayers.getByName("name");

var myLayer = allLayers.children("name");

var myLayer = allLayers.children.getByName("name");

var myLayer = allLayers.layers.getByName("name");

var myLayer = allLayers.layers["name"];

var myLayer = allLayers.layers.name("name")

...

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
People's Champ ,
Dec 06, 2018 Dec 06, 2018

cmoke73 

Nope.

that´s "undefined".

Explain clearly and completely what you want.
I do not understand :)

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
Engaged ,
Dec 06, 2018 Dec 06, 2018

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.

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
People's Champ ,
Dec 06, 2018 Dec 06, 2018

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?

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
Engaged ,
Dec 06, 2018 Dec 06, 2018

Yes, indeed. Perfect, thanks!

I just have to change PLACEBEFORE to PLACEAFTER.

But what exactly happens in line 10?

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
People's Champ ,
Dec 06, 2018 Dec 06, 2018

cmoke73

But what exactly happens in line 10?

It's hard for me to explain. But this is such a feature of accessing array elements in javascript.
)

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 ,
Dec 06, 2018 Dec 06, 2018

cmoke73  schrieb

… But what exactly happens in line 10?

This is not a 'simple' Array - this is an associative array object eg as:

object = { 'string': 'value1' } is the same as

object ['string'] = 'value'; // object - key - value

in array notation:

allLayers[theLayer.name] = theLayer;

allLayers["Layer5"] = theLayer;

another notation could be (while the key also can be an array property):

allLayers.Layer5 = theLayer;

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
Engaged ,
Dec 07, 2018 Dec 07, 2018
LATEST

Thanks for explanation, pixxel schubser. Usefull for next time.

PS. Du hattest doch letztens nach meinem script gefragt im thread. Wolltest du dazu noch etwas Aufklärungsarbeit beisteuern? Kein push!!!

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