4 Replies Latest reply on Jan 19, 2016 7:54 AM by G. Singelmann

    Event Listner

    tpk1982 Level 4



      My requirement is not to import .docx files, if one user import .docx then the script need to alert for confirmation to proceed further.


      My idea is using event listener and place the script in startup scripts folder. Below is the tried code:


      #targetengine "session"  
      function main(){  
      var myEventListener = app.addEventListener("beforePlace", myPlaceFile, false);  
      function myPlaceFile(myEvent){  
      var myconfirm=confirm("You are placing docx file? Shall continue?")  
      // here need to place the files  
      //need to exit  





        • 1. Re: Event Listner
          Vamitul Level 4

          Is this what you are looking for?

          InDesign ExtendScript API (8.0)

          • 2. Re: Event Listner
            G. Singelmann Level 3

            When you place something you trigger four (relevant to your question) events: beforeImport, afterImport, beforePlace, afterPlace.


            When you consider the Place Gun (i.e. the loaded cursor you get when you manually place a file) the meanings are these:

            • beforeImport: ID ist about to load the place gun
            • afterImport: ID has loaded the place gun
            • beforePlace: the user has clicked with a loaded place gun
            • afterPlace: ID has placed the file at the location of the click


            Problem: Unless you create links for text files the beforeImport/afterImport event does not know what it is that its importing. (Someone please correct me if this is wrong).


            The only solution I can currently think of would be to hook into the call of the menu item "Place" with MenuAction.afterInvoke and handle the file-selection etc yourself.


            FWIW, here is a snippet that installs most of InDesign's event listeners to help you see, when what happens.

            //An InDesign CS5 JavaScript
            //Installs event listeners for many supported events; displays a
            //message when each event occurs.
            #targetengine "session"
            function main(){
            function mySetup(){
            function mySnippet(){
                 var myEventNames = [
                      "beforeQuit", "afterQuit",
                      "beforeNew", "afterNew",
                      "beforeOpen", "afterOpen",
                      "beforeClose", "afterClose",
                      "beforeSave", "afterSave",
                      "beforeSaveAs", "afterSaveAs",
                      "beforeSaveACopy", "afterSaveACopy",
                      "beforeRevert", "afterRevert",
                      "beforePrint", "afterPrint",
                      "beforeExport", "afterExport",
                      "beforeImport", "afterImport",
                      "afterDelete", "beforeDelete", 
                      "afterEmbed", "beforeEmbed",
                      "afterInvoke", "beforeInvoke",
                      "afterMove", "beforeMove",
                      "afterPlace", "beforePlace",
                 /*     "afterSelectionAttributeChanged", 
                      "afterSelectionChanged",     */
                      "afterUnembed", "beforeUnembed",
                      "afterUpdate", "beforeUpdate"
                 ] ;
                 for (var myCounter = 0; myCounter < myEventNames.length; myCounter ++){
                      app.activeDocument.addEventListener(myEventNames[myCounter], myEventInfo);
            function myTeardown(){
            function myEventInfo(myEvent){
                      var myString = "Application Event: " +myEvent.eventType;
                      myString += "\r\rTarget: " + myEvent.target.constructor.name + ", " +myEvent.target.name;
                      myString += "\rCurrent: " +myEvent.currentTarget.constructor.name + ", " + myEvent.currentTarget.name;
                      myString += "\r\rPhase: " + myGetPhaseName(myEvent.eventPhase );
                      myString += "\rBubbles: " + myEvent.bubbles;
                      myString += "\r\rCancelable: " +myEvent.cancelable;
                      myString += "\rStopped: " +myEvent.propagationStopped;
                      myString += "\rCanceled: " +myEvent.defaultPrevented;
                      myString += "\r\rTime: " +myEvent.timeStamp;
                      function myGetPhaseName(myPhase){
                                case EventPhases.atTarget:
                                     myPhaseName = "At Target";
                                case EventPhases.bubblingPhase:
                                     myPhaseName = "Bubbling";
                                case EventPhases.done:
                                     myPhaseName = "Done";
                                case EventPhases.notDispatching:
                                     myPhaseName = "Not Dispatching";
                                     myPhaseName = "no phase defined";
                           return myPhaseName;
                      alert("Error getting event properties.");
            • 3. Re: Event Listner
              Laubender Adobe Community Professional & MVP

              Hi Gerald,

              beforeImport does indeed know about the file that will be placed.

              The event when fired has the property fullName which is showing the file or it's full path plus name.

              I tested this with InDesign CS6 v8.1.0 on Mac OSX.


              See also my snippet where I am "exploiting" this:

              Re: EventListner


              The thread here is a double post of this other one (or vice versa)…



              • 4. Re: Event Listner
                G. Singelmann Level 3

                Hi Uwe,


                oh good. I didn't have time to look that far into it then.


                Hopefully tpk1982 can make use of the information