Skip to main content
Participating Frequently
December 4, 2018
Question

How to execute a Trusted script on the open document ?

  • December 4, 2018
  • 20 replies
  • 3099 views

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

This topic has been closed for replies.

20 replies

Participating Frequently
December 5, 2018

try67​ you were right, I tried changing the function's name and restarting everything, but I get :

main()

Exception in line 17 of function main<, script Folder-Level:App:config.js

GeneralError: Operation failed.

App.addToolButton:17:Console undefined:Exec

undefined

with the following code :

app.addToolButton({

cName: "BCSadd",

oIcon: oIcon,

cLabel: "Finish PR",

cEnable: "event.rc = (app.doc != null);",

cExec: "BCSaddFunction();",

nPos: 0

});

(I tried removing the Icon line but it still trigger the error)

So I guess this does not see the BCSaddFunction, but as you can see here : Imgur: The magic of the Internet   The function exists :/ Do you have an idea ?

try67
Community Expert
Community Expert
December 5, 2018

Try narrowing it down. Remove all other code except for the addToolButton command. Does it still not work?

This code worked fine for me:

app.addToolButton({

cName: "BCSadd",

cLabel: "Finish PR",

cEnable: "event.rc = (app.doc != null);",

cExec: "BCSaddFunction();",

nPos: 0

});

Edit: Fixed wrong method name...

Legend
December 5, 2018

And the error is?

Participating Frequently
December 5, 2018

oh right ! thank you, it's corrected

And god damn this is complicated... i now have an error in

app.addToolButton({

cName: "BCSadd",

oIcon: oIcon,

cLabel: "Finish PR",

cEnable: "event.rc = (app.doc != null);",

cExec: "BCSadd();",

nPos: 0

});

The arrow is on nPos: 0 , but I guess it can be anywhere in the function.

Would it be possible that this fail because the function BCSadd defined after is not correctly called ? It is written under the script : Imgur: The magic of the Internet

try67
Community Expert
Community Expert
December 5, 2018

I think the issue is that you have a function named BCSadd as well a menu item with that name. Try changing one of them...

And yes, this is complicated stuff, which is why it's very important to read the documentation carefully and keep an eye on the small details and the error messages you're getting.

try67
Community Expert
Community Expert
December 5, 2018

PS. In the future, please post the error message as text, in full, not as an image.

Participating Frequently
December 5, 2018

I use :

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

var myDoc = app.newDoc();

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

When I copypaste it into the file browser, it opens the icon correctly so I guess it works

Karl Heinz  Kremer
Community Expert
Community Expert
December 5, 2018

Look at try67's first comment about the format of the path. You are using Windows syntax for the path, which will not work in an Acrobat JavaScript. You need to use Adobe's device independent format.

Participating Frequently
December 5, 2018

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

Bernd Alheit
Community Expert
Community Expert
December 5, 2018

Do you use the correct syntax for the path?

Participating Frequently
December 5, 2018

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 ?

Bernd Alheit
Community Expert
Community Expert
December 5, 2018

Read reply 2.

Legend
December 5, 2018

When you save AS from Notepad, note the encoding options. Most of the time ANSI will work.

Participating Frequently
December 5, 2018

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

Legend
December 5, 2018

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

Participating Frequently
December 5, 2018

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.