Count visible layerSets (warning: total newb)

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

applezap

Count visible layerSets (warning: total newb)

Post by applezap »

Hey everyone,

I am trying to make a script that will count the number of visible layerSets in a document. I thought it would be something like below, but it keeps returning 1 regardless of how many layerSets are showing.

for (i = 0; i < activeDocument.layerSets.length.visible = true; i++) {
}
alert (i);

Thanks!

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Mike Hale

Count visible layerSets (warning: total newb)

Post by Mike Hale »

You have both a common javascript error and a Photoshop Object Model error in your code.

activeDocument.layerSets.length is a property of the layerSets collection and returns a Number. Numbers don't have a visible property.

But you can extend object in javascript so your code adds a new property visible and sets it to true. As a side note you use == or === for equates and = to assign.

var count = 0;
for (i = 0; i < activeDocument.layerSets.length; i++) {
if(activeDocument.layerSets[1].visible == true) count++;
}
alert (count);

Note that will only count the visible top level layerSets. It will not count any nested layerSets. To count all the possible layerSets in a document you can either make a recursive function or use Action Manager.
applezap

Count visible layerSets (warning: total newb)

Post by applezap »

Mike Hale wrote:You have both a common javascript error and a Photoshop Object Model error in your code.

activeDocument.layerSets.length is a property of the layerSets collection and returns a Number. Numbers don't have a visible property.

But you can extend object in javascript so your code adds a new property visible and sets it to true. As a side note you use == or === for equates and = to assign.

var count = 0;
for (i = 0; i < activeDocument.layerSets.length; i++) {
if(activeDocument.layerSets[1].visible == true) count++;
}
alert (count);

Note that will only count the visible top level layerSets. It will not count any nested layerSets. To count all the possible layerSets in a document you can either make a recursive function or use Action Manager.

I changed the [1] after layerSets to and it worked perfectly. Thanks very much!
Mike Hale

Count visible layerSets (warning: total newb)

Post by Mike Hale »

Yeah, it should have been an i. Typos are something else to add to the list of 'why things don't work'.