Copy link to clipboard
Copied
Hello folks,
I am working on a script that adds a custom button. Via a privileged function triggered thru a menu item, I can add a document-level script that creates this button.
However, when I try adding an icon to the button, the debugger throws an error:
RangeError: Invalid argument value.
Doc.importIcon:1:Document-Level:Add_Button
Here is my document-level script:
this.importIcon("HomeIcon", "C:\\Test\\Home_Button.png", 0);
var oIcon_Home = util.iconStreamFromIcon(this.getIcon("HomeIcon"));
app.addToolButton({cName: "Home", cExec: "app.alert('Test Message')", cEnable: "event.rc = (event.target != null);", cTooltext: "Home Screen"});
I suspect, Acrobat fails to access the C:\Test folder to create a PDF out of the icon file for some reason. Please, advise!
Thank you in advance!
Copy link to clipboard
Copied
Your file-path syntax is incorrect. In Acrobat the correct path for this file would be:
"/C/Test/Home_Button.png"
Copy link to clipboard
Copied
Hi Gilad,
The first thing I tried was:
"/C/Test/Home_Button.png"
It didn't work. So I've changed it to the Win notation.
BTW, the second/third lines should be (omitted the oIcon):
app.addToolButton({cName: "Home", oIcon: oIcon_Home, cExec: "app.alert('Test Message')", cEnable: "event.rc = (event.target != null);", cTooltext: "Home Screen"});
Best regards,
Roman
Copy link to clipboard
Copied
From the documentation of importIcon:
Note: If cDIPath is specified, this method can only be executed during a batch or console event. See
Privileged versus non-privileged context for details. The event object contains a discussion of
Acrobat JavaScript events.
This means that if you want to use this method from the doc-level and to specify the file-path of the image, it needs to be backed by a trusted folder-level script.
Copy link to clipboard
Copied
Hi Gilad,
Thank you for your response!
If I understood correctly, that's what I did. The document-level script is injected from the folder-level script that contains an app.trustedFunction that adds the menu item and adds the button when the menu item is pressed.
Please, confirm that I understood you right.
Thank you!
Copy link to clipboard
Copied
You wrote this was a document-level script, and that's what the error message says as well... So why do you say now it's injected from a folder-level script?
Copy link to clipboard
Copied
My main script sits in the Acrobat JS folder and loads a menu item when Acrobat is opened. When this menu item is pressed, it injects a doc-level script that is supposed to build a button. It works pretty well w/o an icon. But when I add the icon import and parsing code, Acrobat still adds the code to the doc but throws an error and doesn't load the icon.
Best regards,
Roman
Copy link to clipboard
Copied
That was not clear from your original post. Please post your full code, specifying exactly which code is located in a folder-level script and which in a doc-level script.
Copy link to clipboard
Copied
Hi Gilad,
The whole code appears in a folder-level script.
Here is my code:
app.addMenuItem({cName: "Add Button", cParent: "File", cExec: "myButtonItem()", cEnable: "event.rc = (event.target != null);", nPos: 0});
myButtonItem = app.trustedFunction( function()
{
this.addScript("Add_Button", "this.importIcon(\"HomeIcon\", \"/C/Test/Home_Button.png\", 0); var oIcon_Home = util.iconStreamFromIcon(this.getIcon(\"HomeIcon\")); "app.addToolButton({cName: \"Button\", cExec: \"app.alert('Someone pressed me!')\", cEnable: \"event.rc = (event.target != null);\", cTooltext: \"Button\"});");
});
I don't understand what range the error refers to:
RangeError: Invalid argument value.
Best regards,
Roman
Copy link to clipboard
Copied
The code might appear in a folder-level script, but you're adding a doc-level script that tries to use importIcon with a path... That's not going to work. Unless you can have a folder-level trusted function that executes importIcon with these parameters installed on every computer where this file will be used, you can't do it.
Copy link to clipboard
Copied
Hi Gilad,
Thanks for your response!
Do you mean something like the following? By the way, is there a way to add a separator between buttons if more than one button appears and change the button Cname font weight/color?
app.addMenuItem({cName: "Add Button", cParent: "File", cExec: "myButtonItem()", cEnable: "event.rc = (event.target != null);", nPos: 0});
myButtonItem = app.trustedFunction( function()
{
myButton = app.trustedFunction( function add_btn()
{this.importIcon("HomeIcon", "/C/Test/Home_Button.png", 0);
var oIcon_Home = util.iconStreamFromIcon(this.getIcon("HomeIcon"));
this.addScript("Add_Button_Main", "app.addToolButton({cName: \"Button\", oIcon: oIcon_Home, cExec: \"app.alert('Test Message')\", cEnable: \"event.rc = (event.target != null);\", cTooltext: \"Button\"});");});
this.addScript("Add_Button", "app.addToolButton({cName: \"Button\", cExec: \"myButton()\", cEnable: \"event.rc = (event.target != null);\", cTooltext: \"Button\"});");
});
Thanks again,
Roman
Copy link to clipboard
Copied
Where do you use oIcon_Home? Why do you define a trusted function inside of a trusted function?
Copy link to clipboard
Copied
Hi,
- oIcon_Home is called within the addToolButton function. However, it seems to be a local variable and it's not accessed by app.addToolButton. Please, advise!
- Right, I removed the 2nd trusted function. I thought the importIcon operation requires its own trusted function.
This is the updated code.
app.addMenuItem({cName: "Add Button", cParent: "File", cExec: "myButtonItem()", cEnable: "event.rc = (event.target != null);", nPos: 0});
myButtonItem = app.trustedFunction( function()
{
this.importIcon("HomeIcon", "/C/Test/Home_Button.png", 0);
var oIcon_Home = util.iconStreamFromIcon(this.getIcon("HomeIcon"));
this.addScript("Add_Button_Main", "app.addToolButton({cName: \"Button\", oIcon: oIcon_Home, cExec: \"app.alert('Test Message')\", cEnable: \"event.rc = (event.target != null);\", cTooltext: \"Button\"});");
});
Thanks,
Roman
Copy link to clipboard
Copied
Where do you call the document-level script "Add_Button_Main" ?
Copy link to clipboard
Copied
Hi Bernd,
The Add_Button_Main script is called when triggering myButtonItem(), when a document is active.
Best regards,
Roman
Copy link to clipboard
Copied
No, that is not correct. The function myButtonItem adds the script to the active document nothing else.
Copy link to clipboard
Copied
Doesn't the following mean that the event is triggered when a doc is loaded?
cEnable: "event.rc = (event.target != null);
Thanks,
Roman
Copy link to clipboard
Copied
No. It means the button becomes enabled when a file is open.
Copy link to clipboard
Copied
Sorry, I was wrong.
Copy link to clipboard
Copied
Why didn't you add the tool button at application start?
Copy link to clipboard
Copied
It's only relevant when a file is open.
My current problem is that the icon bit stream is not loaded to the addToolButton function.
Maybe, Acrobat doesn't know how to switch to /C/Test/? Should I put the icon in the Acrobat Javascript folder?
Thanks,
Roman
Copy link to clipboard
Copied
BTW, the toolbar that shows up if I don't specify the icon is not dockable, which means it was created by an nonprivileged script. But how come? I called it from a trusted function???
Thanks,
Roman
Copy link to clipboard
Copied
You can use following at folder-level:
myButtonItem = app.trustedFunction( function()
{
this.importIcon("HomeIcon", "/C/Test/Home_Button.png", 0);
var oIcon_Home = util.iconStreamFromIcon(this.getIcon("HomeIcon"));
app.addToolButton({cName: "Button", oIcon: oIcon_Home, cExec: "app.alert('Test Message')", cEnable: "event.rc = (event.target != null);", cTooltext: "Button"});
});
Copy link to clipboard
Copied
Hi Bernd,
This is very bad news as I won't be able to make the button available as part of the document.
BTW, the code above doesn't work either.
Throws the following error:
GeneralError: Operation failed.
App.addToolButton:13:Menu Add Buttons:Exec
Copy link to clipboard
Copied
Why not save yourself a lot of problems and just add the button to the document itself, instead of using a floating toolbar, or just drop the icon and use a generic button?
Find more inspiration, events, and resources on the new Adobe Community
Explore Now