photoshopphotoshop-script

How to condition file name shorter?


I have some images named: e14_A_0002, e14_B_0002, e14_8_0001, e14_9_0001 ... I used to set condition for the file name like below and it works good:

var fileName1 = doc.name.indexOf ("_A_") != -1 || doc.name.indexOf ("_B_") != -1;
var fileName2 = doc.name.indexOf ("_8_") != -1 || doc.name.indexOf ("_9_") != -1;

if ( fileName1 == true ) 
{
    doAction (1)
}

else if ( fileName2 == true )
{
    doAction (2)
}

else 
{
    doAction (3)
}

But the variable will be too long if I list all of them. I've tried // var fileName = doc.name.indexOf ("7") , ("8") != -1 // but it doesn't work. Please help me if there is a shorter way for that!!!


Solution

  • You want a way to to peform an action based on the filename. But using if else gets a bit clunky. This should springboard you to where you want to go.

    var fileName = "e14_A_0002"; // doc.name
    var letter = fileName[4]; //  "A" in this example
    
    
    switch (letter)
    {
      case "A": action = 1;
                break;
      case "B": action = 2;
                break;
      default:  action = 3;
    }
    
    alert(letter + ": doAction(" + action + ")");