Skip to main content
Inspiring
July 28, 2016
Answered

If variable exists.....object vs variable, null vs. undefined

  • July 28, 2016
  • 4 replies
  • 717 views

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, in line 37 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;

    var curDocNoExt = curDoc.name.split(".");

    var workingName = curDocNoExt[0];

    if(workingName.match(/^\d{5,6}$/) != null) { 

        var frontArt = app.documents;

        var targetName = frontArt.name

    } else {  

    if(workingName.match(/^\d{5,6}(b))$/) != null) {  

        var backArt = app.documents;

        var backToggle = 1;

    } else {

     if(workingName.match(/^fkeep$/) != null) { 

        var frontTemp = app.documents;

    } else {

     if(workingName.match(/^fxkeep$/) != null) { 

        var frontSquare = app.documents;

    } else {

     if(workingName.match(/^bkeep$/) != null) { 

        var backTemp = app.documents;

    } else {

     if(workingName.match(/^bxkeep$/) != null) { 

        var backSquare = app.documents;

      }

     }  

    }

   }

  }

}

}

//use variables to do stuff!

if (backArt != null) {

    app.activeDocument = backTemp;

    var namedBackTemp = backTemp.duplicate(targetName + "B");

}

I have also this on line 37, but they didn't seem to fix things:

if (backArt != 'undefined') {

This topic has been closed for replies.
Correct answer Kukurykus

Isn't there /^\d{5,6}(b))$/ one closing parentheses too many (that before dollar character)?

4 replies

pixxxelschubser
Community Expert
Community Expert
July 29, 2016

Hi squirpy​,

you doesn't need the parentheses aroud the "b". But there are some other problems with your code. Please explain exactly what you really want to do.

Perahps this snippet could help you a little bit.

// do you want to close the document after the loop???

// in this case: loop backwards

for(i = app.documents.length-1; i >= 0; i--) {

    var aDoc = app.activeDocument = app.documents;

    var fileName = aDoc.name;

    var workingName;

    if(fileName.match( /(.*)\.([^\.]+$)/ )) {

    fileName = fileName.match( /(.*)\.([^\.]+$)/ );

    } else {

        fileName = [ fileName, fileName, undefined ];

        }

    workingName = fileName[1];

    switch(true){

    case (workingName.match(/^\d{5,6}b$/) != null):

    var backArt = aDoc; // difficult - if more than one file with this name is opened and the document is still opened after this loop

    var backToggle = 1;

    break;

   

    case (workingName.match(/^fkeep$/) != null):

    var frontTemp = aDoc; // IMHO not needed, you can use in the next line aDoc instead

    var namedFrontTemp = frontTemp.duplicate(workingName + "F"); // difficult - if more than one file with this name is opened and the document is still opened after this loop

    // Perhaps it could be better to close the frontTemp now

    //aDoc.close(SaveOptions.DONOTSAVECHANGES);

    break;

   

    // and so on …

   

    default:

    // all other document names

    break;

    }

}

Have fun

Kukurykus
KukurykusCorrect answer
Legend
July 28, 2016

Isn't there /^\d{5,6}(b))$/ one closing parentheses too many (that before dollar character)?

squirpyAuthor
Inspiring
July 29, 2016

It was the extra parentheses (hangs head in shame)

Inspiring
July 28, 2016

Should be

if (typeof(backArt) == "undefined") {

Kukurykus
Legend
July 28, 2016

I'm not sure, but perhaps you shouldn't use == or != in conditions, but === or !==

null == undefined gives true

null === undefined gives false