I am writting a script to check my images. Here is what I need to check:
#target photoshop
var doc = app.activeDocument;
var pLen = doc.pathItems.length;
var pArray = [];
var nLen = doc.layers.length;
var cArray = [];
if (nLen != 3)
{alert ("Wrong structure")}
else if (pLen != 2 )
{alert ("Wrong path item")}
else {for (var i=0; i<nLen; i++)
{if (doc.layer[i].getByName ("Freisteller") == false || doc.layer[i].getByName ("Hintergrund") == false || doc.layer[i].getByName ("Hintergrund BR") == false )
{alert ("Wrong layer name")}
}
}
I aslo tried this but it doesn't work
#target photoshop
var doc = app.activeDocument;
var pLen = doc.pathItems.length;
var pArray = [];
var nLen = doc.layers.length;
var cArray = [];
var layerName = ["Freisteller", "Hintergrund", "Hintergrund BR"];
if (nLen != 3)
{alert ("Wrong structure")}
else if (pLen != 2)
{alert ("Wrong path item")}
else {for (var i=0; i<nLen; i++)
{if (doc.layer[i].getByName ("Freisteller") != layerName || doc.layer[i].getByName ("Hintergrund") != layerName || doc.layer[i].getByName ("Hintergrund BR") != layerName )
{alert ("Wrong layer name")}
}
}
Please help me to fix it
You need to look for typename == "ArtLayer"
for normal, standard art layers, and layer.kind == "LayerKind.SMARTOBJECT"
for smart objects. And layer.isBackgroundLayer == true for background layers.
Your script didn't work as "getbyname" isn't quite what it says.
doc.activeLayer = doc.artLayers.getByName("Freisteller");
Will get the layer called "Freisteller", if it exists. Also if there are several layers called that it'll grab the first. You were trying to "get" it from layer i.
This is your script with a couple of changes.
#target photoshop
var doc = app.activeDocument;
var pLen = doc.pathItems.length;
var pArray = [];
var nLen = doc.layers.length;
var cArray = [];
var layerName = ["Freisteller", "Hintergrund", "Hintergrund BR"];
if (nLen != 3)
{
alert ("Wrong structure\n" + nLen + " layers in file.");
}
else if (pLen != 2)
{
alert ("Wrong path item");
}
else
{
for (var i = nLen -1; i >= 0; i--)
{
//if (doc.layer[i].getByName ("Freisteller") != layerName || doc.layer[i].getByName ("Hintergrund") != layerName || doc.layer[i].getByName ("Hintergrund BR") != layerName )
var n = doc.layers[i].name;
if (n == "Freisteller" || n == "Hintergrund" || n == "Hintergrund BR")
{
// all good
// alert(n);
}
else alert ("Wrong layer name");
}
}
Also, I think this might help you out.
// call the source document
var srcDoc = app.activeDocument;
var numOfLayers = srcDoc.layers.length;
// main loop
for (var i = numOfLayers -1; i >= 0; i--)
{
var thisLayer = srcDoc.layers[i];
srcDoc.activeLayer = thisLayer;
var k = what_type(thisLayer);
alert(thisLayer.name + "\nis a " + k);
}
function what_type(alayer)
{
if (alayer.isBackgroundLayer == true) return "background layer";
var lk = alayer.kind;
if (lk == "LayerKind.SMARTOBJECT") return "smart object";
else if (lk == "LayerKind.TEXT") return "text layer";
if (alayer.typename == "ArtLayer") return "normal art layer";
else return undefined;
}