4 Replies Latest reply on Feb 8, 2011 8:30 AM by stephanwissner

    Error 30476 (Indesign CS4, Javascript, EventListener)?

    stephanwissner

      Dear all,

       

      I wrote a Javascript for InDesign CS4 (Mac), that doesn’t work and have absolutely no Idea, what could be the problem.

       

      #targetengine "session"

      app.eventListeners.everyItem().remove();

      app.documents[0].eventListeners.everyItem().remove();

       

      EventlistenerAfterSafeAs()

       

      function EventlistenerAfterSafeAs()

      {

        var myEventListener = app.documents[0].addEventListener("afterSaveAs",  MyAfterSaveHandller, false);

        }

       

       

      function MyAfterSaveHandller()

      {

      alert ("everything is fine now");

      }

       

       

       

      Now, everytime after I save a document, Indesign throws an Javascript Error 30476 – The requested action could not be completed because the object no longer exist."

      I would be very happy if someone could hepl!

      Thank you!

        • 1. Re: Error 30476 (Indesign CS4, Javascript, EventListener)?
          Olav Martin Kvern Level 3

          Hi stephanwissner,

           

          Hm. This script works fine for me in both CS4 and CS5. But I might not be following the same set of steps you're using to test it.

           

          Here's something to try: Add the event listener to the app, rather than to app.documents.item(0). Does it still fail?

           

          Thanks,

           

          Ole

          • 2. Re: Error 30476 (Indesign CS4, Javascript, EventListener)?
            stephanwissner Level 1

            Hi Ole,

             

            It’s strange, that it works for you.

            For me, it works,in CS4 but just as long as I put the Script in the Startup-Scripts-Folder. Otherwise it throws that error – even if I add the Listener to the app instead of the active Document.

            However – I did a small workaround: I put it in the Startup-Scripts-Folder. The Listener calls a function but only if a special textvariable in my document exists.

            So everything is fine now

             

            But thanks for you reply

            Stephan

            • 3. Re: Error 30476 (Indesign CS4, Javascript, EventListener)?
              Marc Autret Level 4

              Hi Stephan,

               

              I don't get the logic of your original code. As far as I understand the script attempts to attach an event listener to app.documents[0] whatever the context, so if no document is present when you run the script, the specifier points out to nothing. And that's really surprising that you got your code to work as a startup script: this should throw an error on the unresolved Document specifier...

               

              Anyway, each time you run or re-run the script when at least a document is present, app.documents[0] becomes valid and the event listener is attached to that Document instance, so the script temporarily seems to work. But that is not the expected behaviour, I suppose.

               

              Also, using app.eventListeners.everyItem().remove(); in a startup script sounds to me really dangerous because this disregards the possibility that another startup script may load its own event listeners!

               

              As Olav previously suggested you should attach the listener at the Application level. Here is a possible approach using a Startup Script:

               

              #targetengine "mySession"
              
              // Uncomment the following line if you *REALLY* need to kill *ALL* listeners:
              // app.eventListeners.everyItem().remove();
              
              
              // Declare the (persistent) event handler(s)
              // ---
              var AfterSaveAsHandler = AfterSaveAsHandler||function(ev)
              {
                   // The saved document is ev.target
                   alert( ev.target.name + " saved.\r\rAll is fine." );
              };
              
              
              // Attach the listener(s) once
              // ---
              var AttachOnce = AttachOnce||(function()
              {
                   app.addEventListener('afterSaveAs', AfterSaveAsHandler);
                   return true;
              })();
              

               

              @+

              Marc

              • 4. Re: Error 30476 (Indesign CS4, Javascript, EventListener)?
                stephanwissner Level 1

                Hi Marc,

                 

                thank you – it now works as it should.

                What seemed surprising to you (that it did work) was not the result of a startup-script!

                The problem I had was that I wanted to initialize the Listener as a User-Script (and that did’n work and brought me that error).

                I thought (because I’m really really new in Javascript), that the handler function MUST be called everytime after a "afterSaveAs"-action if I put it in the startup-scripts.

                Now, the handler in my startup-script looks, if a special textvariable in my document exists. If it does, the action (writing all placed images in a log file) takes place – if not, it does nothing.

                 

                Thanks to you and Olav!

                Stephan