Copy link to clipboard
Copied
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.
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;
}
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
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"];
Copy link to clipboard
Copied
var myLayer = allLayers["name"];
Instead of
allLayers.push(theLayer);
use
allLayers[theLayer.name] = theLayer;
P.S. Maybe: var myLayer = allLayers[name]; ???????
Copy link to clipboard
Copied
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")
...
Copy link to clipboard
Copied
Nope.
that´s "undefined".
Copy link to clipboard
Copied
Ok, sorry if it was misleading.
My PS-document looks like:
top-layerSet 0
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Yes, indeed. Perfect, thanks!
I just have to change PLACEBEFORE to PLACEAFTER.
But what exactly happens in line 10?
Copy link to clipboard
Copied
But what exactly happens in line 10?
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
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!!!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now