Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Adding a button with an icon -- RangeError

Participant ,
Nov 07, 2016 Nov 07, 2016

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!

TOPICS
Acrobat SDK and JavaScript , Windows
5.2K
Translate
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
Community Expert ,
Nov 07, 2016 Nov 07, 2016

Your file-path syntax is incorrect. In Acrobat the correct path for this file would be:

"/C/Test/Home_Button.png"

Translate
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
Participant ,
Nov 07, 2016 Nov 07, 2016

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

Translate
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
Community Expert ,
Nov 07, 2016 Nov 07, 2016

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.

Translate
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
Participant ,
Nov 07, 2016 Nov 07, 2016

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!

Translate
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
Community Expert ,
Nov 07, 2016 Nov 07, 2016

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?

Translate
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
Participant ,
Nov 07, 2016 Nov 07, 2016

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

Translate
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
Community Expert ,
Nov 07, 2016 Nov 07, 2016

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.

Translate
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
Participant ,
Nov 08, 2016 Nov 08, 2016

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

Translate
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
Community Expert ,
Nov 08, 2016 Nov 08, 2016

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.

Translate
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
Participant ,
Nov 08, 2016 Nov 08, 2016

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

Translate
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
Community Expert ,
Nov 08, 2016 Nov 08, 2016

Where do you use oIcon_Home? Why do you define a trusted function inside of a trusted function?

Translate
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
Participant ,
Nov 08, 2016 Nov 08, 2016

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

Translate
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
Community Expert ,
Nov 08, 2016 Nov 08, 2016

Where do you call the document-level script "Add_Button_Main" ?

Translate
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
Participant ,
Nov 08, 2016 Nov 08, 2016

Hi Bernd,

The Add_Button_Main script is called when triggering myButtonItem(), when a document is active.

Best regards,

Roman

Translate
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
Community Expert ,
Nov 08, 2016 Nov 08, 2016

No, that is not correct. The function myButtonItem adds the script to the active document nothing else.

Translate
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
Participant ,
Nov 08, 2016 Nov 08, 2016

Doesn't the following mean that the event is triggered when a doc is loaded?

cEnable: "event.rc = (event.target != null);

Thanks,

Roman

Translate
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
Community Expert ,
Nov 08, 2016 Nov 08, 2016

No. It means the button becomes enabled when a file is open.

Translate
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
Community Expert ,
Nov 08, 2016 Nov 08, 2016

Sorry, I was wrong.

Translate
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
Community Expert ,
Nov 08, 2016 Nov 08, 2016

Why didn't you add the tool button at application start?

Translate
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
Participant ,
Nov 08, 2016 Nov 08, 2016

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

Translate
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
Participant ,
Nov 08, 2016 Nov 08, 2016

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

Translate
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
Community Expert ,
Nov 08, 2016 Nov 08, 2016

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"});

});

Translate
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
Participant ,
Nov 08, 2016 Nov 08, 2016

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

Translate
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
Community Expert ,
Nov 08, 2016 Nov 08, 2016

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?

Translate
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