3 Replies Latest reply on Apr 10, 2014 2:21 PM by RobertKyle

    Exploring OnOpen eventhandling.

    Dave Saunders Level 4

      A discussion in another topic has me looking into event-driven scripts. I started simple with this:

       

       

      #target indesign
      #targetengine 'dave'
      
      
      (function(){
                var myEventListener = app.eventListeners.add("afterOpen", testFunction, false);
      
      
                function testFunction() {
                          $.writeln("Count of open documents: " + app.documents.length);
                }
      }())
      
      

       

      I saved this in the InDesign CC/Scripts/startup scripts folder under the name OpenHandler.jsx and restarted InDesign. Then I created a new empty document and in the ESTK JavaScript Console, I see:

       

       

      Count of open documents: 1
      

       

      So, so far so good. But my suspicion is that at this point in the document opening process, there is not yet a window to work with. Easy enough to check ...

       

       

      #target indesign
      #targetengine 'dave'
      
      
      (function(){
                var myEventListener = app.eventListeners.add("afterOpen", testFunction, false);
      
      
                function testFunction() {
                          $.writeln("Count of open documents: " + app.documents.length);
                          $.writeln("Count of windows: " + app.windows.length);
                }
      }())
      
      

       

      And I'm wrong, there is a window:

       

      Count of open documents: 1
      Count of windows: 1
      

       

      And I got this same result with and without the Application Frame active (relevant only on Mac).

       

      So, the question is, can I force the window to be a particular size? I'll reply to that in the comments.

        • 1. Re: Exploring OnOpen eventhandling.
          Dave Saunders Level 4

          What do you know, this works:

           

          #target indesign
          #targetengine 'dave'
          
          
          (function(){
                    var myEventListener = app.eventListeners.add("afterOpen", testFunction, false);
          
          
                    function testFunction() {
                              app.layoutWindows[0].bounds = [50, 32, 1143, 944];
                    }
          }())
          
          

           

          Dave

          • 2. Re: Exploring OnOpen eventhandling.
            DBLjan Level 2

            Hello Dave,

             

            This. Is. Awesome!

             

            Really, i was using your script for this every day a couple of times, i keybinded it and was quite happy. Then at work with not much work at hand I revisited an eventhandling-thingy (applying metadata before saving), getting to know the different eventhandlers, and tried to clip your window-resizing-script together with afterOpen – but i'm not a (JS-)programmer, fiddled around, got frustrated.

             

            It was time to ask the comm: Bumping your 2009-thread (never thought you would help me out there) with the idea that i had to bind this on opening documents – and bam! – you nailed it!

             

            It got my bounds out of the .txt of your (old) script, and now my workflow purrs like a kitten…meaning its so pleasing not to keypress for every new open doc.

             

            Chapeu!

            …and when you decide to merge your fresh eventhandling-skills together with the file-reading-bounds-checking-script, let me know. But as it is, its perfect, because I only hab one bound-setting for my windows.

            • 3. Re: Exploring OnOpen eventhandling.
              RobertKyle Level 2

              This probably falls under the heading of if-ain't broke-who-cares, but I learned the hard way that the afterOpen event can fire twice. If the user double-clicks on a document when InDesign isn't running, the application launches, which triggers an afterOpen event. Then the document opens, which triggers another. So my afterOpen handlers look like this:

               

              function doThisWhenDocOpens(myEvent) {

                   if (myEvent.target.constructor.name == "LayoutWindow") {

                        // do your thing here

                   }

              }

               

              It's kind of educational to use $.writeln() to print the target.constructor.name to the ESTK console, for this an other event listeners.

              1 person found this helpful