2 Replies Latest reply on Mar 2, 2017 11:54 AM by AndyAtPBZinc

    Need barebones script to run automatically after specific document open

    AndyAtPBZinc Level 1

      Team, so far I've found this to be the best start: Re: Why will this not work on startup? 

      I've gotten as far as putting a script in the startup scripts folder and it even runs...(I replaced the Then action with a simple alert) But the script given in that discussion's answer seems to do its thing before the document even opens. I'm not sure that's what I want. Here are the basic things I would like my script to do;

      1. Run as the very last thing the app does, after the document is open/viewable, after any Preflight or other dialogues, but before I do anything else in the document. (Currently I replaced  action with a simple alert for testing)
      2. Target a specifically named document but not others.
      3. Run if I double-click the specific document to start it/and the app, OR run if I open the document (clicking it or from File menu) when the app is already running/open.
      4. Run if I open the specified document from a book panel (book file)

       

       

      Can anybody suggest some tips or a barebones script, ie the beginning and the IF part of the script so that I can fill in the THEN part with the action I want to accomplish?

      NOTE: I am a scripting/javascript newbie... please be extra clear with ideas... if you give developer level jargon (without the exact code), I will not understand a thing your saying... Please do not throw this InDesign ExtendScript API (12.0) at me... it looks like a great reference but doesn't translate anything into actual code, therefore it's completely useless to me. (right now)

      Thank for any help.

        • 1. Re: Need barebones script to run automatically after specific document open
          Loic.Aigon Adobe Community Professional

          Fact is that the kind of code you are looking for and will probably get in a moment from a fellow requires a good understanding of InDesign Object Model. So I guess you won't have much more understanding of the code itself without doing some efforts in getting the meaning of the objects.

           

          Adding a script that would react to file opening is what is called using a startup script. That is to say it will run as long as InDesign is open and to the condition a targetengine has been set. See, another concept to acquire.

           

          Once you get this listener set to the right event (afterOpen) you need a function to catch this event aka an eventListener. Those function use a special argument of "type" Event with specific properties.

           

          Then you have to ensure the object which fired the event is actually the document you are after. Checking one of its property (url, name, whatever) to a reference and then act properly.

           

          Once that done, that could lead to

           

          #targetengine "onAfterOpen"
          
          
          function main() {
            var evName = "onAfterOpen",
            ev = app.eventListeners.itemByName ( evName );
          
            !ev.isValid && app.eventListeners.add ( "afterOpen", onAfterOpenHandler ).name = evName;
          }
          
          
          function onAfterOpenHandler ( evt ) {
            var doc = evt.parent;
          
            if ( !(doc instanceof Document) 
            ||
            doc.name != "toto.indd" ) return;
          
            alert( "That's the kind of document I like to work with !" );
          }
          
          
          main();
          

           

          Capture d’écran 2017-02-28 à 22.02.47.png

          HTH

           

          Loic

          www.ozalto.com

          1 person found this helpful
          • 2. Re: Need barebones script to run automatically after specific document open
            AndyAtPBZinc Level 1

            Thanks Loic. I will give this a try. I welcome anything else from anybody else!