7 Replies Latest reply on Feb 21, 2017 4:58 AM by jakec88782761

    'before' EventListeners are firing AFTER the event

    brandonl51082637

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

        }

       

      }

        • 1. Re: 'before' EventListeners are firing AFTER the event
          cbishop01 Level 2
          #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

          • 2. Re: 'before' EventListeners are firing AFTER the event
            cbishop01 Level 2

            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.

            • 3. Re: 'before' EventListeners are firing AFTER the event
              cbishop01 Level 2

              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.

              • 4. Re: 'before' EventListeners are firing AFTER the event
                brandonl51082637 Level 1

                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.

                • 5. Re: 'before' EventListeners are firing AFTER the event
                  brandonl51082637 Level 1
                  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?

                  • 6. Re: 'before' EventListeners are firing AFTER the event
                    jakec88782761 Level 1

                    you can add:

                    $.sleep(number of milliseconds)

                     

                    This will make your script wait a bit

                    • 7. Re: 'before' EventListeners are firing AFTER the event
                      jakec88782761 Level 1

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