3 Replies Latest reply on Dec 20, 2016 12:00 PM by Loic.Aigon

    Event Handling/Creating

    Roy Marshall Level 1

      Hey all

      I am trying to find some documentation around Event Handlers/Listeners etc, but have not found anything I can interpret fully for my purpose.

      My terminology may be completely wrong below, but please humour me!

      I am wanting to add a way at the end of a current script, to trigger off an editText area in an already scriptUI panel to update itself with the contents of a text file that was changed during the running of the first script. Lets call it a log file for augments sake.

      I don't know where event Handlers, triggers, listeners etc need to be placed if this is even possible.

       

      Thanks in advance for any help on this matter.

       

      Roy

        • 1. Re: Event Handling/Creating
          Loic.Aigon Adobe Community Professional

          Hi Roy,

           

          Mainly issue of yours is that ScriptUI has no timer internal function (that I know of at least). So you can only rely on InDesign events and I guess the perfect candidate for this are idleEvents.

           

          #targetengine "readLog"
          #include "EventHandler.jsinc"
          
          
          
          
          function main(){
          
            var ts, onIdleEventListener, 
            onIdleEventHandler = function (myIdleEvent){
          
            var log  = File ( Folder.desktop +"/log.txt" );
          
          
          
            if (!log.exists ||  ) {
            EventManager.dispatchEvent ( "LOG_UPDATE", {log:"No log yet"} );
            return;
            }
          
            if ( ts && log.modified==ts ) {
            return;
            }
          
            ts = log.modified;
            log.encoding ="UTF-8";
            log.open('r');
            var c =log.read();
            log.close();
            EventManager.dispatchEvent ( "LOG_UPDATE", {log:c} );
            };
          
            var myIdleTask = app.idleTasks.item("logger");
            if ( !myIdleTask.isValid ) {
            myIdleTask = app.idleTasks.add({name:"logger", sleep:100});
            myIdleTask.addEventListener(IdleEvent.ON_IDLE, onIdleEventHandler, false);
            }
          
          
          
            ui.show();
            EventManager.dispatchEvent ( "LOG_UPDATE", {log:"No log yet"} );
          }
          
          
          var ui = (function(){
            var w = new Window('palette'),
            bloc = w.add('edittext', undefined, '', {multiline:true});
          
            w.preferredSize = [300, 200];
            w.alignChildren = ["fill","fill"];
          
            EventManager.addEventListener ( "LOG_UPDATE", function(data) {
            bloc.text = data.log;
            });
          
            return w;
          })();
          
          
          main();
          
          
          
          
          
          
          

           

          Download full script here.

           

          Capture d’écran 2016-12-20 à 19.40.47.png

           

          Capture d’écran 2016-12-20 à 19.40.26.png

          HTH

           

          Loic

          Ozalto | Productivity Oriented - Loïc Aigon

          1 person found this helpful
          • 2. Re: Event Handling/Creating
            Roy Marshall Level 1

            Hey Loic

             

            Thanks for the comprehensive response.

            On initial execution, this does exactly what I was looking for.

            I will dissect this to get a full understanding of the events, as they are still 'magic' for me at the moment, but I know I can use the power if I understand it.

             

            Thanks again

             

            Roy

            • 3. Re: Event Handling/Creating
              Loic.Aigon Adobe Community Professional

              Basically the idleEvent will be fired every time InDesign will remain idle for 100ms thus executing the event handler. They will be fired more often than some afterOpen event because the latter can be fired only once in a while.

               

              Those events and the app ones are well documented in the scripting guides.

               

              Loic