Launch javascript for each document

New Here ,
Sep 17, 2018

Copy link to clipboard

Copied

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?

TOPICS
Acrobat SDK and JavaScript, Windows

Views

222

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Launch javascript for each document

New Here ,
Sep 17, 2018

Copy link to clipboard

Copied

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?

TOPICS
Acrobat SDK and JavaScript, Windows

Views

223

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Sep 17, 2018 0
Adobe Community Professional ,
Sep 17, 2018

Copy link to clipboard

Copied

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 17, 2018 0
New Here ,
Sep 17, 2018

Copy link to clipboard

Copied

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 17, 2018 0
Most Valuable Participant ,
Sep 17, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 17, 2018 0
Adobe Community Professional ,
Sep 17, 2018

Copy link to clipboard

Copied

The Acrobat JavaScript documentation is your best friend.

Acrobat DC SDK Documentation

The return value of this "setInterval" must be held in a JavaScript variable. Otherwise, the interval object is subject to garbage-collection, which would cause the clock to stop.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 17, 2018 0
New Here ,
Sep 17, 2018

Copy link to clipboard

Copied

Thank you for your patience. As you can see, I'm taking my first babysteps in this world. By using the timeout function, I had the same problem when using a while loop to wait a few seconds:

function createalert(){

app.alert("hi",3);

app.alert(this.documentFileName,3);

}

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

Even though I have a document open, this.documentFileName still gets returned undefined. My guess is because even though I have only one PDF open, the code does not get applied to the current pdf, as it would if I just run it in the debugger window.

My apologies if my question wasn't very clear, but I still seem to have the problem on how to reference javascript functions which are applied to the current opened document, from inside a jsfile placed in the Adobe programfiles folder.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 17, 2018 0
Most Valuable Participant ,
Sep 17, 2018

Copy link to clipboard

Copied

And don't try to use "this" in a timer. What should it even be? The documentation tells you when "this" is valid, and what it means, and fails to tell you quite forcefully enough NEVER USE IT OTHER TIMES.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 17, 2018 0
Most Valuable Participant ,
Sep 17, 2018

Copy link to clipboard

Copied

Also, when the folder-level script is launched when you open the application (by double-clicking a file, for example) it is executed before the file is actually opened, so you can't access it at that time.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 17, 2018 0
Most Valuable Participant ,
Sep 17, 2018

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 17, 2018 0
New Here ,
Sep 17, 2018

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 17, 2018 0
Most Valuable Participant ,
Sep 17, 2018

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 17, 2018 0
Adobe Community Professional ,
Sep 17, 2018

Copy link to clipboard

Copied

Ok - Stop. Slowly back away from your keyboard. Don't make any sudden movements.

The only way to do what you want to do is to create an a function that runs at a specified interval that will collect the unique ids of every document, somehow indicate which ones it has already scrolled to the page you want, look for any new documents that have been opened and have not run the scroll code, scroll them, add them to the list of scrolled documents, and then remove any documents from the list that may have been closed.

While it's possible, for this code to be of any use, the interval would need to run at a fairly frequent interval and that's going to just kill Acrobat and slow down everything else it's doing.

YOU DO NOT WANT TO DO THIS!

Please describe your goal... be specific. Trust me on this, creating an application level event handler to run every time a document gets opened is far more complicated than it seems.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 17, 2018 0
Most Valuable Participant ,
Sep 17, 2018

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 17, 2018 1
New Here ,
Sep 17, 2018

Copy link to clipboard

Copied

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​?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 17, 2018 0
Most Valuable Participant ,
Sep 17, 2018

Copy link to clipboard

Copied

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...

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 17, 2018 0
Most Valuable Participant ,
Sep 17, 2018

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 17, 2018 0