5 Replies Latest reply on Oct 22, 2017 1:16 AM by Otto Hänninen

    Run Script on Document Open Event

    jkinsman Level 1

      Ok so I know about the afterOpen event and I think I understand how to use it, however, I don't understand how to make it open for a specific document.

       

      I don't want this script to run on every document that I open, I want to embed it in a documnt and then run it everytime I open that document. Is this possible?

       

      Perhaps run the script from scripts label?

       

      Or would I just put a script in the startup panel and then create a session script that checks the name of each document opened and then runs the script if I have a match?

       

      Thanks

        • 1. Re: Run Script on Document Open Event
          dulajun Level 2

          As you chart put this in the startup scripts folder

          //startup script
          #targetengine "session"
          main();
          function main(){
              var myEventListener = app.eventListeners.add("afterOpen", excuteScriptLabel);
          }
          
          
          function excuteScriptLabel(myEvent){
                    //The parent of the event is the document.
                    var myDocument = myEvent.parent;
              if (myDocument.constructor.name == "Document") {
                  if (myDocument.label != "")
                      app.doScript(myDocument.label, ScriptLanguage.javascript);
              }
          }
          

           

          Then try this line on a document and save it, then open the same file and some other others, you will notice that this code only excuted on that document:

           

          app.activeDocument.label = "alert (\"Yes\");"; 
          
          1 person found this helpful
          • 2. Re: Run Script on Document Open Event
            haim6130

            Hi,

             

            You said that if I want to script to run of specific document, I need to put that line in the document.

            Where \ How do I put that code line in the document?

             

            And, The script that you first wrote, how do I run it? just save a JSX of it in the 'startup script' folder?

             

            Thanks

            • 3. Re: Run Script on Document Open Event
              Otto Hänninen

              Thank you dulajun for the example code. Trying to use it in Acrobat X (javascripts-folder) produces this error below; the line 2 contains statement :

              #targetengine "session" 

               

              Why this error:

               

              Acrobat EScript Built-in Functions Version 10.0

              Acrobat SOAP 10.0

               

              SyntaxError: illegal character

              2:Folder-Level:App:AnydocOpenEvent.js

              • 4. Re: Run Script on Document Open Event
                Laubender Adobe Community Professional & MVP

                Hi Otto,

                I hope, the "why" is clear to you.

                 

                If not: This script is written in ExtendScript for InDesign.
                It's not a JavaScript script for Acrobat X.

                 

                Regards,
                Uwe

                • 5. Re: Run Script on Document Open Event
                  Otto Hänninen Level 1

                  Thank you Uwe, yes, I suspected something like this, but as I am new to both javascripting and scripting for Acrobat, I did not recognize the difference in programming environments (towards this extent it would be nice to either users to note their environment in code comments, I only wish I remembered to do so, or to have language recognizer tool that would tell which environment(s) a given code would run).

                   

                  Are you able to help me getting the afterOpen event processing work - at this point I'd need an example that would e.g. show a dialog when the event is handled "Hello world, I'm being opened now!".

                   

                  Best regards

                  Otto