22 Replies Latest reply on Apr 5, 2017 7:13 AM by haim6130

    Run script automatically when opening a specific Indesign document

    haim6130

      Hi,

       

      I need to run a script automatically when I'm opening a specific Indesign document.

       

      I found this answer:

      Re: Run Script on Document Open Event

      But unfortunately I don't understand how to use and activate this code.

       

      I'd love to get a little help!

      Thanks!

        • 1. Re: Run script automatically when opening a specific Indesign document
          amaarora Adobe Employee

          Hi,

          I did the following On Windows and it worked.

          1. Go to the InDesign Application folder ( for me : C:\Program Files\Adobe\Adobe InDesign CC 2017) and then to the Scripts folder.

          2. In the Scripts folder go to startup script folder ( if not there create one)

          3. Save your script as .jsx there. Am also adding my simple script

          ------------------------------------------------------------------------------------------ -

          #targetengine "onAfterOpen"

          main();

          function main() {

             var myApplicationEventListener = app.eventListeners.add("afterOpen",myfunc);

          }

           

          function myfunc (myEvent) {

            var doc = myEvent.parent;

            if ( doc.name == "sample.indd" )

           

            alert( "It worked !" );

          }

          ------------------------------------------------------------------------------------------ -

          4. It should work as per the event specified that is "afterOpen" of the specific document "sample.indd" mentioned

           

          -Aman

          1 person found this helpful
          • 2. Re: Run script automatically when opening a specific Indesign document
            haim6130 Level 1

            Thanks a lot Aman!

             

            I'm putting the files in 'startup script folder' as you said, but than I don't know where to put the code you wrote... I'm sorry for my ignorance.

             

            staertup.PNG

            • 3. Re: Run script automatically when opening a specific Indesign document
              amaarora Adobe Employee

              Hi,

              In a very very basic way, you need to create a new text file using notepad and then save it as a javascript file. (.jsx)

              1. create a new text file on Desktop

              2. After pasting the code goto save as option in notepad and save it as "script.jsx"

              pic.png

              3. Once you get the .jsx file copy it to the "startup script" folder

              You already have a javascript file "Swimmer" in your folder. You can also copy and edit the same file.

              Once done the script would execute itself automatically when you open the desired InDesign document.

              Also do check that you are in the InDesign Application folder ( for me : C:\Program Files\Adobe\Adobe InDesign CC 2017) and then in the Scripts folder-> startup script

              Do let me know if you are able to do this.

               

              -Aman

              1 person found this helpful
              • 4. Re: Run script automatically when opening a specific Indesign document
                haim6130 Level 1

                Thank you for your patience!

                 

                I've created the JSX as you instructed, but still the 'Swimmer' script doesn't run when I open the file named 'Pas_yomi.indt'.

                 

                Can you help me please?

                Thanks!

                 

                pas yomi.PNG

                cvb.PNG

                • 5. Re: Run script automatically when opening a specific Indesign document
                  amaarora Adobe Employee

                  Hi,

                  That is because the document for which you want the script to run is an "indt" file and not "indd" file. indt files open as "untitled" InDesign document and the script above checks the document name as "Pas_Yomi.indt"

                  -Aman

                  1 person found this helpful
                  • 6. Re: Run script automatically when opening a specific Indesign document
                    haim6130 Level 1

                    Thanks!

                     

                    So now I get the message 'it worked !' when I'm opening the file, but the 'Swimmer' still doesn't run.

                    The 'Swimmer' needs to replace the letters 'u', 'd' and 'n' with images (the colored arrows), but it didn't doing it automatically when I'm opening the document. Why?

                     

                    Here it is BEFORE the 'Swimmer' script runs:

                    fff.PNG

                    Here it is AFTER the 'Swimmer' script runs:

                    dfgfd.PNG

                    And here it is when I'm opening the document (that contains the letters that should become images now):

                    fff.PNG

                     

                     

                    Can you help me please with this last step?

                    Thanks again Aman, you are GREAT!

                    • 7. Re: Run script automatically when opening a specific Indesign document
                      amaarora Adobe Employee

                      Hi,

                      Do edit and tweak the script i posted above so that it calls/executes your "swimmer" script from it.

                      At present your "swimmer" script is not being executed.

                       

                      -Aman

                      1 person found this helpful
                      • 8. Re: Run script automatically when opening a specific Indesign document
                        haim6130 Level 1

                        Okay,

                         

                        How do I make your script to calls/executes my "swimmer" script?

                        • 9. Re: Run script automatically when opening a specific Indesign document
                          amaarora Adobe Employee

                          Hi,

                          Here is the original script...

                          #targetengine "onAfterOpen"

                          main();

                          function main() {

                             var myApplicationEventListener = app.eventListeners.add("afterOpen",myfunc);

                          }

                           

                          function myfunc (myEvent) {

                            var doc = myEvent.parent;

                            if ( doc.name == "sample.indd" )

                          {

                          //ADD THE MAIN WORKING CODE OF SWIMMER SCRIPT HERE!

                          alert( "It worked !" );

                               }

                          }

                          -Aman

                          1 person found this helpful
                          • 10. Re: Run script automatically when opening a specific Indesign document
                            haim6130 Level 1

                            Hi Aman, Thanks a lot!

                             

                            I'm sorry but I don't know what is THE MAIN WORKING CODE OF SWIMMER SCRIPT.

                            That script looks like hell founds inside it!

                             

                            Here is the files for the Swimmer script, please help me find the main working code of it and put it correctly in your JSX file, so it will run when I'll open the specific document.

                             

                            The Swimmer files:

                            http://www.megafileupload.com/1qizl/Swimmer.jsxbin

                            http://www.megafileupload.com/1qizk/SwimmerConfig.ini

                             

                            Thank you very much!

                            • 11. Re: Run script automatically when opening a specific Indesign document
                              rohit_ID Adobe Employee

                              Hi

                              Just add the following code where aman is asking to add :

                              var myFile = new File (app.activeScript.parent.fsName + '/abc.jsx');
                                  if (myFile.exists)
                                  {
                                        app.doScript(myFile ,ScriptLanguage.JAVASCRIPT);

                              }

                               

                              doscript function lets you call another javascript / Vbscript / Applescript .

                              So your script becomes:

                              #targetengine "onAfterOpen"

                              main();

                              function main() {

                                var myApplicationEventListener = app.eventListeners.add("afterOpen",myfunc);

                              }

                               

                              function myfunc (myEvent) {

                                var doc = myEvent.parent;

                                if ( doc.name == "sample.indd" )

                              {

                              var myFile = new File (app.activeScript.parent.fsName + '/swimmer.jsx');
                                  if (myFile.exists)
                                  {
                                        app.doScript(myFile ,ScriptLanguage.JAVASCRIPT);

                              }

                              alert( "It worked !" );

                                  }

                              }

                              1 person found this helpful
                              • 12. Re: Run script automatically when opening a specific Indesign document
                                amaarora Adobe Employee

                                Hi,

                                I doubt that the above solution proposed by rohit_ID will work.

                                 

                                Here is my updated script...

                                 

                                #targetengine "onAfterOpen"

                                main();

                                function main() {

                                   var myApplicationEventListener = app.eventListeners.add("afterOpen",myfunc);

                                }

                                 

                                 

                                function myfunc (myEvent) {

                                  var doc = myEvent.parent;

                                  if ( doc.name == "sample.indd" )

                                  {

                                   alert("hi");

                                    var thisFile = new File($.fileName);  

                                    var basePath = thisFile.parent.path;

                                    var f = new File (basePath + '/Swimmer/Swimmer.jsxbin');

                                    app.doScript(f ,ScriptLanguage.JAVASCRIPT);

                                   }

                                }

                                 

                                The above script will run with the following directory structure:

                                1. Have the above script in the "startup scripts" folder ->  this script will run automatically when "sample.indd" is opened

                                2. Have the Swimmer folder in the Scripts folder and not in "Startups scripts folder"

                                A snapshot of my "Swimmer" folder..

                                Picture3.png

                                 

                                Your remaining work now involves to configure the .ini file and the images folder, which you already know how to from Auto replace text with an object

                                It works for me and should for you too.

                                -Aman

                                1 person found this helpful
                                • 13. Re: Run script automatically when opening a specific Indesign document
                                  haim6130 Level 1

                                  Thank you very much Aman and rohit!

                                   

                                  I've tried what you said Aman, and it works! the only problem is it's running Swimmer BEFORE the document opens, so I'm getting the 'Hi !' alert from your script and than an error alert from the Swimmer: 'No documents are open'. only when I click 'OK' on the error alert, the document opens.

                                   

                                  How can I do that the Swimmer will run AFTER the document opens?

                                   

                                  I'm sorry for the trouble and appreciate it very much! Thanks!

                                  • 14. Re: Run script automatically when opening a specific Indesign document
                                    amaarora Adobe Employee

                                    Hi,

                                    You can remove the alert line from the script so that the "Hi" does not come.

                                     

                                    Try opening some other document before the "sample.indd" file. Also check that you have not placed the "Swimmer" folder in the startup scripts folder and only placed in the Scripts folder.

                                    Follow the directory structure as shown in my previous reply.

                                    -Aman

                                    1 person found this helpful
                                    • 15. Re: Run script automatically when opening a specific Indesign document
                                      haim6130 Level 1

                                      Hi Aman,

                                       

                                      I did exactly as you said. thanks!

                                      So if I'm opening the "sample.indd" file after some other document already open, the Swimmer error doesn't show up, but still the script dones't runs on the "sample.indd" file.

                                      See above in my comment 6, I need the Swimmer to replace the letters with the images automatically when I'm opening the "sample.indd" file. For now, the text stay without changing when I'm opening the "sample.indd" file.

                                       

                                      I don't know what I'm doing wrong.

                                      You said you succeeded to open a document and make the Swimmer script runs and make the text replaced by images automatically when the file opens?

                                      • 16. Re: Run script automatically when opening a specific Indesign document
                                        amaarora Adobe Employee

                                        Hi,

                                        I recommend we continue this on either personal message or on mail(amaarora@adobe.com), since the original question has already been answered.

                                         

                                        -Aman

                                        1 person found this helpful
                                        • 17. Re: Run script automatically when opening a specific Indesign document
                                          haim6130 Level 1

                                          Hi Aman,

                                           

                                          And again - Thanks for everything!

                                           

                                          I'm sorry but I'm afraid the original question is not answered yet... I asked how can I run a script automatically when I'm opening a specific Indesign document, and the script (Swimmer) doesn't run yet.

                                          I'm afraid we will move to the personal message or on mail and the problem will stay.

                                           

                                          Please help me run the Swimmer when I'm opening a specific "sample.indd" file (my original question).

                                          You guided me so far, please help me finish this properly.

                                           

                                          Thanks!

                                          • 18. Re: Run script automatically when opening a specific Indesign document
                                            Laubender Adobe Community Professional & MVP

                                            Hi Haim,

                                            if it's ok with you, I would suggest, that a moderator should move the whole thread to the Adobe InDesign Scripting forum:

                                            InDesign Scripting

                                             

                                            FWIW
                                            This topic is also realated to this thread here:

                                            Auto replace text with an object

                                             

                                            Regards,
                                            Uwe

                                            • 19. Re: Run script automatically when opening a specific Indesign document
                                              amaarora Adobe Employee

                                              Hi,

                                              This is my folder structure and my Swimmer.config.ini file

                                              Picture1.png

                                              This is the original swimmer folder.

                                              I have placed the below script in the startup scripts folder that runs the Swimmer.jsxbin file. Lets name this script as "trigger.jsx"

                                               

                                              #targetengine "onAfterOpen"

                                              main();

                                              function main() {

                                                 var myApplicationEventListener = app.eventListeners.add("afterOpen",myfunc);

                                              }

                                               

                                               

                                              function myfunc (myEvent) {

                                                var doc = myEvent.parent;

                                                alert(myEvent.parent);

                                                if ( doc.name == "sample.indd" )

                                                {

                                                 alert("hi");

                                                  var thisFile = new File($.fileName);   

                                                  var basePath = thisFile.parent.path;

                                                  alert(basePath);

                                                  var f = new File (basePath + '/Swimmer/Swimmer.jsxbin');

                                                  app.doScript(f);

                                                 }

                                              }

                                               

                                              Now, the "Swimmer.jsxbin" file is executed automatically when the "sample.indd" file is open. The "swimmer.jsxbin" file is written in such a way so that it executes on ALL the open document. With my script "trigger.jsx" the "swimmer.jsxbin" file is ONLY TRIGGERED TO RUN. The writter of the script "swimmer.jsxbin" wrote it in a way so that it executes on all the open documents.

                                              In order to check the above do this:

                                              1. Open "sample.idml" present in the ORIGINAL swimmer folder. This file contains the text that will be replaced by the images.

                                              Picture2.png

                                              This text to be replaced is mentioned in the ini config file. (as shown above). Do note: am talking about the original swimmer folder which is not edited by you and only downloaded from the link you mentioned.

                                              2. save "sample.idml" as "doc.indd" and also save it as "sample.indd"

                                              -> doing this step so that idml is saved as indd

                                              3. close Indesign

                                              4. open Indesign and the document "doc.indd"

                                              5. Open "sample.indd" -> at this point "swimmer.jsxbin" should be executed thorough "trigger.jsx" without any error.

                                              Now you will see that the content of the "doc.indd" file are replaced by the "swimmer.jsxbin", but "sample.indd" remains uneffected,

                                              To make it run ONLY on "sample.indd" you need to edit the "swimmer.jsxbin" file.

                                               

                                              Hope I am clear.

                                              -Aman

                                              1 person found this helpful
                                              • 20. Re: Run script automatically when opening a specific Indesign document
                                                haim6130 Level 1

                                                It Worked!!!

                                                 

                                                Thanks a lot Aman!!!

                                                • 21. Re: Run script automatically when opening a specific Indesign document
                                                  amaarora Adobe Employee

                                                  I am glad that after so much to and fro it finally worked

                                                  -Aman

                                                  1 person found this helpful
                                                  • 22. Re: Run script automatically when opening a specific Indesign document
                                                    haim6130 Level 1

                                                    All - thanks to your patience and kindness!