5 Replies Latest reply on Apr 11, 2010 10:36 PM by Kasyan Servetsky

    Trigger script on the opening of new InDesign doc CS4 js

    Yuliaart Level 1

      Hi,

       

      Is it possible to trigger a java script on the double click of the InDesign doc in finder, so the script runs as soon as doc is open. How it can be done.

       

      Thank you very much for your help.

       

      Yulia

        • 1. Re: Trigger script on the opening of new InDesign doc CS4 js
          Shonkyin Level 3

          Yulia,

           

          yes this is possible with EventListener.

           

          Please find attached file for example.

           

          Cheers,

          Shonky

          • 2. Re: Trigger script on the opening of new InDesign doc CS4 js
            Kasyan Servetsky Level 5

            Hi there,

             

            To me InDesign’s after/beforeOpen events are at the very least useless, since they are triggered before the document’s layout window is opened. This is why you can do nothing useful with the document.

            The workaround is to use APID Toolkit’s docLoaded event instead, which works perfectly. If you are interested, here is an example of using it: check out source code for Check profile plug-in.

             

            Kasyan

             

            P.S. This was the case with CS3, may be with CS4 things took a turn for the better — I can't check it right now.

            • 3. Re: Trigger script on the opening of new InDesign doc CS4 js
              Shonkyin Level 3

              Hi Kasyan,

               

              You are absolutely right and your attached script is good example of docLoad event.

               

              I read afterOpen event in Indesign Javascript Reference Guide but not used it. This event not work how they write in reference guide, after/before working same.

               

              Thanks for your information.

               

              Shonky

              • 4. Re: Trigger script on the opening of new InDesign doc CS4 js
                Yuliaart Level 1

                Hi, Kasyan.

                 

                I finally had a chance to look into your script and adjusted it to what I need, but I am not sure how to make it work. The script that I need to trigger works on it's own. Now I want it to be triggered as document gets open from Finder.  Here some of what I have:

                 

                 

                 

                if (theItem.eventCode == "docLoaded") {
                     var myDoc = app.activeDocument;
                    var myDocName = app.activeDocument.name;
                    placeFilesOnDoubleClick();
                }

                 

                 

                 

                function placeFilesOnDoubleClick(){

                 

                // places single page 'front_' and 'back _' files from the same as InDesign file folder and centres them

                 

                myDoc = app.activeDocument;
                myDocPath = myDoc.filePath;
                var myFolder = new Folder(myDocPath);
                myGB_Y2 = myDoc.documentPreferences.pageHeight;
                myGB_X2 = myDoc.documentPreferences.pageWidth;

                ...

                }

                 

                 

                 

                Thank you very much for your help.

                 

                Yulia

                • 5. Re: Trigger script on the opening of new InDesign doc CS4 js
                  Kasyan Servetsky Level 5

                  Hi Yulia,

                   

                  To use the script with docLoaded event you should compile a scripted plug-in using the APIDTemplate, place this plug-in to the folder of the same name and restart InDesign.

                  I asserted that before/afterOpen events are useless all the time here on the forum, but I was wrong. I talked to Peter Truskier on InDesign forum and he opened my eyes on this issue. The trick is to use app.documents[0] instead of app.activeDocument.

                  Below is a sample code that should work for you. Don't forget to use persistent engine, e.g. "session", place the script to "Startup Scripts" folder. I think It is good idea to define variables using var inside placeFilesOnDoubleClick function to make them local.

                   

                  Regards,

                  Kasyan

                   

                  #target indesign
                  #targetengine "session"
                  
                  var myEventListener = app.eventListeners.add("afterOpen", placeFilesOnDoubleClick, false);
                   
                  function placeFilesOnDoubleClick(){
                       var myDoc = app.documents[0];
                       var myDocPath = myDoc.filePath;
                       var myFolder = new Folder(myDocPath);
                       var myGB_Y2 = myDoc.documentPreferences.pageHeight;
                       var myGB_X2 = myDoc.documentPreferences.pageWidth;
                       ...
                  }