11 Replies Latest reply on Jan 19, 2016 1:01 AM by tpk1982

    EventListner

    tpk1982 Level 4

      Hi,

       

      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"
      main();
      function main(){
      var myEventListener = app.addEventListener("beforePlace", myPlaceFile, false);
      }
      function myPlaceFile(myEvent){
      var myconfirm=confirm("You are placing docx file? Shall continue?")
      if(myconfirm)
      {
      // here need to place the files
      }
      else{
      //need to exit
      
      }
      
      }
      

       

      Regards,

      Karthi

        • 1. Re: EventListner
          Peter Spier Most Valuable Participant (Moderator)

          Moved to the scripting forum...

          • 2. Re: EventListner
            Laubender Adobe Community Professional & MVP

            Hi Karthi,

            there are various problems with this approach.

             

            Better use "beforeImport" :

            Adobe InDesign CS6 (8.0) Object Model JS: Document

             

            And then run a for x in myEvent loop to check the various property/value pairs that are in myEvent.

            Property fullName of that looks very promising.

             

            #targetengine "checkBeforeImport"
            
            var myBeforeImport = app.eventListeners.add("beforeImport",checkPropertiesOfImportEvent);
            var myCounter = 0;
            var x;
            
            function checkPropertiesOfImportEvent(myBeforeImportEvent)
            {
                ++myCounter
                for(x in myBeforeImportEvent)
                {
                    $.writeln(myCounter+"\t"+x+"\t"+myBeforeImportEvent[x]);
                };
            };
            

             

            Uwe

            • 3. Re: EventListner
              tpk1982 Level 4

              Hi Uwe,

               

              Thanks for your reply. It works only if i use Import option, but the people here only using Place option for flowing texts

               

              Regards,

              Karthi

              • 4. Re: EventListner
                Laubender Adobe Community Professional & MVP

                After testing a bit with several event listeners listening for:

                 

                beforeImport

                afterImport

                beforePlace

                afterPlace

                 

                I think, it's hard to impossible to prevent a user from placing a Word document.

                However, you can always notify the user, that he/she is about to place a document with suffix *.doc or *.docx .

                 

                I tried to save a document after the beforeImport event is fired and it is clear, that the name of the file has the right suffix and the user has confirmed not to procede. Property fullName of the event gave me the file and its suffix. But the listening mechanism does not allow to save or to close the document at this stage.

                 

                If this would work we could revert to the old state after the file was placed.

                But this is also not possible. The listening mechanism does not allow this either.

                 

                So, currently I'm out of ideas…

                 

                Uwe

                • 5. Re: EventListner
                  Laubender Adobe Community Professional & MVP

                  Hm, I tested without using import options.

                  The listener for beforeImport fired. Even as I dragged a doc text document to the open InDesign document.

                  Maybe that is different on Windows? I used InDesign CS6 v8.1.0 on Mac OSX.

                   

                  Uwe

                  • 6. Re: EventListner
                    Laubender Adobe Community Professional & MVP

                    Hm.

                    Finally I had success with the following combination of event listeners:

                     

                    beforeImport

                    afterSelectionChanged

                     

                    The beforeImport event would set a flag, if a doc or docx file is imported and the user confirms not to like that.

                     

                    The afterSelectionChanged event will only act upon that flag. Otherwise does nothing.

                    If the flag is set, two undos are necessary to undo placing the file. And that is working. For now.

                     

                    Warning: I still don't know, if for ALL cases.

                    Just found one case where the user will be very likely irritated.

                    And he/she always can undo the undos.

                     

                    Note:

                    We could check the undoName of the document and/or the undoHistory array to decide what to do.

                    The values of these properties are locale dependent. So another step would be necessary to build a script, that will work on all international versions of InDesign using these strings.

                     

                    Tested with my German InDesign CS6 v8.1.0 on OSX.

                     

                    Uwe

                    • 7. Re: EventListner
                      tpk1982 Level 4

                      Hi Uwe,

                       

                      Can you please share your coding for

                      beforeImport

                      afterSelectionChanged?

                       

                      I am beginner in event listener user

                       

                      Thanks,

                      Karthi

                      • 8. Re: EventListner
                        Laubender Adobe Community Professional & MVP

                        Hi Karthi,

                        I think nearly everyone is a "beginner" when it comes to event listeners.

                        You have to do a lot of testing to anticipate what is happening where and when.

                         

                        I can give you the following code, that is not "field tested".

                        E.g. I did not test what will happen, if the user is dragging more than one file to the document (is that possible at all?)

                        There may be circumstances where the code will not work as expected or will annoy the user too much.

                         

                        It is also debatable at what point we like to intercept the process of importing or placing.
                        In any case we have to do it with "beforeImport" or "afterImport". Otherwise the file name cannot be retrieved (at least to my knowledge).

                         

                        It could be, that we also need "beforePlace" and "afterPlace" for some checks or to control the process more densly. Also inspecting the placeGun could be necessary perhaps.

                         

                        Before testing, read my comments in the script, there is room for improvement.
                        Also test with other file suffixes like "txt".

                         

                        // Notify-and-UndoPlacingOf-WordFiles-COMMENTED-VERSION.jsx
                        // Uwe Laubender
                        
                        /**
                        * @@@BUILDINFO@@@ Notify-and-UndoPlacingOf-WordFiles-COMMENTED-VERSION.jsx !Version! Tue Jan 19 2016 07:06:06 GMT+0100
                        */
                        
                        // See discussion at:
                        // https://forums.adobe.com/message/8399607#8399607
                        
                        // NOTE:
                        // I did not test this, just a suggestion:
                        // ONE VARIANT COULD BE NOT TO USE THE "afterSelectionChanged" EVENT LISTENER FROM THE START,
                        // BUT TO ADD IT AFTER THE USER CANCELS IT, HAVE THE TWO undo() AND REMOVE IT AFTER DOING THIS.
                        // "afterSelectionChanged" IS VERY OFTEN FIRED, SO IT WOULD BE GOOD TO MINIMIZE ITS USE.
                        
                        
                        #targetengine "Notify-and-UndoPlacingOf-WordFiles"
                        
                        // FLAG, THAT IS SET, IF THE USERS CONFIRMS, THAT HE/SHE DOES NOT LIKE TO IMPORT doc/docx FILES:
                        // Default value is: false
                        var undoPlaceWordFile = false;
                        
                        // Variable is not used further in this script snippet.
                        var myBeforeImport = app.eventListeners.add("beforeImport",checkAllowImport);
                        // Variable is not used further in this script snippet.
                        var myAfterSelectionChanged = app.eventListeners.add("afterSelectionChanged",undoPlaceIfPlacedAWordFile);
                        
                        
                        // Purpose of this function:
                        // 1. Check, how flag is set:
                        
                        // false : do nothing
                        // true : do two undo() actions
                        
                        // 2. Afterwards reset the flag to default
                        
                        
                        function undoPlaceIfPlacedAWordFile(myAfterSelectionChangedEvent)
                        {
                            if(undoPlaceWordFile === false){return};
                           
                            // That would load the place gun again or undo a replacement action:
                            app.documents[0].undo();
                            // That would undo importing the file:
                            app.documents[0].undo();
                           
                            // Note 1: Even, if this will work,
                            // the user could load the place gun when he/she will undo the undo.
                           
                            // Note 2: Before doing every undo(), you can check the value of:
                            // document.undoName
                            // to determine, if the undo() should be done.
                           
                            // Reset the flag to default:
                            undoPlaceWordFile = false;
                        }
                        
                        // Purpose of this function:
                        // Check the file name of the imported file and decide, if it is a Word document.
                        // I do this with a match on the string of the file name. The suffixes doc and docx at the end of the name.
                        // If there is no suffix or the suffix will not match, the function will do nothing.
                        
                        
                        function checkAllowImport(myBeforeImportEvent)
                        {
                        
                            var myPlacedFile = File(myBeforeImportEvent.fullName);
                           
                            // If files are about to be placed, that do NOT MATCH file suffixes ".doc" or ".docx", the function will do nothing.
                            // Or if the file has no suffix at all.
                           
                            // This matching procedure could be refined perhaps looking into the file.
                           
                            if(!myPlacedFile.name.match(/(\.doc$)|(\.docx$)/i)){return};
                           
                            // If the show goes on, a confirm dialog will be started, that will set the undoPlaceWordFile flag
                           
                            // myConfirm : true  does mean the user likes to place the doc file, do nothing then.
                           
                            // myConfirm : false does mean the user likes to undo() the import.
                            // The flag will be set to value true.
                            // The doc or docx file will be placed.
                            // That will force a afterSelectionChanged event (see function undoPlaceIfPlacedAWordFile() above),
                            // either a new frame is created and selected
                            // or a selected text will be changed (replaced) after placing.
                           
                            var myConfirm = confirm("You are about to place a Word file. Shall we continue?");
                           
                            // Do nothing:
                            if(myConfirm === true){return};
                           
                            // Reset the flag to value true; no if-clause really needed here, just to show you the mechanism better:
                            if(myConfirm === false){undoPlaceWordFile = true};
                           
                        };
                        

                         

                        Sorry, I will not give further assistance in writing and testing this. Too much time will go into this.

                        But I think, you can get the ideas behind all this now. Do a lot of testing, also with some $.writeln() statements for getting the idea what the listeners are and what properties and values you can check when listening to something.

                         

                        Uwe

                        • 9. Re: EventListner
                          tpk1982 Level 4

                          Hi Uwe,

                           

                          Sorry if i ate your time. But the above script what you gave is more enough to improve.

                           

                          Thanks a lot for your help.

                           

                          Regards,

                          Karthi

                          • 10. Re: EventListner
                            Laubender Adobe Community Professional & MVP

                            Hi Karthi,

                            no problem.

                             

                            Just come back here, if you find a recipe and share your experience with the event listeners.

                             

                            Best,

                            Uwe

                            • 11. Re: EventListner
                              tpk1982 Level 4

                              Hi Uwe,

                               

                              Sure, i will share the final script

                               

                              Thanks,

                              Karthi