4 Replies Latest reply on Dec 17, 2016 7:43 AM by Geoff0409

    Why the script has to be run twice?

    Geoff0409

      Hi,

      I am writing a script to spell-check PDF. With reference to the official API document here, the script is copied as a js file to Acrobat javascript folder. I also add a line to insert a menu item to run the script:

      app.addMenuItem({cName: "&Spell Check All Pages", cParent: "Edit", nPos: 0, cExec: "testck();", cEnable: "event.rc = (event.target != null)"});
      
      function testck() {
        var ckWord, numWords;
        for (var i = 0; i < this.numPages; i++ ) {
          numWords = this.getPageNumWords(i);
          for (var j = 0; j < numWords; j++) {
            ckWord = spell.checkWord(this.getPageNthWord(i, j))
            if ( ckWord != null ) {
              this.addAnnot({
                page: i,
                type: "Squiggly",
                quads: this.getPageNthWordQuads(i, j),
                author: "A. C. Acrobat",
                contents: ckWord.toString()
              });
            }
          }
        }
      }
      

       

      However, the strange thing I encountered is that when I click the item in the menu in first time, it doesn't complete. When I click the second time, it runs completely and show the result. Afterwards, it run completely by just one click. If I close the Acrobat and restarts, it requires a double execution. Would you please help indicate the problem? Thank you!!

       

      Geoff

        • 1. Re: Why the script has to be run twice?
          George_Johnson MVP & Adobe Community Professional

          Try this:

           

          app.addMenuItem({cName: "&Spell Check All Pages", cParent: "Edit", nPos: 0, cExec: "testck(this);", cEnable: "event.rc = (event.target != null)"}); 

           

          function testck(doc) { 

            var ckWord, numWords; 

            for (var i = 0; i < doc.numPages; i++ ) { 

              numWords = doc.getPageNumWords(i); 

              for (var j = 0; j < numWords; j++) { 

                ckWord = spell.checkWord(doc.getPageNthWord(i, j)) 

                if ( ckWord != null ) { 

                  doc.addAnnot({ 

                    page: i, 

                    type: "Squiggly", 

                    quads: doc.getPageNthWordQuads(i, j), 

                    author: "A. C. Acrobat", 

                    contents: ckWord.toString() 

                  }); 

                } 

              } 

            } 

          2 people found this helpful
          • 2. Re: Why the script has to be run twice?
            JR_Boulay Adobe Community Professional

            Hi.

             

            Just tested: it works fine.

            (AcDC on Mac OS X)

            1 person found this helpful
            • 3. Re: Why the script has to be run twice?
              Geoff0409 Level 1

              Thank you. However it still requires double execution. I try to revise as follows:

               

              app.addMenuItem({cName: "&Spell Check All Pages", cParent: "Edit", nPos: 0, cExec: "testck(this)"});

               

               

              function testck(doc) {

                var ckWord, numWords;

                for (var i = 0; i < doc.numPages; i++ ) {

                  numWords = doc.getPageNumWords(i);

                  for (var j = 0; j < numWords; j++) {

                    app.alert(j+1 + "/" + numWords);

                    ckWord = spell.checkWord(doc.getPageNthWord(i, j));

                    app.alert("passsed");

                    if ( ckWord != null ) {

                      this.addAnnot({

                        page: i,

                        type: "Squiggly",

                        quads: doc.getPageNthWordQuads(i, j),

                        author: "A. C. Acrobat",

                        contents: ckWord.toString()

                      });

                    }

                  }

                }

                app.alert("done")

              }

               

              When I click the menu item (open the document, and immediately run the item without other commands. If I click other item, it runs completely), it just show 1/14 without alerting "passed", then it stops without any notification of error. Then I click a second time, it shows 1/14-14/14 completely with "passed".

               

              Please help, thank you!!

              • 4. Re: Why the script has to be run twice?
                Geoff0409 Level 1

                I just found a thread with similar strange behaviour here.

                 

                Therefore if I add this line,

                if (!spell.available) {

                app.alert("message");

                return;

                 

                the menu item behaves as expected. Thanks everyone for investigation and testing!