• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

'before' EventListeners are firing AFTER the event

New Here ,
Nov 10, 2016 Nov 10, 2016

Copy link to clipboard

Copied

Hey All!

I'm new to JavaScript, so please bare with me.

I've written a simple script that triggers the Spell Checking action before certain events occur (I'm aiming to do this before Save, SaveAs, and Export).

The problem that I'm running into is that the invoke action for Spell Checking occurs AFTER the event is finished running. The issue this causes is that it allows for an unchecked document to be saved or exported. My intention is to force the user to check spelling before they can successfully save or export a doc.

I'm sure I'm missing something in my script, but I can't figure out what it is. Any help would be appreciated!

My Script so far:

#target indesign

#targetengine "session"

main();

function main(){

     alert('Hello 8');

     var myBeforeSaveEvList = app.addEventListener("beforeSave", checkSpelling, false);

     var myBeforeSaveAsEvList = app.addEventListener("beforeSaveAs", checkSpelling, false);

     var myBeforeExportEvList = app.addEventListener("beforeExport", checkSpelling, false);


     function checkSpelling(myEvent){

            alert('Check Spelling...');

            app.menuActions.item("Check Spelling...").invoke();

  }

}

TOPICS
Scripting

Views

1.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Nov 10, 2016 Nov 10, 2016

Copy link to clipboard

Copied

#targetengine "session"

main();

function main(){

     alert('Hello 8');

app.addEventListener( "beforeExport", chSpelling );

app.addEventListener( "beforeSave", chSpelling );

app.addEventListener( "beforeSaveAs", chSpelling );

    

     function chSpelling(myEvent){

alert('Check Spelling...');

app.menuActions.item("Check Spelling...").invoke();

  }

}

Try this

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Nov 10, 2016 Nov 10, 2016

Copy link to clipboard

Copied

I just changed it from checkSpelling to chSpelling because i didnt want to type the other 3 characters.  You can call that whatever you want.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 10, 2016 Nov 10, 2016

Copy link to clipboard

Copied

Hey cbishop!

Thanks for taking the time to help.

I'm still having the same results with your updated version of the script.

For example, I have the text 'Baad speling iz bad' in the indd document and choose export. The export dialog appears, asks me to name the exported document, and after clicking save, the Check Spelling... script runs. The exported document has already saved at this point and is sitting in the output folder with the spelling errors. After fixing the typos to read 'Bad spelling is bad', the original pdf still remains.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Nov 10, 2016 Nov 10, 2016

Copy link to clipboard

Copied

I just had it working.  YOu'll still get the export dialog before the script shows up but before the actual file is written it corrects it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 10, 2016 Nov 10, 2016

Copy link to clipboard

Copied

main(); 

 

function main(){ 

 

     alert('Hello 12'); 

 

//app.addEventListener( "beforeExport", chSpelling ); 

//app.addEventListener( "beforeSave", chSpelling ); 

//app.addEventListener( "beforeSaveAs", chSpelling ); 

  

  var

  //beforeSave = app.menuActions.itemByID ( 260 ).addEventListener ( "beforeInvoke", checkSpelling );

  //beforeSaveAs = app.menuActions.itemByID ( 261 ).addEventListener ( "beforeInvoke", checkSpelling );

  //beforeSaveAsCopy = app.menuActions.itemByID ( 262 ).addEventListener ( "beforeInvoke", checkSpelling );

  //beforeExport = app.menuActions.itemByID ( 113411 ).addEventListener ( "beforeInvoke", checkSpelling );

  beforeSave = app.menuActions.itemByName ( "$ID/Save" ).addEventListener ( "beforeInvoke", checkSpelling );

  beforeSaveAs = app.menuActions.itemByName ( "$ID/Save As..." ).addEventListener ( "beforeInvoke", checkSpelling );

  beforeSaveACopy = app.menuActions.itemByName ( "$ID/Save a Copy..." ).addEventListener ( "beforeInvoke", checkSpelling );

  beforeExport = app.menuActions.itemByName ( "$ID/Export..." ).addEventListener ( "beforeInvoke", checkSpelling );

  

     function checkSpelling(myEvent){ 

  alert('Check Spelling...');

  app.menuActions.item("Check Spelling...").invoke(); 

  }

 

 

  } 

I came up with a solution - see above.

The beforeInvoke listener tells the function to run before the event occurs.

My solution, however, put me into another problem. The function runs properly before the event and triggers the spell checking window to pop, but afterwards it continues to open the save/saveas/export dialog immediately afterwards.

Anyone have any ideas on how to make the checkspelling function wait until the actual spell checking is complete before popping the event dialog?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 21, 2017 Feb 21, 2017

Copy link to clipboard

Copied

you can add:

$.sleep(number of milliseconds)

This will make your script wait a bit

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 21, 2017 Feb 21, 2017

Copy link to clipboard

Copied

LATEST

I used this idea and it worked really well, thanks brandonl51082637​

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines