0 Replies Latest reply on Apr 17, 2012 1:28 AM by benjaminmueller_burda

    CSSDK IdleTask Eventhandler

    benjaminmueller_burda Level 1

      Hi,

       

      I just wanted to share a little bit of coding madness since I did not find any answers anywhere and had to figure it out myself.

       

      We developed an application that automatically fills an InCopy or InDesign document (article/story) with data hosted in a remote database.  All the routines are coded in Flex/CSSDK/JSX.  As soon all the information is "imported" into the document we  automatically check the document/article into our editorial system "vjoon k4".  The problem is : Everything happends way too fast for InCopy/InDesign.  We try to check in the document before InCopy/InDesign has finished with its drawing routines, which resulted in a crash approx 50% of the time. 

       

      We needed to know when InCopy/InDesign had finished its drawing routines.  So we had a look at the "IdleTaskEventHandler".  Unfortunatly it was not possible (for me) to code it directly in Flex/CSSDK since the paramters ("handleParam") are not defined in any API description. The second problem was, since a handler only receives the event as parameter, we had no means of passing any arguments to the handler method.   So we had to do it the hard and dirty way : Adobe ExtendScript.

       

      What we did :

       

      When we are done with filling the data in the document, we fork into Extendscript with "theObject" as parameter.

       

      _extendScriptInterface.createCheckinIdleHandler(theObject);

       

      In the jsx File we have the following functions :

       

      // GLOBAL VARIABLES

      var myIdleTask;

      var metaDataObjectContainer;

       

      function createCheckinIdleHandler(theObject) {

         

                       // CREATE NEW IDLE TASK

              myIdleTask = app.idleTasks.add({name:"myIdleTask", sleep:1000});

             

              // PASS PARAMETERS TO GLOBAL VARIABLE

              metaDataObjectContainer = new Object();

              metaDataObjectContainer = theObject;


              // CREATE NEW EVENT LISTENER

              var onIdleEventListener = myIdleTask.addEventListener("onIdle", onIdleEventHandler);

          }

       

      and

       

      function onIdleEventHandler(myIdleEvent) {

       

           // REMOVE EVENT LISTENER SO METHOD GETS ONLY CALLED ONCE!

           myIdleTask.removeEventListener(IdleEvent.ON_IDLE, onIdleEventHandler);

       

           // RETRIEVE PARAMETER FORM GLOBAL VARIABLE

           parameter = metaDataObjectContainer["parameter"];

       

           // DO THE REST

           call_some_function(parameter);

      }

       

      with this little piece of code, the "onIdleEventHandler" gets called when InCopy is not drawing anymore, so we know the document/article is ready to get checkedin/saved etc.

       

      If you have any ideas how to improve the code, please do so.  I would appreciate it, since I am not really happy to do it in ExtendScript.

       

      Regards

       

      Benjamin Müller