3 Replies Latest reply on Dec 24, 2008 3:41 PM by Harbs.

    AddEventListener afterOpen to Document

      The CS3 scripting guide specifically mentions that you can use the "afterOpen" event on either an Application or Document object. I seem unable to bind to the afterOpen event on a document, however. What I am trying to do is detect certain files being opened, read metadata from them, close that document, and open a new document based on the metadata of the original.

      It sounds like it should be easy, but the "afterOpen" event is a little misleading. Although it contains information about the document, the application has not yet finished opening the document - meaning it does not yet have a window or has not yet become the activeDocument. I have a reference to the document being opened, but addEventListener has now apparent effect and I cannot call doc.close() without a javascript error being thrown.

      The code is fairly straightforward so far:

      function doAfterOpen(event) {
      var doc = event.parent;
      //read from the first story
      var story = doc.stories.firstItem();
      if(story) {
      var realFilePath = story.extractLabel("realPath");
      //I now have the realpath and need to close this temporary document
      doc.close(); <--- Throws a javascript error: 1 (I'm guessing cause it's not yet open which belies the afterOpen name)

      -- OR --

      doc.addEventListener("afterOpen",function(event) { alert("docOpen"); })) <---- it never fires
      }

      }

      Anyone have any idea if there is actually a way to bind to a REAL afterOpen event, meaning that the document as in fact been opened???
        • 1. Re: AddEventListener afterOpen to Document
          Kasyan Servetsky Level 5
          Hi mattp,

          I had a similar problem and successfully solved it by using APID Toolkit – thanks Harbs for the tip.
          In short, use APID Toolkit’s docLoaded event instead of InDesign’s afterOpen event.
          If you, or anybody else is interested, I can write about how I did it in detail.

          Kasyan
          • 2. Re: AddEventListener afterOpen to Document
            Level 1
            I just jumped on this bandwagon. When I realized that afterOpen fires before the first window appears, I tried opening one in my event handler:
            //DESCRIPTION: Start-up Script
            

            #target indesign
            #targetengine 'onOpen'

            (function() {
              app.eventListeners.add("afterOpen", afterOpen);

              function afterOpen(myEvent) {
                myEvent.target.windows.add();
              }
            }())
            This worked. The window was drawn. But when I tried to close the window, InDesign CS4 crashed. To be thorough, I shall now go through the process again to make sure it is repeatable ... Yes it is. I just tried again with a different document -- happens it was one that had to be converted from CS3. Same thing happened. One window only which on closing caused the crash.

            Then, restarting CS4 caused the document to be recovered with two windows.

            Looks like APID is the way to go if you want to do anything useful with a document when it opens.

            Dave
            • 3. Re: AddEventListener afterOpen to Document
              Harbs. Level 6
              Dave_Saunders@adobeforums.com wrote:
              > I tried opening one in my event handler:
              >
              Yeah. If I remember correctly I had tried that as well... ;)
              --
              Harbs
              http://www.in-tools.com