4 Replies Latest reply on Jun 6, 2011 9:28 PM by John Hawkinson

    [JS CS3] Problem with EventListener

    Tom Tomasko Level 1

      Hello,

       

      I am having a problem passing information from two functions to a third when those functions are invoked by an eventListener. Below is the code, very simplified.

       

      The first function will get the last word of each text frame at the moment the file is opened.

       

      In the second function, the last word of each text frame will be recorded at the moment of Save.

       

      The third function will compare the two arrays and get the page number where the last word has moved. This script is to tell the user if there is any text flow from one page to another while making minor changes.

       

      The problem is that the first two functions only return the function itself and not the values of the function.

       

      If I assign the first two functions to variables, the third function works correctly but the eventListener does not recognize the functions. I have to comment out the eventListeners and call the first two functions outside of them.

       

      Any advice to help me solve this would be helpful.

       

      Thanks,

       

      Tom

       

      #targetengine "session"

       

      main();
      function main(){
      var myEventListener1 = app.addEventListener("afterOpen",arrLastWordsOnOpen, false);
      var myEventListener2 = app.addEventListener("afterSave",arrLastWordsOnSave, false);
      var myEventListener3 = app.addEventListener("afterSave",compare, false);

       

      function arrLastWordsOnOpen(){
      var a = 5;
      alert(a);
      return a;
          }

       

      function arrLastWordsOnSave(){
      var b = 6;
      alert(b);
      return b;
          }

       

      function compare(){
          var c = arrLastWordsOnOpen+arrLastWordsOnSave;
          alert(c);
          }
      }

        • 1. Re: [JS CS3] Problem with EventListener
          Marc Autret Level 4

          Hi Tom,

           

          Your three functions are registered as event handlers, so they are intended to be automatically called when the corresponding events occur, so this is —generally— a wrong approach to call such functions by yourself from other points of your code. What you need is to store the results computed by arrLastWordsOnOpen and arrLastWordsOnSave, so that the third handler can use the values. Since you are using a persistent engine, you could simply declare a and b at the main() level. Then arrLastWordsOnOpen would update a, arrLastWordsOnSave would update b,and compare just needs to set c = a + b;

           

          If you don't want to 'pollute' the outer scope of the handler functions, you can also store the computed values as new properties of the functions themselves:

           

          #targetengine "session"
          
          main();
          function main()
          {
               var myEventListener1 = app.addEventListener("afterOpen",arrLastWordsOnOpen, false);
               var myEventListener2 = app.addEventListener("afterSave",arrLastWordsOnSave, false);
               var myEventListener3 = app.addEventListener("afterSave",compare, false);
          
               function arrLastWordsOnOpen()
               {
                    var a = 5;
                    alert(a);
                    arrLastWordsOnOpen.computedValue = a;
               }
          
               function arrLastWordsOnSave()
               {
                    var b = 6;
                    alert(b);
                    arrLastWordsOnSave.computedValue = b;
               }
          
               function compare()
               {
                    var c = arrLastWordsOnOpen.computedValue + arrLastWordsOnSave.computedValue;
                    alert(c);
               }
          }
          

           

          Hope that helps.

           

          @+

          Marc

          1 person found this helpful
          • 2. Re: [JS CS3] Problem with EventListener
            Peter Kahrel Adobe Community Professional & MVP

            Nice one, Marc.

             

            Peter

            • 3. Re: [JS CS3] Problem with EventListener
              Tom Tomasko Level 1

              Thanks Marc for your editing. It works fine, but I guess I did not make  clear that the results of the first two functions would be arrays, not numbers. The third function is to compare the two arrays and make a third array that includes any differences between the two.

               

              So when I try "computedValue" for an array, it does not work.

               

              Below is a better version (still simplified because the array of numbers will actually be arrays of Word objects). If I comment out the eventListeners and use the calls for the functions, the script works. But I need the eventListeners.

               

              Tom

               

              #targetengine "session"
              main();
              function main(){
              var myEventListener1 = app.addEventListener("afterOpen",arrLastWordsOnOpen, false);
              var myEventListener2 = app.addEventListener("afterSave",arrLastWordsOnSave, false);
              var myEventListener3 = app.addEventListener("afterSave",compare, false);

               

              //var c =arrLastWordsOnOpen();
              //var d =arrLastWordsOnSave();
              //compare(c,d);

               

              //****functions********
              function arrLastWordsOnOpen(){
                  var a = [1,2,3,4];
                  return a;
                  }

               

              function arrLastWordsOnSave(){
                  var b = [1,2,7,4];
                  return b;
                  }

               

              function compare(x,y){
                  var arrLastWordsMoved = [];
                  for(var i = 0; x.length>i;i++){
                      if(x[i] != y[i]){
                          arrLastWordsMoved.push(x[i]);
                          }
                      }
                  if(arrLastWordsMoved.length <1){
                      alert("So far there has been no text flowing from one page to another!");
                  }
                  else{alert(arrLastWordsMoved.join(","));}
                  }
              }

              • 4. Re: [JS CS3] Problem with EventListener
                John Hawkinson Level 5

                Tom:

                So when I try "computedValue" for an array, it does not work.

                You'll get much better help if you are precise and explicit in your questions. Where are you "trying a 'computedValue'? What is the exact code you are trying that does not work?

                 

                Anyhow, you should reread Marc' original reply, where he says:

                 

                Your three functions are registered as event handlers, so they are intended to be automatically called when the corresponding events occur, so this is —generally— a wrong approach to call such functions by yourself from other points of your code. What you need is to store the results computed by arrLastWordsOnOpen and arrLastWordsOnSave, so that the third handler can use the values.

                 

                To reiterate, you cannot return values from an event handler. The event handler is a function that will be called in the guts of InDesign and the return value will go nowhere.

                 

                You must preserve the values you wish to return in some other way. Such as how Marc suggested and demonstrated.