Skip to main content
Inspiring
February 4, 2013
Answered

Run Script on Document Open Event

  • February 4, 2013
  • 1 reply
  • 4482 views

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

Correct answer dulajun

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 reply

dulajun
dulajunCorrect answer
Inspiring
February 5, 2013

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

haim6130
Inspiring
April 4, 2017

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

Participating Frequently
October 13, 2017

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