7 Replies Latest reply on Aug 11, 2008 12:23 PM by (scriptworx)

    eventListeners

      Hi all,

      I have been testing today with Eventlisteners in CS3.
      Can't figure out the logic of it. I have the code below:

      #targetengine "session"

      main();

      function main(){
      var myApplicationEventListener = app.eventListeners.add("afterOpen", myEventInfo, false);
      }

      function myEventInfo(myEvent){
      alert(myEvent.currentTarget.documents.length);
      app.activeDocument.textFrames.add();
      }

      The thing is that the alert says that there is 1 document open,
      but when I want to do something with this doc I get an error message
      that there is no open document.

      Can someone help with this?

      Thanks Michel
        • 1. Re: eventListeners
          Martin Fischer Level 2
          Michel,

          at this point of time the document which you have opened isn't active, yet.

          Try app.documents[0] (or better app.documents[-1]) instead of app.activeDocument.

          Martin
          • 2. Re: eventListeners
            Level 1
            Hi Martin,

            Thanks for your help.

            app.documents[-1].textFrames.add();

            is working, but

            app.documents[-1].close();

            is not.

            Any idea why?

            Michel
            • 3. Re: eventListeners
              Loic.Aigon Adobe Community Professional
              What about mixing both approaches.
              If you need to refer to app.documents[-1] first time to active it.
              Maybe in a second time app.activeDocument is just enough.
              Have a try and tell us.
              Loic
              • 4. Re: eventListeners
                CTC Imaging Level 1
                I recommend reading another post about Eventlistener that I was involved in ( Alexandre VERCAMMEN, "(CS3) Does anyone know how to place request for..." #, 7 May 2008 12:42 pm). Regarding the close statement, with the help of Olev, we went the option of menu.actions. Please note the limitations (which a + in my case) of closing the doc. by closing the window.

                At Startup I run the following script. FYI: A lot of my scripts are triggered Manualy or by Menu Actions, therefore a few GLOBAL vars. Also it adds flexibility when installed for different machines.

                The Function EventOpen DOES NOT WORK when associated to app.eventListener, it crashes InDesign when closing the document! But it does work when triggered manually or by menu.

                **************************************************************
                #target indesign
                #targetengine "session"
                app.scriptPreferences.version = 5.0;

                //*** Initialize GLOBAL Variables
                app.scriptArgs.clear();
                app.scriptArgs.set("Event_Path", myScriptPath); //***Path to InEventScript Plug-In
                app.scriptArgs.set("Event_Open", myScriptPath + "/PrePressOPEN.jsx");
                app.scriptArgs.set("Event_Close", myScriptPath + "/PrePressCLOSE.jsx");
                app.scriptArgs.set("Event_Copy", myScriptPath + "/PrePressCOPY.jsx");
                app.scriptArgs.set("Event_Clean", myScriptPath + "/PrePressCLEAN.jsx");
                app.scriptArgs.set("Event_Print", myScriptPath + "/PrePressPRINT.jsx");
                app.scriptArgs.set("Event_App", myScriptPath + "/PrePressAPPPreferences.jsx");
                app.scriptArgs.set("Event_Doc", myScriptPath + "/PrePressDOCPreferences.jsx");

                //******************** ACTIONS ********************
                app.menuActions.item("Open...").addEventListener("afterInvoke",EventOpen, false);
                app.menuActions.item("Close").addEventListener("beforeInvoke",EventClose, false);
                app.menuActions.item("Copy").addEventListener("afterInvoke",EventCopy, false);
                app.menuActions.item("Paste").addEventListener("afterInvoke",EventClean, false);
                app.menuActions.item("Print...").addEventListener("beforeInvoke",EventPrint, false);

                //****************** FUNCTIONS Definitions ********************

                function EventOpen (itsEvent)
                {
                var myExeSrcFile = new File (app.scriptArgs.get("Event_Open"));
                if (myExeSrcFile.exists)
                {
                myExeSrcFile.open ('r:(read)');
                app.doScript(myExeSrcFile, ScriptLanguage.javascript);
                myExeSrcFile.close();
                }
                else
                alert ("Error! Missing File:\n\n" + myExeSrcFile.fsName);
                return;
                }

                function EventClose (itsEvent)
                {
                var myExeSrcFile = new File (app.scriptArgs.get("Event_Close"));
                if (myExeSrcFile.exists)
                {
                myExeSrcFile.open ('r:(read)');
                app.doScript(myExeSrcFile, ScriptLanguage.javascript);
                myExeSrcFile.close();
                }
                else
                alert ("Error! Missing File:\n\n" + myExeSrcFile.fsName);
                return;
                }

                function EventCopy (itsEvent)
                {
                var myExeSrcFile = new File (app.scriptArgs.get("Event_Copy"));
                if (myExeSrcFile.exists)
                {
                myExeSrcFile.open ('r:(read)');
                app.doScript(myExeSrcFile, ScriptLanguage.javascript);
                myExeSrcFile.close();
                }
                else
                alert ("Error! Missing File:\n\n" + myExeSrcFile.fsName);
                return;
                }

                function EventClean (itsEvent)
                {

                var myExeSrcFile = new File (app.scriptArgs.get("Event_Clean"));

                if (myExeSrcFile.exists)
                {
                myExeSrcFile.open ('r:(read)');
                app.doScript(myExeSrcFile, ScriptLanguage.javascript);
                myExeSrcFile.close();
                }
                else
                alert ("Error! Missing File:\n\n" + myExeSrcFile.fsName);
                return;
                }

                function EventPrint (itsEvent)
                {

                var myExeSrcFile = new File (app.scriptArgs.get("Event_Print"));

                if (myExeSrcFile.exists)
                {
                myExeSrcFile.open ('r:(read)');
                app.doScript(myExeSrcFile, ScriptLanguage.javascript);
                myExeSrcFile.close();
                }
                else
                alert ("Error! Missing File:\n\n" + myExeSrcFile.fsName);
                return;
                }
                ****************************************************
                • 5. Re: eventListeners
                  Level 1
                  Alexandre,

                  Thanks for you posting. If I understand it right the clue is to invoke menu actions to do something like closing a document, am I right?

                  I was wondering if eventListeners are a sort of experimental feature in Indesign CS3. If they don't work properly it's better to leave them out I think.

                  Michel
                  • 6. Re: eventListeners
                    Level 1
                    Hi Michel,

                    The event listener is working properly, it's just that the document is not active at the time you think it should be active.

                    In addition, there are some things that you can do with the document (especially if you use app.documents.item(0) rather than app.activeDocument)--you can create page items, for example, and set XMP metadata, as shown in the example for the afterNew event. What you can'd do is change the view, select objects, or do anything else that relies on the document being active.

                    re: "If they don't work properly it's better to leave them out I think."

                    In terms of general design principles, I disagree. I would always rather be able to have a chance at doing something, rather than no chance at all.:-)

                    Thanks,

                    Ole
                    • 7. Re: eventListeners
                      Level 1
                      Olav,

                      eventListeners are a great feature, but is there any reason not to trigger an "afterOpen" event after the document is fully active?
                      Is this a design problem?

                      I agree that it's good to implement new features, but some more logic behavior would be welcome.

                      Michel