Skip to main content
Participant
September 17, 2018
Question

Launch javascript for each document

  • September 17, 2018
  • 5 replies
  • 2455 views

Adobe has a functionality to launch a javascript on launch of Adobe, by dropping a .js file in the following folder:

C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\Javascripts\

I have a document javascript (e.g. scroll to a specific page), which I would like to apply to every PDF I open.
If I add my document specific javascript as a file in the above folder, it doesn't work, because I reference javascript functions which are only available while an active document is opened.

I tried creating a loop to wait for an active document to be opened, but this makes Adobe freeze, even if I open Adobe by double clicking on a document (and thus opening an active document)?

Any advice on how I could tickle this?

This topic has been closed for replies.

5 replies

Legend
September 17, 2018

Acrobat JavaScript is fundamentally unsuitable for this as it doesn't have a "watcher". A lot of programming models do have this, it's a natural thing to look for, just it is not there. Again, if you are specific and detailed about EXACTLY what you want to do, we may have inventive alternatives. You mention " (e.g. scroll to a specific page)" - maybe there is a way, in Windows, based on running a command line script. Or if you want to open a file from an app and do this stuff, sure. But "I want to run a bit of JavaScript on every open" isn't going to fly.

Participant
September 17, 2018

I understand the concern, and yes this is a niche idea for which I have yet to hear a good alternative to my initial problem (I am trying to look for a solution in a constructive way). I agree that you need to build in checks. The first would for instance be limiting it to certain document format name. If I call "app.activeDocs" in the timer function, I never get a return value (even if a document is opened), which makes me believe that you can't seem to call document specific functions, try67​?

try67
Community Expert
Community Expert
September 17, 2018

I did some tests and it only works (for me) if the file is already disclosed, which kind of beats the purpose, as you would need to embed a script in it to make it disclosed in the first place...

Legend
September 17, 2018

And are you SURE you want to apply it to every PDF you open? What about, for example, PDF files viewed in a web browser, which might well run your code.

Legend
September 17, 2018

Perhaps we weren't clear enough. DON'T DO THIS. Please describe your aim, so we can help you find a way that will work.

Participant
September 17, 2018

try67: Makes a lot of sense. Will code inside a folder-level script somehow be able to access the opened document at some point though?

@test screen name:

My aim:

I have a document javascript code block (e.g. scroll to a specific page), which I would like to apply to every PDF I open. The javascript code scrolls to a specific page in the opened PDF, based on contents of the PDF.

try67
Community Expert
Community Expert
September 17, 2018

Sure, if you put that code in a function and call the function after the file has been loaded.

You can use an Action to process your files. Forget about the idea of processing any file that is opened. It might be technically feasible (doubt it, as there are issues with the files being disclosed to your script, beyond those you've already encountered), but it's not a good idea.

Joel Geraci
Community Expert
Community Expert
September 17, 2018

You can't use a loop for the reasons you've already observed. You need to use an interval so that Acrobat has time to do other stuff in between when your code runs. However, I strongly advise you research other solutions. Do you really want this to run on every file that gets opened?

Participant
September 17, 2018

Thank you for your response. I have tried the following:

Create "test.js" in:

C:\Program Files (x86)\Adobe\Acrobat DC\Acrobat\Javascripts


With the following contents:

app.alert("hi",3);

Uppon opening a random PDF, I see the "hi" messagebox.

When I try to use the timeinterval, and put the following code into the test.js, it doesn't work anymore:

function createalert(){

app.alert(this.documentFileName,3);

}

app.setInterval("createalert()",5000);

However, when I run this code in the debug window, it seems to work perfectly.

Any idea why this code seems to break upon putting it in the Javascripts folder?

try67
Community Expert
Community Expert
September 17, 2018

You must use a variable to save the return value of setInterval, even if you don't use it for anything else.