Search code examples
javascriptphotoshopflash-cs5photoshop-script

Photoshop Javascript strange behaviour


I'm trying to figure out if it is something wrong with me or the CS5 JavaScript engine. My Photoshop document have one layer called "A" and a Layer Group called "Group 1".

Consider this example:

var Mess = "";
var Layers = app.activeDocument.layers;
alert(Layers.length);
for (n=0; n<app.activeDocument.layers.length; n++) {
  Mess = Mess + app.activeDocument.layers[n].name + "\r\n";
}
alert(Mess);

This will show 2 for length and then list the layers:
A
Group 1

Everything fine so far. But if we in the foor loop use the variable Layers instead we get a different result.

var Mess = "";
var Layers = app.activeDocument.layers;
alert(Layers.length);
for (n=0; n<Layers.length; n++) {
  Mess = Mess + app.activeDocument.layers[n].name + "\r\n";
}
alert(Mess);

As before we get length 2 but now only one layer is listed:
A

Shouldn't the two code examples produce the same result?!?


Solution

  • I think Layers is a reserved word in the Photoshop DOM. Photoshop CS6 JavaScript Reference

    var Mess = "";
    var lyrs = app.activeDocument.layers;
    alert(lyrs.length);
    for (n=0; n<lyrs.length; n++) {
        Mess = Mess + app.activeDocument.layers[n].name + "\r\n";
    }
    alert(Mess);
    

    This seems to work now after changing the name of the variable.