Skip to main content
haim6130
Inspiring
April 4, 2017
Answered

Run script automatically when opening a specific Indesign document

  • April 4, 2017
  • 3 replies
  • 8825 views

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!

    This topic has been closed for replies.
    Correct answer amaarora

    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!


    Hi,

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

    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.

    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

    3 replies

    amaarora
    Inspiring
    April 5, 2017

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

    -Aman

    haim6130
    haim6130Author
    Inspiring
    April 5, 2017

    All - thanks to your patience and kindness!

    rohit_ID
    Adobe Employee
    Adobe Employee
    April 4, 2017

    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 !" );

        }

    }

    amaarora
    Inspiring
    April 4, 2017

    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

    haim6130
    haim6130Author
    Inspiring
    April 4, 2017

    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.

    amaarora
    Inspiring
    April 4, 2017

    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"

    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