Max Kielland
Max Kielland

Reputation: 5841

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?!?

Upvotes: 2

Views: 288

Answers (1)

pdizz
pdizz

Reputation: 4240

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.

Upvotes: 4

Related Questions