= v ==

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

essejesse

= v ==

Post by essejesse »

I wrote a simple little script to identify how many layers a photoshop document had, and if it was one, add a layer, display an alert and then delete the layer.

It looked like this.
Code: Select allvar myDoc = app.activeDocument
var layerCount = new Array;
layerCount = myDoc.artLayers.length
if (layerCount = 1) {
    myDoc.artLayers.add()
    app.refresh()
    alert ("you should have an extra layer right now")
    myDoc.activeLayer.remove();
}

Problem was, regardless of whether it had one layer or more, it added a layer, displayed the alert, and then removed the added layer.
I ended up changing
Code: Select allif (layerCount = 1) {
to
Code: Select allif (layerCount == 1) {

and it started working as I wanted. But this made me realize that while I often spot this mistake when I make it, I'm not entirely sure why I should use one or the other.

Can someone explain it to me?

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

Mike Hale

= v ==

Post by Mike Hale »

It's basic JavaScript. A single equal sign is used for assignment.Code: Select allif (layerCount = 1) {// set LayerCount to 1( always true )Using double( or triple ) equal signs are used for comparison.Code: Select allif (layerCount == 1) {// if LayerCount equals 1 then do something

Note that document.artLayer.length will only show the number of top level artLayers. It will not count layerSet or the artLayers in those sets.
essejesse

= v ==

Post by essejesse »

I knew it had to be something simple, thanks.

I'm going at this using only the Adobe provided PDFs for scripting their programs, and forums like this. I don't have much of a need for javascript outside of the Adobe world, but it's been massively helpful (and fun) to learn this stuff.

I don't need it to recognize layer sets or the layers in them right now, but I'm going to look into how to count them anyway. In case I need it in the future.