Skip to main content
Participating Frequently
March 7, 2021
Answered

Script to play action via top layer name

  • March 7, 2021
  • 2 replies
  • 415 views

Hi everyone

 

I've been playing around with scripting for a little while, however I don't quite understand what all is going on.

This is the script I've come up with:

 

#target photoshop

var s = app.activeDocument;

if(putName = "Flash 4" ){
app.doAction('4 Layer Flash', 'New Script Actions')
}
else {
if (putName = "Flash 3" ){
app.doAction('3 Layer Flash', 'New Script Actions')
}
else {
if (putName = "Flash 2" ){
app.doAction('2 Layer Flash', 'New Script Actions')
}
else {
if (putName = "No Flash 2" ){
app.doAction('2 Layer No Flash', 'New Script Actions')
}
}
}
}

 

What I was hoping this would do is run an action based off the top layers name which I would change to depending on how many layers I have and what I want it to do.

The issue i'm having is that even if I specify the name of the layer e.g No Flash 2 it would still run Flash 4.

 

Clearly I am doing something wrong.

 

Could anyone help me rectify where my mistakes are and what I should change?

This topic has been closed for replies.
Correct answer Chuck Uebele

Your script should be set up something like this:

 

 

#target photoshop
var doc = acitveDocument;
var putName = doc.layers[0].name;

switch (putName){
    case 'Flash 4':
        app.doAction('4 Layer Flash', 'New Script Actions');
        break;
    case 'Flash 3':
        app.doAction('3 Layer Flash', 'New Script Actions');
        break;        
    case 'Flash 2':
        app.doAction('2 Layer Flash', 'New Script Actions');
        break;
    case 'Flash 1':
        app.doAction('1 Layer Flash', 'New Script Actions');
        break;        
    }//end switch

 

2 replies

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
March 8, 2021

Your script should be set up something like this:

 

 

#target photoshop
var doc = acitveDocument;
var putName = doc.layers[0].name;

switch (putName){
    case 'Flash 4':
        app.doAction('4 Layer Flash', 'New Script Actions');
        break;
    case 'Flash 3':
        app.doAction('3 Layer Flash', 'New Script Actions');
        break;        
    case 'Flash 2':
        app.doAction('2 Layer Flash', 'New Script Actions');
        break;
    case 'Flash 1':
        app.doAction('1 Layer Flash', 'New Script Actions');
        break;        
    }//end switch

 

Participating Frequently
March 8, 2021

Thanks so much, I will give it a go

Chuck Uebele
Community Expert
Community Expert
March 7, 2021

you need to either use the else if statement or switch

 

if (some condition) {some code}

else if (a different condition){code}

else if (something else){code}

 

Or a switch:

https://www.w3schools.com/js/js_switch.asp

Participating Frequently
March 7, 2021

Great thanks, I will give it a go.

If you dont mind me asking, which method would be preferrable?

Chuck Uebele
Community Expert
Community Expert
March 7, 2021

I would use the switch method. I'm not at my computer, so it hard to say how to incorporate your script into a switch.