0

I have a switch() that needs to contain multiple conditions for it to be true.
The online community tells me I should separate them in two cases and then define it. like so:

function changeGrep(searchFor){
app.findGrepPreferences.findWhat = searchFor;
var myFound = myDoc.findGrep();
for(i=0; i<myFound.length; i++){
    switch(searchFor){
        case "^201\\d$":
        case myFound[i].parent.fillColor == app.activeDocument.swatches.item(14):
            myFound[i].parent.fillColor = app.activeDocument.swatches.item(3);
            break;

        case "^-?\\+?\\(?((\\d+,)?(\\d+,)?(\\d+)(\\.\\d+)?%?\\)?)$":
        case myFound[i].parent.fillColor == app.activeDocument.swatches.item(14):
            myFound[i].parent.fillColor = app.activeDocument.swatches.item(4);
            break;

        }
    }
}

changeGrep("^-?\\+?\\(?((\\d+,)?(\\d+,)?(\\d+)(\\.\\d+)?%?\\)?)$");
changeGrep("^201\\d$");

To be complete at first the entire table is placed in a red color (14). If two conditiond are true it should change the color. However it does't care about the second case.

Any ideas on how to do this in extendscript?

Interactive
  • 1,474
  • 5
  • 25
  • 57

2 Answers2

1

You could try this as a vanilla JS solution. Pass true to switch statement, and then each case can contain a condition.

switch (true) {
  case (<condition> && <condition>):
    // do something
    break;
  case (searchFor === '^201\\d$'):
    // do other thing
    break;
  case ((searchFor === '^201\\d$') && (myFound[i].parent.fillColor == app.activeDocument.swatches.item(14)):
    // code
    break;
}
Davion
  • 881
  • 6
  • 12
1

I think this could be a lot cleaner (and also faster), if you skip the switch statement.

I would first figure out, what your replacement swatch is going to be and then in the loop you just need to figure out, if the parent object has the correct swatch for replacement, if so, replace with the replacement swatch.

function changeGrep(searchFor){
  app.findGrepPreferences.findWhat = searchFor;
  var myFound = myDoc.findGrep();

  var myReplaceSwatch;
  if(searchFor === "^201\\d$") {
    myReplaceSwatch = app.activeDocument.swatches.item(3);
  } else if (searchFor === "^-?\\+?\\(?((\\d+,)?(\\d+,)?(\\d+)(\\.\\d+)?%?\\)?)$") {
    myReplaceSwatch = app.activeDocument.swatches.item(4);
  }

  for (var i = 0; i < myFound.length; i++) {
    if(myReplaceSwatch && myFound[i].parent.fillColor === app.activeDocument.swatches.item(14) {
      myFound[i].parent.fillColor = myReplaceSwatch;
    }
  }
}

changeGrep("^-?\\+?\\(?((\\d+,)?(\\d+,)?(\\d+)(\\.\\d+)?%?\\)?)$");
changeGrep("^201\\d$");

Or, to simplify even more, you could just pass the index of the swatch to the function as well:

function changeGrep(searchFor, swatchIndex){
  app.findGrepPreferences.findWhat = searchFor;
  var myFound = myDoc.findGrep();

  var myReplaceSwatch = app.activeDocument.swatches.item(swatchIndex);

  for (var i = 0; i < myFound.length; i++) {
    if(myReplaceSwatch && myFound[i].parent.fillColor === app.activeDocument.swatches.item(14) {
      myFound[i].parent.fillColor = myReplaceSwatch;
    }
  }
}

changeGrep("^-?\\+?\\(?((\\d+,)?(\\d+,)?(\\d+)(\\.\\d+)?%?\\)?)$", 4);
changeGrep("^201\\d$", 3);
mdomino
  • 1,195
  • 1
  • 8
  • 22