7 Replies Latest reply on Apr 2, 2012 12:11 PM by dgolberg

    Dynamic If Statment

    dgolberg Level 1

      Is it possible to create an if statment with dynamic logical tests?  For example, I have a .csv file that has a file name in the first column, and the columns after that have random tag data.  The first row of the .csv is a header (so it's pretty much just ignored), the second row is the file and tags to be compared to the others, the third row is the header for the file name column and all of the tag category columns after that, and below that are all the files and their tags to be compare row 2 to.  The problem is, not every category of files has the same number of tags, and not all tags may be necessary for the comparison, so I was going to have a dialog window of checkboxes come up listing the header row's non-null tag columns as individual checkboxes.  The if statement would then need to compare all of the selected columns to the other listed files (the on row 4 and below) to find any that contain the exact same tags as the source file (the one in row 2).


      Below is kind of a rough mock-up of what I'm trying to do.

      var baseTag = [];
      baseTag = data[1].split(','); //the comparison row's columns loaded to the baseTag array
      if(checkbox1.value) tag1 = true;
      if(checkbox2.value) tag2 = true;
      if(checkbox3.value) tag3 = true;
      ///etc. etc.///
      //check for any files that match
      for(i=3;i<data.length;i++) { //data being the array containing all the rows of the .csv
           var tag = [];
           tag = data[i].split(',');
           if(baseTag[1] == tag[1] && baseTag[2] == tag[2] && baseTag[3] == tag[3] /*etc*/) { //this needs to be set up so that if tag2 = false, it doesn't do the "baseTag[2] == tag[2]" check; but if it's true, it does check it


      Anyway, any ideas on how to do this?  I have the feeling it's something simple, but just can't seem to figure it out.  Thanks in advance for any help!



        • 1. Re: Dynamic If Statment
          dgolberg Level 1

          Seems I've come up with a stumper.  I'm not even sure if this is doable, but if anyone has any ideas or knows if it's doable or not; let me know.  The script basically just needs to check for files whose tags match the first file's tags and open them in Photoshop so we can compare and get rid of any duplicates.  The biggest obstacle being the random number of tag categories per file category.

          • 2. Re: Dynamic If Statment
            Michael L Hale Level 5

            I think you have way too many conditions to check with just one if statement. My guess is that you will need multiple if statements and it may also help to have a switch statement.


            It's hard to make a suggestion to the logic flow you will need because I an not clear what data those arrays hold.

            • 3. Re: Dynamic If Statment
              dgolberg Level 1

              Hey Michael, thanks for the reply.  The array labelled "baseTag" holds the data for a single row in a .csv file (for simplicity, we'll say it's the very first row).  The array labelled "tag" holds the data for all the remaining rows of the .csv file.  So for example, the .csv might look something like below when opened with a spreadsheet program:


              File NameTag1Tag2Tag3


              FileName1.format is the file we're doing the check on using the tag columns to the right, and if it finds a match using those tags, it opens the file listed in column 1.  So the script would need to make sure all tags match the first row's tags (in this example, only FileName4.format would match FileName1.format's tags).  However, there may be instances where I wish to exclude one of the columns from the check (for example, only check for matches of tag 1 and 3).  So this would require that the if statement's check only look for matches of the tag1 and tag3 columns while ignoring Tag2 (so now FileName2.format would also match).  Essentially, instead of being:


              if(baseTag[1] == tag[1] && baseTag[2] == tag[2] && baseTag[3] == tag[3]) {

              it would then be:

              if(baseTag[1] == tag[1] && baseTag[3] == tag[3]) { 

              if I decide to have it exclude the tag2 column.


              The issue I'm having is making this change to the if statement be dynamic (without a huge mess of inefficient code).  Your mention of a switch statement sounds interesting.  I'm not the greatest programmer yet (pretty much just the really basic stuff) so I hadn't heard about this statement yet; but I'll certainly be looking it up now.  Anyway, hope the info helps clarify it a little better.

              • 4. Re: Dynamic If Statment
                Michael L Hale Level 5

                oK, you can do this in one if statement but I think it is hard to read and understand.



                var tag1 = true;
                var tag2 = true;
                var tag3 = false;
                var baseTag = ['test.jpg','wood','dark','strong'];
                var tag = ['test.jpg','wood','dark','light'];
                if((!tag1 || baseTag[1] == tag[1]) && (!tag2 || baseTag[2] == tag[2] ) && ( !tag3 || baseTag[3] == tag[3] ) /*etc*/) {
                alert('load file');


                You have have if statement that uses the AND operator. In order for that to test true all the expressions need to test true. So for each expression you create another set of expression that uses the OR operator. With the OR operator if either are true it test true.


                So if you want baseTag[1] == tag[1] to test true if tag1 is false( skip this tag ) you create an expression that will test true if tag1 == false. !tag1 || baseTag[1] == tag[1] You then wrap with () so it makes one expression as part of your AND comparisons

                1 person found this helpful
                • 5. Re: Dynamic If Statment
                  Paul Riggott Level 6

                  Would something like this work?



                  var w = new Window('dialog','tag test');
                  w.cb1 = w.add('checkbox',undefined,'Use Tag 1');
                  w.cb2 = w.add('checkbox',undefined,'Use Tag 2');
                  w.cb3 = w.add('checkbox',undefined,'Use Tag 3');
                  w.bu1 = w.add('button',undefined,'Cancel');
                  w.bu2 = w.add('button',undefined,'Process');
                  var csvFile = File.openDialog("Open Comma-delimited File","comma-delimited(*.csv):*.csv;");
                  var Data = csvFile.read();
                  Data = Data.split('\n');
                  Data.shift(); //remove header line
                  var maintag = Data.shift().split(',');
                  var string1 ='';
                  if(w.cb1.value) string1 += maintag[1].toString().replace(/^\s+|\s+$/g,'');
                  if(w.cb2.value) string1 += maintag[2].toString().replace(/^\s+|\s+$/g,'');
                  if(w.cb3.value) string1 += maintag[3].toString().replace(/^\s+|\s+$/g,'');
                  var sectag = Data.shift().split(',');
                  var string2 ='';
                  if(w.cb1.value) string2 += sectag[1].toString().replace(/^\s+|\s+$/g,'');
                  if(w.cb2.value) string2 += sectag[2].toString().replace(/^\s+|\s+$/g,'');
                  if(w.cb3.value) string2 += sectag[3].toString().replace(/^\s+|\s+$/g,'');
                  if(string1 == string2) $.writeln(sectag[0].toString());    
                      }//end while
                  /**************CSV file.*****************************
                  File Name,Tag1,Tag2,Tag3
                  FileName1.format,wood ,dark,strong
                  FileName2.format,wood,light, strong
                  FileName3.format,wood,medium, light
                  FileName4.format,wood, dark  , strong
                  • 6. Re: Dynamic If Statment
                    dgolberg Level 1

                    Hmm, both methods look very promising.  I'll give them a try and see how it goes.  Thanks guys!

                    • 7. Re: Dynamic If Statment
                      dgolberg Level 1

                      Well, it was a tough call, but both methods worked quite well.  Michael's method was a little easier to implement, while Paul's was a little bit more dynamic.  As a result, I decide to give the "Correct" answer credit to Paul, though both are technically correct (if I could give it to 2 people, I would!).  Thanks again for the help guys!  It is very much appreciated!


                      Edit: The thing that makes Pauls more dynamic is the ability to incorporate it with a for loop so that the length can be determined by an array's length, where as the other method is easier to implement, but limited to the scope of the defined if statement.