2 Replies Latest reply on Apr 4, 2017 12:47 AM by haim6130

    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\");"; 
          
          • 2. Re: Run Script on Document Open Event
            haim6130 Level 1

            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