1 Reply Latest reply on Jul 13, 2016 5:40 AM by Laubender

    Strange behaviour while adding event handlers

    Manan Joshi Level 4

      Hello All,


      I am trying the following code snippet and am a bit confused by the results. Can someone help me understand the output i see


      var a = app.menuActions.itemByName("$ID/Save").addEventListener('beforeInvoke', sv);

      function sv()


          alert("Save Done")




      The alert gives 2 as the result, but i have added only a single event handler. If i run the same on an app event it works reports correct result.


      The problem that i am trying to solve here is, i want to make sure i don't add multiple instances of my event handler.


      Also the following code returns undefined



      But if i add an event on app for selectionchanged the handler name is set. For which events InDesign sets the handler name and for which it does not?


      Please spare some thoughts on these scenarios, as i mentioned the goal here is to prevent multiple instances of my event handler.




        • 1. Re: Strange behaviour while adding event handlers
          Laubender Adobe Community Professional & MVP

          Maybe the event your listener is listening to fires more than one time.

          You have to find out what the actual target is when it is fired the first and the second time.


          You could use a counter and test what the event that is returned is build of in details:


          #targetengine lookingForBeforeInvoke
          var a = app.menuActions.itemByName("$ID/Save").addEventListener('beforeInvoke', sv);
          var counter = 0;
          function sv(event)
              for(x in event)
                  $.writeln( counter+"\t"+x+"\t"+event[x].toString() );


          Maybe you can make out some differences between the two occurences. Or check, if variable counter exists before adding the listener.

          You can then differentiate and take action when it is appropriate.