3 Replies Latest reply on Feb 16, 2016 8:34 PM by Laubender

    Clarification needed for Event Listener

    tpk1982 Level 4

      Hi All,


      I am trying to use event listener to import character styles from my template. I placed this script in startup script. My problem is the confirmation message come twice everytime. Can you please anyone solve this?


      #target indesign  
      #targetengine "session"  
        myEventListener = app.eventListeners.item("loadstyles");  
        if (!myEventListener.isValid) {  
        myEventListener = app.addEventListener( "afterOpen", doJob );  
        myEventListener.name = "loadstyles";  
      function doJob() {  
        if (!app.documents.length) return;  
        var myconfirm=confirm("It is Live job?");
       if( myconfirm){
      var theDocument = app.activeDocument;  
      var sourceFile = new File(".../dummt text.indd");
      //import paragraph styles  
      var paraImport = ImportFormat.CHARACTER_STYLES_FORMAT;    
      //This will import without overwrite existing styles.  
      var clashPolicy = GlobalClashResolutionStrategy.DO_NOT_LOAD_THE_STYLE;  
      theDocument.importStyles(paraImport, sourceFile, clashPolicy);  


      Thanks in advance.


        • 1. Re: Clarification needed for Event Listener
          Laubender Adobe Community Professional & MVP

          Hi Karthik,

          that's no problem or bug, that's the way it works.

          One time the event is fired after opening the document, the second time when the window for the document is ready.

          You could run a counter, if you want to react only one time. Or check the property/value pairs of both events happening.

          Hint: they are a bit different.


          More on this here on the German InDesign Scripting Forum at www.hilfdirselbst.ch:


          Startup-Script: Wie den Fehlende-Schriften-Dialog unterdrücken? - Adobe InDesign Skriptwerkstatt - Seite 2 HilfDirSelbs…


          Also see the other posts in this thread.



          • 2. Re: Clarification needed for Event Listener
            tpk1982 Level 4

            Hi Uwe,


            Thanks for your kind reply.


            I have another thought. Is it possible to run a function only once in script? Means without using startup and eventlistner, i can call the function (loading character style) once in the main script.




            • 3. Re: Clarification needed for Event Listener
              Laubender Adobe Community Professional & MVP

              Don't know exactly what you mean with "running once".


              E.g.: The eventListener could be removed after the function was invoked once. ( But then what would be the purpose of the whole script? )

              You only need to add that to the bottom of your function doJob():


              app.removeEventListener( "afterOpen", doJob );


              Or it could be dismissed for one special case and used again when this case is gone the next time the event fires and the listener listens:


              // Pseudo code, do not execute as is:
              function doJob(myEvent)
                   if(/*pseudo code*somethingIsTrue){return};
                   // Here comes the rest of my super cool function functionality:
                   // ...

              Also check for method preventDefault() of the Event object in the DOM documentation for example here:

              Adobe InDesign CS6 (8.0) Object Model JS: Event


              // Pseudo Code, do not execute as is:
              function doJob(myEvent)
              if (/*pseudo code*/somethingIsTrue){ myEvent.preventDefault() };


              To make more out of doJob() you could implement the event object as well and check for property/value pairs you'd like to use in the function.
              As you might have seen in the thread about "beforeOpen" and "afterOpen" events on www.hilfdirselbst.ch .


              function doJob(myEvent)
                   for(x in myEvent)
                        $.writeln(x +" : "+myEvent[x].toString() );


              Have fun while experimenting…