11 Replies Latest reply on Dec 3, 2009 3:12 PM by Jo_2005

    How to use an if statement in javascript code

    Jo_2005 Level 1

      Hello,

       

      I have a batch processing script to search for text "employee signature" on each page in a multiple page file and to then list in the console any pages that do not have the "Employee Signature" text included.

       

      The script is not yet functional as an if statement needs to be included.

       

      Can anyone please advise how to use an if statement in javascript code?

       

      var numpages = this.numPages;
      for (var i=0; i < numpages; i++)

      search.query("Employee Signature", "ActiveDoc");
      console.println('Pages that do not include an employee signature: ' + this.pageNum +' ');

       

      Any assistance will be most appreciated.

       

       

        • 1. Re: How to use an if statement in javascript code
          gkaiseril MVP & Adobe Community Professional

          The 'search' object does not return any value that can be used for a logical test. I would look at writing a block of code or function using the 'getPageNthWord' method and check each word.

          1 person found this helpful
          • 2. Re: How to use an if statement in javascript code
            Jo_2005 Level 1

            Thank you very much for your help.

             

            I have used the getPageNthWord statement for my code but have the following errors:

             

            After the script is run in the console window:

             

            • All of the resulting page numbers are showing as 0.
            • Code results keeps looping and can only be exited by using the esc key.

             

            Can you please advise where the code is going wrong?

             

            var ckWord, numWords;
            for (var i = 0; i < this.numPages; i++ )
            {
            numWords = this.getPageNumWords(i);
            for (var j = 0; j < numWords; j++)
            {
            ckWord = this.getPageNthWord(i, j);
            if ( ckWord == "Employee Signature" );
            {
            console.println('Pages that do not include an employee signature: ' + this.pageNum +' ');
            }
            }
            }

            Thank you very much.

            • 3. Re: How to use an if statement in javascript code
              Bernd Alheit Adobe Community Professional & MVP

              if ( ckWord == "Employee Signature" );

              Remove the ';' at this line.

              • 4. Re: How to use an if statement in javascript code
                try67 MVP & Adobe Community Professional

                It won't work anyway because getPageNthWord only retrieves ONE word at a

                time.

                • 5. Re: How to use an if statement in javascript code
                  Bernd Alheit Adobe Community Professional & MVP

                  That is a other issue. Many messages or no message.

                  • 6. Re: How to use an if statement in javascript code
                    gkaiseril MVP & Adobe Community Professional

                    "Employee Signature' is 2 words and not one. The method you are using to obtain the word only obtains one word. You also want to identify the 'not true' found for the page situation and not the 'true' found on page.

                     


                    var ckWords; // word pair to test
                    var bFound = false; // logical status of found words
                    // loop through pages
                    for (var i = 0; i < this.numPages; i++ ) {
                       bFound = false; // set found flag to false
                       numWords = this.getPageNumWords(i); // number of words on page
                       // loop through the words on page
                       for (var j = 0; j < numWords; j++) {
                          // get word pair to test
                          ckWords = this.getPageNthWord(i, j) + ' ' + this.getPageNthWord(i, j + 1); // test words
                          // check to see if word pair is 'Employee Signature' string is present
                          if ( ckWord == "Employee Signature") {
                             bFound = true; // indicate found logical value
                             console.println('Pages that includes an employee signature: ' + (this.pageNum + 1) +' ');
                             break; // no need to further test for this page
                          } // end Employee Signature
                       } // end word loop
                       // test to see if words not found
                       if(bFound == false) {
                             console.println('Pages that do not include an employee signature: ' + (this.pageNum + 1) +' ');
                        } // end not found on page  
                    } // end page loop

                    • 7. Re: How to use an if statement in javascript code
                      Jo_2005 Level 1

                      Thank you very much for your assistance, I really appreciate your help.

                       

                      I have run the code on a 17 page document and the console report lists each page as page 1 (as follows).

                       

                       

                      Pages that do  include an employee signature: 1
                      Pages that do include an employee signature:  1
                      Pages that do  include an employee signature: 1
                      Pages that do  include an employee signature: 1
                      Pages that do  include an employee signature: 1
                      Pages that do  include an employee signature: 1
                      Pages that do  include an employee signature: 1
                      Pages that do  include an employee signature: 1
                      Pages that do  include an employee signature: 1
                      Pages that do  include an employee signature: 1
                      Pages that do  include an employee signature: 1
                      Pages that do  include an employee signature: 1
                      Pages that do  include an employee signature: 1
                      Pages that do  include an employee signature: 1
                      Pages that do  include an employee signature: 1
                      Pages that do  include an employee signature: 1
                      Pages that do  include an employee signature: 1

                      Can you please advise why the script is only showing page 1?

                       

                      Thank you.

                       

                      • 8. Re: How to use an if statement in javascript code
                        try67 MVP & Adobe Community Professional

                        There's an error in the script. Where it says:

                        this.pageNum + 1

                        replace it with just:

                        i + 1

                        • 9. Re: How to use an if statement in javascript code
                          Jo_2005 Level 1

                          Thank you very much for your assistance try.

                           

                          I have modified the code as suggested and the page numbers are now listing correctly, thank you, but....................,

                           

                          The console  lists every page as having an "employee signature" when there are pages in the document that do not have an employee signature.

                           

                          The code (revised as follows) is not processing the "getPageNthWord part of the statement" in the console report?

                           

                          Can you please advise where the code needs reworking?

                           

                          var ckWords; // word pair to test
                          var bFound = false; // logical status of found words
                          // loop through pages
                          for (var i = 0; i < this.numPages; i++ ) {
                             bFound = false; // set found flag to false
                             numWords = this.getPageNumWords(i); // number of words on page
                             // loop through the words on page
                             for (var j = 0; j < numWords; j++) {
                                // get word pair to test
                                ckWords = this.getPageNthWord(i, j) + ' ' + this.getPageNthWord(i, j + 1); // test words
                                // check to see if word pair is 'Employee' string is present
                                if ( ckWord == "Employee") {
                                   bFound = true; // indicate found logical value
                                   console.println('Pages that includes an employee signature: ' + (i + 1) +' ');
                                   break; // no need to further test for this page
                                } // end Employee Signature
                             } // end word loop
                             // test to see if words not found
                             if(bFound == false) {
                                   console.println('Pages that do include an employee signature: ' + (i + 1) +' ');
                              } // end not found on page  
                          } // end page loop

                           

                          Thank you

                           

                          • 10. Re: How to use an if statement in javascript code
                            try67 MVP & Adobe Community Professional

                            The code was pretty buggy to begin with (I would have done this differently), and you changed it but did not solve the bugs.

                             

                            In the original code this line appeared:

                            if ( ckWord == "Employee Signature") {

                             

                            You changed it into this:

                            if ( ckWord == "Employee") {

                             

                            In fact, it should have been this:

                            if ( ckWords == "Employee Signature") {

                             

                            Another issue is with the second for-loop. It should run up to numwords-1, not numwords, or the script might crash.

                            1 person found this helpful
                            • 11. Re: How to use an if statement in javascript code
                              Jo_2005 Level 1

                              Your a legend try, I have implemented all of the suggestions you have made to the script and it now works perfectly.

                               

                              Thank you for giving your time and knowledge.   The script will now search for the words "Employee Signature" and report in the console with the page numbers that "Employee Signature" does appear and also lists the page numbers that "Employee Signature" does not appear, exactly what was required.

                               

                              The completed script is as follows for reference:

                               

                              var ckWords; // word pair to test
                              var bFound = false; // logical status of found words
                              // loop through pages
                              for (var i = 0; i < this.numPages; i++ ) {
                                 bFound = false; // set found flag to false
                                 numWords = this.getPageNumWords(i); // number of words on page
                                 // loop through the words on page
                                 for (var j = 0; j < numWords-1; j++) {
                                    // get word pair to test
                                    ckWords = this.getPageNthWord(i, j) + ' ' + this.getPageNthWord(i, j + 1); // test words
                                    // check to see if word pair is 'Employee Signature' string is present
                                    if ( ckWords == "Employee Signature") {
                                       bFound = true; // indicate found logical value
                                       console.println('Pages that includes an employee signature: ' + (i + 1) +' ');
                                       break; // no need to further test for this page
                                    } // end Employee Signature
                                 } // end word loop
                                 // test to see if words not found
                                 if(bFound == false) {
                                       console.println('Pages that do not include an employee signature: ' + (i + 1) +' ');
                                  } // end not found on page  
                              } // end page loop