How to execute a Trusted script on the open document ?

New Here ,
Dec 04, 2018 Dec 04, 2018

Copy link to clipboard

Copied

After a migration from win7 from win10, my team do not know how to execute the script anymore. Here is the script I want to execute, it should append another PDF to the end of document and send a mail (I did not write it) : https://justpaste.it/61gxj .

From what I understand from google, I have to put this script in C:\Users\UserName\AppData\Roaming\Adobe\Acrobat\Privileged\10.0\JavaScripts to be able to execute trusted methods/functions. Then, how do I actually use this function ? Is the path correct ? Thank you for your help, I'm really new to this.

Edit :

app.getPath("app","javascript"); returns  /C/Program Files (x86)/Adobe/Acrobat DC/Acrobat/JavaScripts so I added my script there

TOPICS
Acrobat SDK and JavaScript, Windows

Views

478

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
community guidelines
Adobe Community Professional ,
Dec 04, 2018 Dec 04, 2018

Copy link to clipboard

Copied

One of the first things the script does is try to read a file from...

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

... but there's no error checking so it just fails if not found.

Did you migrate that file as well when you updated your machine?

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
community guidelines
Adobe Community Professional ,
Dec 04, 2018 Dec 04, 2018

Copy link to clipboard

Copied

Either folder will work, but there's an error in your code. You did not specify the iconPath variable correctly.

The correct syntax is;

var iconPath = "/C/Program Files (x86)/Adobe/Acrobat DC/Acrobat/Javascripts/bcs.jpg";

And the file doesn't have to be located in the trusted folder, by the way. It can be anywhere.

To call the first function use this command:

main();

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
community guidelines
New Here ,
Dec 04, 2018 Dec 04, 2018

Copy link to clipboard

Copied

Hello,

thank you both for your answer try67​ Joel_Geraci, but the problem at the moment is simply to be able to play the code. The script does not show anywhere in Adobe.

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
community guidelines
LEGEND ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

You say the script "does not show anywhere in Adobe". But scripts are just defined, not shown. What exactly do you mean by "shown"?

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
community guidelines
Adobe Community Professional ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

The script adds a tool button.

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
community guidelines
New Here ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

@test I added the script in the folder. I thinks that is what you mean by being defined. Then, I want to use that script. I don't see anything in Adobe that would let me access, call, write... that script in an action wizard or anywhere else.

@Bernd Where should it create a tool button ? I'm in the tool menu and there are only the default actions

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
community guidelines
Adobe Community Professional ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

The tool button is at Tools > Add-on Tools. The label is "Finish PR".

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
community guidelines
LEGEND ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

There is nothing in Acrobat that shows you these defined scripts. I don't really understand what you are looking for.

The script is executed at startup. Once and once only. You cannot execute it again.

The script may define functions, including trusted functions. You can call these in any context.

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
community guidelines
New Here ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

The script may define functions, including trusted functions. You can call these in any context

Ok, so I think I correctly defined a method "main" in my function : var main = app.trustedFunction(function () { ...

So if I understand you well, scripts placed in those folders will actually simply be read at startup and Adobe will remember the function defined there, right ? Now how do I call this function ? By simply writing main ?

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
community guidelines
Adobe Community Professional ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

To test if your folder-level script is installed correctly add the following line at the top of it (OUTSIDE any function definition):

app.alert("Hi!");

When you open the application an alert message with that text should appear. If it doesn't, the script is not installed properly.

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
community guidelines
LEGEND ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

You would just call main() in any JavaScript context. Such as from the console, but more usually from an embedded JavaScript on a button or other form field.

Do you understand why trusted functions are needed? If not, this is all going to seem unnecessarily complicated.

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
community guidelines
Adobe Community Professional ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

The function main adds only the tool button.

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
community guidelines
New Here ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

I think it is not correctly installed.

What I tried to do :

- Create an Action with the Action Wizard to execute a javascript

- In the javascript, I only added main()

- open my document, Execute the javascript.

I have those errors in the debugger :

SyntaxError: illegal character

1:Folder-Level:App:config.jsException in line 23 of function top_level, script Document-Level:AddBSC

NotAllowedError: Security settings prevent access to this property or method.

App.trustedFunction:23:Document-Level:AddBSC

Exception in line 1 of function top_level, script Batch:Exec

ReferenceError: main is not defined

1:Batch:ExecException in line 1 of function top_level, script Batch:Exec

ReferenceError: main is not defined

1:Batch:ExecException in line 1 of function top_level, script Batch:Exec

AddBSC is a function I defined before that I deleted, but I guess Adobe remember it somehow ?

And main is not defined.

@TestScreenName, I'm doing it this way because when I try to simply execute the script from the open document, I have a NotAllowedError, which from what I saw online requires the script to be added in the trusted 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
community guidelines
Adobe Community Professional ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

Clear the debugger output and restart Adobe Acrobat (without open a document). What messages did you get?

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
community guidelines
LEGEND ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

Acrobat (we prefer not to call it Adobe) doesn't remember anything. If it is finding this script AFTER you have definitely quit and restarted Acrobat it is because you have left it somewhere and it's still being picked up. Perhaps in a document level script, and perhaps in startup scripts.

When dealing with errors it's best to take them from the top, rather than pick out interesting ones that seem easier to fix. Quite often, the first error will be causing the others.

So, the first error seems to be this

SyntaxError: illegal character

1:Folder-Level:App:config.jsException in line 23 of function top_level, script Document-Level:AddBSC

But please quit Acrobat and check. Check the JavaScript console BEFORE opening any files AND before running your action, so you can see exactly which errors relate to the startup script. Then open the document and see which errors, if any, now appear. Finally, run the action. You now know which part is triggering which error.

Not allowed errors are sometimes overcome by using a trusted script (Just putting a script in the startup folder does NOT however make it trusted unless you code it that way.) You can check the documentation of the method used to see what is needed. Have you done this?

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
community guidelines
New Here ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

Ah nice catch ! I changed the PDF used for the test, and AddBSC don't show anymore, so i guess I added it in the PDF somehow.

Now when I execute the script, I have this :

SyntaxError: illegal character

1:Folder-Level:App:config.js

SyntaxError: illegal character

1:Folder-Level:App:config.jsException in line 1 of function top_level, script Batch:Exec

So the script is correctly loaded, which is nice. Now I just need to debug it line by line and correct all errors. Thank you for your help

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
community guidelines
LEGEND ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

Illegal character is sometimes a coding error but more often it's an error in how the script is saved. For example, did you save it as a Word document? What encoding did you select when saving the file - if you're not sure, what did you edit it with (Notepad is recommended)?

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
community guidelines
New Here ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

I found the error. In the script that is running, the first line has a weird first character :

Imgur: The magic of the Internet

I saved it in the windows notepad as a .js . Those characters don't show up in the original file.

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
community guidelines
LEGEND ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

You probably saved it as UTF-8. Notepad adds a BOM character to tell the world it is UTF-8. Unfortunately most of the rest of the world goes "what the heck is this?"

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
community guidelines
New Here ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

What is the recommended way then ? Will Word also add the character ?

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
community guidelines
LEGEND ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

When you save AS from Notepad, note the encoding options. Most of the time ANSI 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
community guidelines
New Here ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

great, it corrected the error, thank you

I corrected another error, but now I have a

TypeError: Invalid argument type.

Util.iconStreamFromIcon:9:Folder-Level:App:config.js

on

var main = app.trustedFunction(function () {

app.beginPriv();

var iconPath = "C:\Program Files (x86)\Adobe\Acrobat DC\Acrobat\Javascripts\bcs.jpg";

var myDoc = app.newDoc();

myDoc.importIcon("bcsIcon", iconPath, 0);

var oIcon = util.iconStreamFromIcon(myDoc.getIcon("bcsIcon"));

The icon exists, and the local variable is not null and seems to have a correct path value, so I don't know what could cause this. Any 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
community guidelines
Adobe Community Professional ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

Read reply 2.

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
community guidelines
New Here ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

I have my jpg at the right place. I tried to isolate the parameter with var getIcon = (myDoc.getIcon("bcsIcon")); and the result is null value, so the script don't actually get the jpg or icon

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
community guidelines