8 Replies Latest reply on Aug 17, 2014 5:04 AM by Test Screen Name

    How to invoke a plug-in menu item via Javascript?

    dedswa

      Hi, I installed a plug-in on my Acrobat Pro v11, it shows a plug-in menu, and there's one menu item in this plug-in menu dropdown. I can manually select that menu item and it does what it's designed to do.

       

       

      My question is, how do I use javascript to invoke it, to achieve the same result as when I manually select that menu item?

       

       

      I tried using this "app.execMenuItem("name-of-menu-item");" under my click event of a button on the PDF form, but it didn't work.

       

       

      What have I done wrong? How to make it work?

       

       

      TIA

        • 1. Re: How to invoke a plug-in menu item via Javascript?
          George_Johnson MVP & Adobe Community Professional

          Did you read in the Acrobat JavaScript reference about this method and its security restrictions? Does it work if you execute it in the interactive console? How about if you select: Edit > Preferences > JavaScript > Enable menu items JavaScript execution privileges

          • 2. Re: How to invoke a plug-in menu item via Javascript?
            Test Screen Name Most Valuable Participant

            You have to use the internal name of the menu item, too. Refer to the plug-in source code to find that. It is unlikely to be what you see on screen.

            • 3. Re: How to invoke a plug-in menu item via Javascript?
              dedswa Level 1

              Thanks to all those who responded.

               

              Actually I got it working soon after I posted the question.

              However, I don't know, my trial&error way seems to be a little different from all those documents I read. 

              So, I am not exactly sure if it's the correct way to do this sort of thing or not.

              It certainly is not very end-user friendly IMHO.  

               

              I will describe what I did below, if you know of a better way, please let me know, thank you.

               

              First, I put a folder-level script in the Acrobat install folder, the so-called "app" level;

              because when I used the getPath("user", "javascript") call, it returned an error without giving me the user-level path. 

               

              Anyway, my script (the .js file) looks like this:

              xyz = app.trustedFunction(

                           function() {

                            app.beginPriv();

                            app.execMenuItem("ADBE:myxyz");

                            app.endPriv();

                            return;

                            }

                       );

               

              Then, on my form's button-click event, I make a call

                xyz();

               

              and that got it to invoke the menu item "myxyz" successfully.

               

              ----

              But... I can't say I like my solution:

              1) I don't like the .js file, because I'll have to put it in all my users' Acrobat install folders.

                   Extremely inconvenient.

                   Is there a way to make it work without using the folder level script???

              2) The script itself, I am not sure if it makes sense or not.

                   I can't say I understand all these trustFunction and Security stuff;

                    it just so happened that it worked for me, but I don't know why.

                    so, maybe there could be a simpler script that can also work?

              3) If I put the "beginPriv()" , "execMenuItem()" and "endPriv()" lines directly in the Button-click event,

                    I got an error message in the Acrobat console, something like:  "your security settings does not allow....";

                    in fact, I tried many many different ways to write this Button-click script, none of them worked without the folder-level .js file.

                    But I hope somebody could come up with a way to make it work without the folder-level script file....

                    Is there a way to change the "security settings" without using the .js file?

              4) The execMenuItem is also a headache, sorry about complaining... but... sometimes it just silently exited without doing anything,

                    and without returning any error messages... how am I supposed to know what went wrong when it's so quiet?!

                     Furthermore, why does it insist on taking the full name of the menu item?  I mean, why do I must put the "ADBE:" part in there?

                    there's no other menu item named "myxyz", right?

                    I remember Adobe's founders once said that "making our customers' lives easier" was what drove them to work everyday.

                    execMenuItem() certainly didn't listen to what they said.

              • 4. Re: How to invoke a plug-in menu item via Javascript?
                George_Johnson MVP & Adobe Community Professional

                Your folder-level script is correct. The reason it didn't give you anything when you tried to get the user JavaScript folder is because you need to create it manually, on Windows at least. More info: The Acrobat Ninja: Acrobat 10.1.1 JavaScript changes

                 

                They are not trying to make it easier on developers, but they have good reasons that are security related. If a script in a document could execute any menu item without restriction, it could at least be an annoyance and a big security hole at most. Security of their applications will always win out over your convenience.

                 

                A user can choose to allow this JavaScript method, either by installing a folder-level script like you have, by disabling the restriction in their preferences, by modifying a white list in the Windows registry (I think), or by trusting your digital certificate to allow for privileged JavaScript to be executed without restriction. So whichever method is used to bypass this restriction, a user has to make a conscious decision to allow it.

                 

                This latter method gives you a way to include the app.execMenuItem alone in a button action. You will have to digitally certify the document and convince your users to add your certificate to their list of trusted certificates and trust it to allow privileged JavaScript, at least.

                • 5. Re: How to invoke a plug-in menu item via Javascript?
                  try67 MVP & Adobe Community Professional

                  Is there a way to make it work without using the folder level script???

                  Only if you add this command to the White-List of items that can be executed without the trusted context. However, this requires a registry change.

                   

                  sometimes it just silently exited without doing anything, and without returning any error messages... how am I supposed to know what went wrong when it's so quiet?!

                  You're not. This method is a convenience tool that basically does the same as clicking a menu/toolbar icon. That action doesn't "return" anything, it's just mimicking GUI interaction, in a way. Annoying, perhaps, but that's the way it is.

                   

                  why does it insist on taking the full name of the menu item?  I mean, why do I must put the "ADBE:" part in there? there's no other menu item named "myxyz", right?

                  Maybe there is and maybe there isn't, but "ADBE:" is a part of the item's ID and you have to specify the full and exact ID for it to work correctly. This is the same with any computer language in the world.

                   

                  Keep in mind there's a big difference between "developers" and "customers". As a customer I would be pissed off if a PDF file could start doing all kinds of weird things on my computer just by opening it (printing itself, opening other files, saving itself in all kinds of locations, performing other tasks), without my explicit consent. Not to mention the security risks involved... As a developer it annoys me that I have to go through loops to achieve what I'm after, sure, but it's a small price to pay for the benefits in security and user trust. If users don't trust that the code embedded in the files is safe they'll switch it off entirely, or stop using that file format.

                  • 6. Re: How to invoke a plug-in menu item via Javascript?
                    Test Screen Name Most Valuable Participant

                    There is a particular reason for the ADBE:

                     

                    Every menu has a string you see on screen, and which beginning developers assume is the menu name. And every menu has a name. This name is created by the programmer who made the menu. A rule (sometimes broken) is that the menu name starts with a four letter code, assigned by Adobe, and private to the company making the menu.

                     

                    Two benefits from this:

                    1. If two companies add a menu with the same string they will have a different name. We can well imagine that two companies might add menu items with a string "Reset" for example. These will not be confused.

                    2. If the end user changes language, the strings will change. But the name will stay the same. That way JavaScript automation keeps working, even if the language changes, without the JavaScript developer needing to buy and test all possible languages.

                     

                    Adobe follow their own rules, so their menu item names begin ADBE: You cannot guess these names by looking at the strings on screen, no matter how often a guess might be right. You must refer to the internal names.

                    • 7. Re: How to invoke a plug-in menu item via Javascript?
                      try67 MVP & Adobe Community Professional

                      You must refer to the internal names.

                      Correct. And that's very easy to do since there are built-in methods that will print out all of them for you: app.listMenuItems() and app.listToolbarButtons()

                      • 8. Re: How to invoke a plug-in menu item via Javascript?
                        Test Screen Name Most Valuable Participant

                        Actually, one more point. Adobe might (somewhere) claim to be making it easy for end users, but the second you start writing JavaScript you are a developer and all claims of things being easy or simple are null and void. You need to be prepared to scour documents of thousands of pages and deal with barely documented changes. That's the job.