Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Explorer ,
Jul 28, 2016 Jul 28, 2016

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') {

TOPICS
Actions and scripting
649
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jul 28, 2016 Jul 28, 2016

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

Translate
Adobe
LEGEND ,
Jul 28, 2016 Jul 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jul 28, 2016 Jul 28, 2016

Should be

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 28, 2016 Jul 28, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 28, 2016 Jul 28, 2016

It was the extra parentheses (hangs head in shame)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 28, 2016 Jul 28, 2016
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines