12 Replies Latest reply on May 1, 2011 6:50 PM by JoFrmOZ

    Find the first occurance of a word

    JoFrmOZ

      I have a script which finds and counts ALL occurances of a word.

       

      I need the script to find the FIRST instance of a word only.

       

      Can any one please advise how to modify the script to find the first instance of the word?

       

      Script as follows:

       

      {

      var numWords = this.getPageNumWords(0);

      for (var i=0; i<numWords; i++)

      {

      var ckWord = this.getPageNthWord(0,i);

      if ( ckWord == "MATERIAL")

      {

      /*Script will go here based on finding the first instance of the word "MATERIAL"*/

      }

      }

      }

       

      I have tried var CKWord = this.getPageNthWord(0,0); but console reports back is UNDEFINED?

        • 1. Re: Find the first occurance of a word
          try67 MVP & Adobe Community Professional

          Inside the if-statement add a break command.

          That will cause the loop that goes over all the words to stop.

          1 person found this helpful
          • 2. Re: Find the first occurance of a word
            JoFrmOZ Level 1

            Thanks for your help.  Script now returns the first occurance of the word "MATERIAL".

             

            I know have to add to the script to find the SECOND OCCURANCE of the word "ISSUED".

             

            Can you please advise how I would code the script for this, ie would this have something to do with the break command?

             

            {

            var numWords = this.getPageNumWords(0);

            for (var i=0; i<numWords; i++)

            {

            var ckWord = this.getPageNthWord(0,i);

            if ( ckWord == "ISSUED")

            {

            var coord = this.mouseX;

             

             

            var annot = this.addAnnot({

            page: 0,

            type: "Stamp",

            name: "AppStamp",

            rect: [coord+1000, 2300, 30, 2820],

            rotate: 90,

            AP: "#C94cHAFFa42U1gTH5Tug5C" }); 

            /*break; how can this be modified to break for the second occurance of the word "ISSUED"?*/

            }

            }

            }

             

            • 3. Re: Find the first occurance of a word
              try67 MVP & Adobe Community Professional

              The break command should not be inside of the if statement, but as a

              stand-along command:

              break;

               

              You should place it after the part that adds the annotation, I'm guessing.

              • 4. Re: Find the first occurance of a word
                JoFrmOZ Level 1

                I have placed the break; command after the annotation and this now works correctly for finding the first occurance of the word 'MATERIAL".

                 

                I have now created another script to find the second occurance of the word "ISSUED", as I need the mouse X co-ordinate of the second word.

                 

                I have tried building on the if statement by adding "&& i == 2" but the console reports back as "UNDEFINED" .

                 

                Can you please advise how to modify the if statement in the script to work correctly?

                 

                {
                var numWords = this.getPageNumWords(0);
                for (var i=0; i<numWords; i++)
                {
                var ckWord = this.getPageNthWord(0,i);
                if ( ckWord == "ISSUED" && i == 2)
                {
                var coord = this.mouseX;


                var annot = this.addAnnot({
                page: 0,
                type: "Stamp",
                name: "AppStamp",
                rect: [coord+1000, 2300, 30, 2820],
                rotate: 90,
                AP: "#C94cHAFFa42U1gTH5Tug5C" }); 
                break;
                /* this.flattenPages(); */
                }
                }
                }

                • 5. Re: Find the first occurance of a word
                  try67 MVP & Adobe Community Professional

                  You need to create a counter that starts with zero and gets increased each

                  time a match is made. Then you add an if-statement in the loop checking if

                  the counter equals two. If so, break the loop.

                  Getting the location of a word can be done using this.getPageNthWordQuads()

                  • 6. Re: Find the first occurance of a word
                    JoFrmOZ Level 1

                    Thanks for your advice.

                     

                    I have added a counter and after running the script the console is reporting back as "undefined", where in fact there are two occurances of the word "ISSUED" in the drawing.

                     

                    Can you please check my code as there may be a problem with the way the counter is set up?

                     

                    {
                    var numWords = this.getPageNumWords(0);
                    for (var i=0; i<numWords; i++)
                    {
                    var ckWord = this.getPageNthWord(0,i);

                    var l = count;
                    for (l = 0; l<ckWord; l++)

                    if ( ckWord == "ISSUED" && l == 2)
                    {
                    break;
                    var coord = this.mouseX;


                    var annot = this.addAnnot({
                    page: 0,
                    type: "Stamp",
                    name: "AppStamp",
                    rect: [coord+1000, 2300, 30, 2820],
                    rotate: 90,
                    AP: "#C94cHAFFa42U1gTH5Tug5C" });

                    /* this.flattenPages(); */
                    }
                    }
                    }
                    Thanks.

                    • 7. Re: Find the first occurance of a word
                      try67 MVP & Adobe Community Professional

                      undefined just means the script ended. If you want the annotation to be

                      added you need to add the break command after the call to this.addAnnot(),

                      not before it, or the script will never arrive to that point.

                      • 8. Re: Find the first occurance of a word
                        JoFrmOZ Level 1

                        I have changed the break; command to after this.annot.

                         

                        Then the script was run and the console reported that "count is not defined".

                         

                        The variable was changed from var l = count; to var l = ckWord.count;

                         

                        After running the script with the changes the console is still reporting back as undefined.

                         

                        Can you please check the revised script as to why the stamp is not being placed on the drawing?

                         

                        {
                        var numWords = this.getPageNumWords(0);
                        for (var i=0; i<numWords; i++)
                        {
                        var ckWord = this.getPageNthWord(0,i);
                        var l = ckWord.count;
                        for (l = 0; l<ckWord; l++)
                        if ( ckWord == "ISSUED" && l == 2)
                        {
                        var coord = this.mouseX;

                        var annot = this.addAnnot({
                        page: 0,
                        type: "Stamp",
                        name: "AppStamp",
                        rect: [coord+1000, 2300, 30, 2820],
                        rotate: 90,
                        AP: "#C94cHAFFa42U1gTH5Tug5C" }); 
                        break;
                        /* this.flattenPages(); */
                        }
                        }
                        }
                        Thanks for your continued assistance.

                        • 9. Re: Find the first occurance of a word
                          try67 MVP & Adobe Community Professional

                          A string has no property called "count". The counter needs to be a separate variable.

                          1 person found this helpful
                          • 10. Re: Find the first occurance of a word
                            JoFrmOZ Level 1

                            I have revised script to place count as a separate variable.

                             

                            The script is not placing the stamp on the drawing and a "1" is reporting in the console after script is executed.

                             

                            Can you please help, there is still something I am not getting quite right with the counter?

                             

                            {
                            var numWords = this.getPageNumWords(0);
                            for (var i=0; i<numWords; i++)
                            {
                            var ckWord = this.getPageNthWord(0,i);
                            var count = 0;
                            ++count;
                            if ( ckWord == "ISSUED" && ckWord.count == 2)
                            {
                            var coord = this.mouseX;

                            var annot = this.addAnnot({
                            page: 0,
                            type: "Stamp",
                            name: "AppStamp",
                            rect: [coord+1000, 2300, 30, 2820],
                            rotate: 90,
                            AP: "#C94cHAFFa42U1gTH5Tug5C" }); 
                            break;
                            /* this.flattenPages(); */
                            }
                            }
                            }

                            • 11. Re: Find the first occurance of a word
                              try67 MVP & Adobe Community Professional

                              var numWords = this.getPageNumWords(0);

                              var count = 0;

                              for (var i=0; i<numWords; i++) {
                                  var ckWord = this.getPageNthWord(0,i);
                                  if (ckWord == "ISSUED") {
                                      count++;
                                      if (count == 2) {
                                          var coord = this.mouseX;

                               

                                          var annot = this.addAnnot({
                                          page: 0,
                                          type: "Stamp",
                                          name: "AppStamp",
                                          rect: [coord+1000, 2300, 30, 2820],
                                          rotate: 90,
                                          AP: "#C94cHAFFa42U1gTH5Tug5C" });
                                         
                                          break;
                                          /* this.flattenPages(); */
                                      }
                                  }
                              }

                              • 12. Re: Find the first occurance of a word
                                JoFrmOZ Level 1

                                Thank you very much for all of your help.

                                 

                                The script will now locate and count the word and place the stamp on the drawing.