2 Replies Latest reply on Sep 1, 2015 2:13 PM by Whatevermajorloser

    While loop going on infinitely?

    Whatevermajorloser Level 1

      I've got this script that finds the layer Tone and duplicates it if it finds it.  For some reason it doesn't accept my match = true at the end and just keeps duplicating the layer until I terminate the script.

       

      var match = false
      var front = /fr/gi;
      var back = /bk/gi;
      var outfit = /ou/gi;
      var currentDoc = activeDocument.name;
      
      while(match === false) {
          for(i = 0; i <app.documents.length; i++)
              if(front.exec(app.documents[i].name) || back.exec(app.documents[i].name) || outfit.exec(app.documents[i])){
                  for (n = 0; n < app.documents[i].layers.length; n ++)
                      if(app.documents[i].layers[n].name === 'Tone'){
                          app.documents[i].layers[n].duplicate (app.documents.getByName(currentDoc))
                          match = true
                          }
              }
      }
      

       

      What am I doing wrong?

        • 1. Re: While loop going on infinitely?
          JJMack Most Valuable Participant

          Are you sure the two inner loop are ending.  Looks like you may be missing a { or two in the for statement. After the match  = true; statement add a break; statement to end the n loop also there is a blank between the n and the ++ in the  n for statement.    in the i loop  what is ending that loop perhaps it should  for(i = 0; ( i<app.documents.length && match===false); i++)   {

           

          I also don't know javascript and find

          if(front.exec(app.documents[i].name) || back.exec(app.documents[i].name) || outfit.exec(app.documents[i]))

           

          strange the first two have .name and the last one does not where you defined  front back and outfit the same way.

          • 2. Re: While loop going on infinitely?
            Whatevermajorloser Level 1

            Hi JJMack, thanks for your reply.The match = true; and && match === false were indeed what I was missing. It works perfectly now! Thank you.