0

The first part of this script seems to work correctly, it iterates through every document and if the document name matches a particular regex pattern, it it gives it a specific variable to be utilized later in the script.

However, at the end of the script when I attempt to us whether or not a variable exists as a condition for an if statement, things don't evaluate true or false as expected. What am I doing wrong here?

// iterate through all docs assigning variables to templates and art  
for (i = 0; i < documents.length; i++) {
  var curDoc = app.activeDocument = app.documents[i];
  var curDocNoExt = curDoc.name.split(".");
  var workingName = curDocNoExt[0];
  if (workingName.match(/^\d{5,6}$/) != null) {
    var frontArt = app.documents[i];
    var targetName = frontArt.name
  } else {
    if (workingName.match(/^\d{5,6}(b))$/) != null) {
      var backArt = app.documents[i];
      var backToggle = 1;
    } else {
      if (workingName.match(/^fkeep$/) != null) {
        var frontTemp = app.documents[i];
      } else {
        if (workingName.match(/^fxkeep$/) != null) {
          var frontSquare = app.documents[i];
        } else {
          if (workingName.match(/^bkeep$/) != null) {
            var backTemp = app.documents[i];
          } else {
            if (workingName.match(/^bxkeep$/) != null) {
              var backSquare = app.documents[i];
            }
          }
        }
      }
    }
  }
}

//use variables to do stuff!  

if (backArt != null) {
  app.activeDocument = backTemp;
  var namedBackTemp = backTemp.duplicate(targetName + "B");
}
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
Andrew Hall
  • 139
  • 7
  • 2
    Just so you know, you can use [`else if`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else#Using_else_if) to avoid all of this nesting. It makes your code a lot more readable. – Mike Cluck Jul 28 '16 at 21:21
  • if the very first if condition is true in your code then the variable backart never even gets defined.... hence you get reference error – Sai Jul 28 '16 at 21:23
  • 1
    @Sai They wouldn't get a reference error, it would just be `undefined`. `backArt` is [hoisted](https://developer.mozilla.org/en-US/docs/Glossary/Hoisting) to the top of the function scope. – Mike Cluck Jul 28 '16 at 21:25
  • 2
    There is a difference between `null` and `undefined` in JavaScript. – user1620220 Jul 28 '16 at 21:25
  • @MikeC - you are correct , my apologies – Sai Jul 28 '16 at 21:29
  • All of the variables will definitely exist even if they haven't been assigned values, but the structure of your code is such that `backArt` and `backTemp` cannot both be assigned a value within a single iteration of the loop. So depending on what other values are in your document array, it may be that `backArt` has a value but `backTemp` doesn't. Or vice versa. (If there happened to be only one document then most of the variables would not have a value.) – nnnnnn Jul 28 '16 at 21:39
  • There will never be more than one instance of each regex pattern, so it doesn't matter if 2 variables can't be assigned on a single iteration. There are always at least 3 matches, sometimes all 6. Every time each one must be assigned to it's variable. I'm trying to make something happen ONLY IF the backArt variable has been assigned. This my issue? [link] (http://stackoverflow.com/questions/2647867/how-to-determine-if-variable-is-undefined-or-null) – Andrew Hall Jul 28 '16 at 23:12
  • At the very least I know that multiple variables ARE being defined. For example at the end of the script i can rename frontTemp with the name of frontArt, then rename backTemp with the name of frontArt. The problem seems is to be the If backArt is NOT null, which is admittedly a double negative. Is there a way to have the condition if be if backArt IS defined? – Andrew Hall Jul 28 '16 at 23:36
  • OK....this is embarrassing. The problem was caused by an extra parenthesis in the backArt regex pattern. The silver lining is that I learned some stuff. – Andrew Hall Jul 29 '16 at 00:28

1 Answers1

1

In javascript undefined is falsey, so you can just use the variable in the if statement like so:

var var1 = '', // can be anything
    var2; // this is an undefined var
if (var1){ // var1 has been initialized so this evaluates to true
  doSomething(); // this will execute
}
if (var2){ // var2 is undefined, so it evaluates as false
  doSomethingElse(); // this will not execute
}

However, A better practice is to use typeof, which returns a string of the type of object:

var var1 = '';
var var2 = {};

typeof var1 == 'string';
typeof var2 == 'object';
typeof var3 == 'undefined';

if (typeof var1 !== 'undefined'){
  doSomething(); // this gets executed because var1 is a string
}

hopefully this gives you a better understanding

Hans Strausl
  • 605
  • 5
  • 11
  • I don't think this would change the result in the OP's case, because their current `if (backArt != null)` test will be `true` if `backArt` is `undefined` *or* `null` (because of the difference between `!=` and `!==`). – nnnnnn Jul 28 '16 at 21:38
  • @nnnnnn I am suggesting he just use `if (backArt){...}` instead of `if (backArt != null){..}` – Hans Strausl Jul 28 '16 at 21:41
  • Yes, and I'm saying that that won't change the behaviour if used as an undefined test (because `undefined == null` is `true`). – nnnnnn Jul 28 '16 at 21:45
  • 1
    @nnnnnn Oh yes my bad, my ego made me read your comment wrong haha. – Hans Strausl Jul 28 '16 at 21:52