3 Replies Latest reply on Jul 22, 2011 12:12 PM by Bob Stucky

    listening for events (i.e. Document.AFTER_NEW) doesn't work

    JoseAjáAjá Level 1

      Hi guys. Me again.




      • Scripting from Flash Builder 4 with Creative Suite SDK (free, no Extension Builder)
      • Languague: ActionScript
      • InDesign CS5.5


      • I'm posting hre, and not in the CS_SDK forum, because I feel this is more related to InDesign DOM than to CS-SDK. I you think otherwise, please tell me so.



      Description of problem


      I'm having a hard time while trying to listen for some events.


      I'm writing a basic extension that is suppossed to:

      1. Create a new document
      2. Import styles, formats etc. from other document after it has been created.


      I think then, that I have to create/attach an event listener that waits for the (I guess) Document.AFTER_NEW event.


      I try to do so, by using the following code (I'm trying to guess if any of the event listeners, either for Document.AFTER_NEW or for Document.AFTER_OPEN serves the purpose):


      private var myCSXS               :CSXSInterface     = CSXSInterface.getInstance();
      private var newDocumentTemplate   :Document;
      private function createBook():void
                          newDocumentTemplate     = inDesignApp.documents.add();
      private function afterNewDocument_Hdl(...



      But neither of the four possibilities seems to work (i.e. two using the CSXSInterface, and two adding the listener to the newDocument).


      So, the question is/are...

      I'm having a really hard time trying to understand how to handle the events when writing an extension for InDesign.


      1. Do I have to add the eventListener to the CSXSInterface.getInstance() always or just sometimes?
      2. How do I listen for events that are dispatched from a DOM element?: Say, a document, or a textFrame, for instance.
      3. Do you know where in the Documentation is that explained?


      Thank you all !

        • 1. Re: listening for events (i.e. Document.AFTER_NEW) doesn't work
          Bob Stucky Adobe Employee

          My first recommendation is to upgrade to Extension Builder. Extension Builder comes with samples (one is an event watcher utility for PS, ID and AI) and some extra stuff to make this much easier.


          That said, the first step is to read this cookbook entry: http://cookbooks.adobe.com/post_Communicating_between_JavaScript_and_the_Creative-17383.ht ml


          Once you understand that -


          You will need to create a jsx. The jsx will contain at least 2 functions, 1 to register the event, and 1 to call back to ActionScript when the event fires.


          In ActionScript, you do what's in the cookbook for initializing your script, then call it to register for events. When the event fires, your script calls back to ActionScript.


          I can cobble together a sample; however, I can't do it for you today.



          1 person found this helpful
          • 2. Re: listening for events (i.e. Document.AFTER_NEW) doesn't work
            JoseAjáAjá Level 1



            I don't know if I should smother you with kisses, or start crying because of the most cumbersome way to add an event with Creative Suite SDK...


            Thank you very much for your answer, though.


            How I'm solving the issue

            Yes, I followed the instructions in the link. I wrote a jsx script, with two functions:




            var asInterface = {}; 
            function listenForEvent(wrapper)
                asInterface = wrapper; 
                var myEventListener = app.addEventListener(
            function afterNewHandler(myEvent){
                //alert("This event is the " + myEvent.eventType + " event.");
                //Now, callback from JavaScript to control ActionScript handler:



            ...And I called it from ActionScript:




            [Embed (source= "jsx/AfterNewDocument.jsx" , mimeType= "application/octet-stream" )] 
            private static var jsxClassAfterNewDocument     :Class;
            //Gets a pointer to the scripting engine for this extension:
            private var  jsxInterfaceAfterNewDocument     :HostObject     = HostObject.getRoot(HostObject.extensions[0]); 
            private function buttonAction():void
                 newDocumentTemplate     = inDesignApp.documents.add();
            public function afterNewDocument_Hdl():void
                 //perform tasks...



            Albeit cumbersome, it does the trick.



            Many questions remain, though

            1- I can, actually, attach the following event listener:



            CSXSInterface.getInstance().addEventListener("documentAfterActivate", afterActivateDocument_Hdl);



               And it works!!!!.

               Why, in this case, I didn't need to write a script in jsx?


            2- What's the purpose, then, of having the CSXSInterface.getInstance.addEventListener() method?


            3- Why can we see the Document.addEventListenerDocument() method (and many similar ones) if we can't attach an event listener directly from ActionScript?



            Thank you Bob, and all

            • 3. Re: listening for events (i.e. Document.AFTER_NEW) doesn't work
              Bob Stucky Adobe Employee

              Where to start...


              CSXSEvents are a part of the extension infrastructure built into many suite applications (AI, ID, PS are examples). The events are more generic in nature. I guess in this case you found a CSXSEvent that will do what you need!


              If you want to use ID's events, though, you'll need to call through extendscript. Again, with Extension Builder, you get some stuff to make this far less cumbersome.


              Your jsx will work. I would suggest passing the AS function to your jsx, and including that AS function into an anonymous function. Your anonymous function is handles the event, and passes the AS function back to the AS side for execution.


              That way you would have a generic mechanism that would handle any ID event. At least then, you've written 1 thing that'll be useful everywhere.