Skip to main content
Geoff0409
Participant
December 17, 2016
Answered

Why the script has to be run twice?

  • December 17, 2016
  • 2 replies
  • 580 views

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

This topic has been closed for replies.
Correct answer Geoff0409

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!

2 replies

JR Boulay
Community Expert
Community Expert
December 17, 2016

Hi.

Just tested: it works fine.

(AcDC on Mac OS X)

Acrobate du PDF, InDesigner et Photoshopographe
Inspiring
December 17, 2016

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() 

        }); 

      } 

    } 

  } 

Geoff0409
Geoff0409Author
Participant
December 17, 2016

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!!

Geoff0409
Geoff0409AuthorCorrect answer
Participant
December 17, 2016

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!