17 Replies Latest reply on Jun 9, 2013 11:16 AM by rombanks

    Folder-level JS that adds a doc-level JS

    rombanks Level 1

      Hello fellows,

       

      I'm struggling with a folder-level script that should:

      1. Create a menu item under 'File'.

      2. When clicking the menu item, it should:

          a. Open a prompt, asking a user to enter an email address.

          b. Add a document-level script that will:

              -- Be executed only upon closing a PDF.

              -- Will show an alert message explaining that upon clicking 'OK', a new email message will be created.

              -- If the user clicks OK, open the email message in the default email client, with the email address already specified + subject + body

              -- If the user clicks Cancel, abort, and close the PDF

       

      The code I came up with so far (pls, see below) makes the alert message open already when running Acrobat and then the email message opens. I can't figure out how to make it open only after clicking the new menu item that should add the document-level script.

       

      Thank you for your help in advance!!!

       

      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

      app.addMenuItem({cName: "Review Msg", cParent: "File", \

      cExec: "var cTW = app.response({cQuestion: \"Please, enter the owner email:\", cTitle: \"TW\", cDefault: \"user@user.com\", cLabel: \"Email Address:\"});

       

        if (cTW == null) 

          app.alert(\"Please, try again.\");

          var cTW = app.response({cQuestion: \"Please, enter the TW email:\", cTitle: \"TW\", cDefault: \"user@user.com\", cLabel: \"Email Address:\"});

      else

       

      this.addScript('My Code', 'var docTitle = this.info.Title;\

      var c_alert = app.alert(\"If you have published your comments, please, inform the TW.\"\

      + \"After you click OK, a new email message will automatically open with the relevant recipients already specified.\");\

       

      var mail = app.mailMsg({bUI:false, cTo: 'cTW', cSubject: \"I submitted my comments for\" + docTitle, cMsg: \"Hi!\"  + \"I submitted my comments\"});\

      doc.setAction(\"WillClose\", c_alert, mail);');", cEnable: "event.rc = (event.target != null);", nPos: 0});

        • 1. Re: Folder-level JS that adds a doc-level JS
          gkaiseril MVP & Adobe Community Professional

          What happens when you run this code in the Acrobat JS console?

           

          I get the following:

           

          invalid property id

          1:Console:Exec

          SyntaxError: illegal character

          1:Console:Exec

          undefined

          • 2. Re: Folder-level JS that adds a doc-level JS
            try67 MVP & Adobe Community Professional

            You have to place the code in a function and call the function from the

            cExec parameter.

            • 3. Re: Folder-level JS that adds a doc-level JS
              MichaelN Level 3

              Hi rombanks,

               

              Try the following:

               

              app.addMenuItem({
                cName: "Review Msg",
                cParent: "File",
                cExec: "myMenuItem();"
                      });

               

              var myMenuItem = app.trustedFunction( function ()
              {
                  //your code goes here
              }

               

               

               

              Michael

              • 4. Re: Folder-level JS that adds a doc-level JS
                rombanks Level 1

                Hi GKaiseril,

                 

                Thanks for your response! What do you mean by Acrobat JS console? I only see error messages when I add this script as a doc-level script.

                 

                Thanks!

                • 5. Re: Folder-level JS that adds a doc-level JS
                  rombanks Level 1

                  Hi Gilad,

                   

                  Thank you for your suggestion! I will put the code in a function.

                   

                  Thanks again for trying to help me!

                  • 6. Re: Folder-level JS that adds a doc-level JS
                    rombanks Level 1

                    Hi Michael,

                     

                    Thank you your suggestion! If I understand correctly, "myMenuItem" should not be a variable but a function, as you specified in
                    "cExec".

                     

                    Big thanks!

                    • 7. Re: Folder-level JS that adds a doc-level JS
                      rombanks Level 1

                      Hi folks,

                       

                      After putting my code in a function, as you suggested, and after tweaking my code, I made some good progress (thank you for that!), but I still have the following issues:

                       

                      - \n is not honored and causes a problem when put after a string in \"string\".

                      - doc.setAction(\"WillClose\", c_alert, mail);  is ignored, and the alert and email message open when opening the document and not when closing.

                       

                      Here is my current version of the code:

                       

                      app.addMenuItem({cName: "Review Msg", cParent: "File", cExec: "myMenuItem()", cEnable: "event.rc = (event.target != null);", nPos: 0});

                      myMenuItem = app.trustedFunction( function()

                      {

                        cContOwner = app.response({cQuestion: 'Please, enter the content owner email:', cTitle: 'Content Owner', cDefault: 'user@user.com', cLabel: 'Email Address:'});

                          if (cContOwner == null) {

                         app.alert("Please, try again.");

                            cContOwner = app.response({cQuestion: 'Please, enter the content owner email:', cTitle: 'Content Owner', cDefault: 'user@user.com', cLabel: 'Email Address:'});

                         }

                          else

                            {

                         cTW = app.response({cQuestion: 'Please, enter the TW email:', cTitle: 'TW', cDefault: 'user@user.com', cLabel: 'Email Address:'});

                         }

                      if (cTW == null) 

                           {

                         app.alert("Please, try again.");

                            cTW = app.response({cQuestion: 'Please, enter the TW email:', cTitle: 'TW', cDefault: 'user@user.com', cLabel: 'Email Address:'});

                        }

                         this.addScript("My Code", "this.setAction(\"WillClose\", c_alert, mail); var docTitle = this.info.Title;\

                         var c_alert = app.alert(\"If you have published your comments, please, inform the content owner and the TW.\n\n After you click OK, a new email message will automatically open in Outlook with the relevant recipients already specified.\");\

                         var mail = app.mailMsg({bUI:false, cTo: cTW, cCc: cContOwner, cSubject: \"I submitted my comments for\" + docTitle, cMsg: \"Hi!\n\n I submitted my comments\"});");

                      });

                       

                      Your input is much appreciated!!!

                      • 8. Re: Folder-level JS that adds a doc-level JS
                        try67 MVP & Adobe Community Professional

                        There's no  need to escape the quotes in \"WillClose\" (and in other locations). You only need to escape quotes when they're inside other quotes of the same kind.

                        • 9. Re: Folder-level JS that adds a doc-level JS
                          rombanks Level 1

                          Hi Gilad,

                           

                          Thank you for your prompt response!

                           

                          I removed all instances of the backslash before the double-quote, but the script then stopped working completely. So, I replaced double quotes with single quotes and it started working again. However, the script still ignores the "this.setAction('WillClose', c_alert, mail)" part. Do you know why?

                           

                          Thank you again for your help!!!

                          • 10. Re: Folder-level JS that adds a doc-level JS
                            try67 MVP & Adobe Community Professional

                            Why does this function has 3 parameters? It can only have two. You should also consult the JS console to find out your errors.

                             

                            The general format of setAction is:

                            this.setAction("Name", "script");

                            If you place this command inside a string, it needs to become:

                            var myScript = "this.setAction(\"Name\", \"script\");";

                            1 person found this helpful
                            • 11. Re: Folder-level JS that adds a doc-level JS
                              rombanks Level 1

                              Hi Gilad,

                               

                              You are right. I just want to perform 2 actions: both c_alert and mail. I tried splitting them to 2 functions, but result is the same:

                               

                              this.addScript("Document Will Close", "var docTitle = this.info.Title;\

                                 var c_alert = app.alert('If you have published your comments, please, inform the content owner and the TW. After you click OK, a new email message will automatically open in Outlook with the relevant recipients already specified.');\

                                 var c_mail = app.mailMsg({bUI:false, cTo: cTW, cCc: cContOwner, cSubject: 'I submitted my comments for' + docTitle, cMsg: 'Hi! I submitted my comments'});\

                                 this.setAction('WillClose', c_alert);\

                                 this.setAction('WillClose', c_mail);");

                               

                              Any suggestions are much appreciated!!!

                              • 12. Re: Folder-level JS that adds a doc-level JS
                                try67 MVP & Adobe Community Professional

                                Why are you including the setAction commands inside addScript? Why not

                                execute them by themselves?

                                And you can't attach two scripts to 1 event. The second one will overwrite

                                the first. You need to place them all in a single command.

                                 

                                There are quite a lot of mistakes in your code, both of syntax and logical

                                ones. You might be better off hiring a professional to do it for you, and

                                then try to study from the code.

                                1 person found this helpful
                                • 13. Re: Folder-level JS that adds a doc-level JS
                                  rombanks Level 1

                                  Hi Gilad,

                                   

                                  Thank you for your response and for your guidance! I agree that it is always easier to copy and paste a script that a "Pro" wrote, but this way you will only see the "do's", but will not be aware of the "dont's". I struggle with my ugly script but at least I discover the "don'ts" this way. Of course, this is owing to such nice people like you and other guys who responded!

                                   

                                  I finally understood (owing to you ) that setAction can be called as a doc-level script and saved within the document. Previously, I thought that only addScript can do that.

                                  Basically, what I am left with in this lesson is to understand how to preserve the values of "cTW" and "cContOwner" as part of the set action.

                                   

                                  I tried:

                                  ...

                                  app.mailMsg({bUI:false, cTo: cTW.value, cCc: cContOwner.value, cSubject: \"I submitted my comments for\" + docTitle, cMsg: \"Hi! I submitted my comments\"});");

                                   

                                  but cTW.value and cContOwner.value are not converted to the values I entered in the app.response prompts.

                                   

                                  Your input will be highly appreciated!!!

                                  • 14. Re: Folder-level JS that adds a doc-level JS
                                    try67 MVP & Adobe Community Professional

                                    Without the full code it's hard to see what's going wrong.

                                    • 15. Re: Folder-level JS that adds a doc-level JS
                                      rombanks Level 1

                                      Hi Gilad,

                                       

                                      Sorry, you are right! Below is the code I came up with so far. The console shows no errors, and the "WillClose" action is set, but I cannot make the app.response pass the user input to cTW and cContOwner respectively in app.mailMsg .

                                       

                                      Here is the code:

                                       

                                      app.addMenuItem({cName: "Review Msg", cParent: "File", cExec: "myMenuItem()", cEnable: "event.rc = (event.target != null);", nPos: 0});

                                      myMenuItem = app.trustedFunction( function()

                                      {

                                        var cContOwner = app.response({cQuestion: 'Please, enter the content owner email:', cTitle: 'Content Owner', cDefault: 'user@user.com', cLabel: 'Email Address:'});

                                          if (cContOwner == null) {

                                         app.alert("Please, try again.");

                                            var cContOwner = app.response({cQuestion: 'Please, enter the content owner email:', cTitle: 'Content Owner', cDefault: 'user@user.com', cLabel: 'Email Address:'});

                                         }

                                          else

                                            {

                                         var cTW = app.response({cQuestion: 'Please, enter the TW email:', cTitle: 'TW', cDefault: 'user@user.com', cLabel: 'Email Address:'});

                                         }

                                      if (cTW == null) 

                                           {

                                         app.alert("Please, try again.");

                                            var cTW = app.response({cQuestion: 'Please, enter the TW email:', cTitle: 'TW', cDefault: 'user@user.com', cLabel: 'Email Address:'});

                                        }

                                         

                                      var c_test = 'app.alert(\"If you have published your comments, please, inform the content owner and the TW. After you click OK, a new email message will automatically open in Outlook with the relevant recipients already specified.\");\r'

                                          + 'var docTitle = this.info.Title; \r'

                                          + 'app.mailMsg({bUI:false, cTo: cTW.value, cCc: cContOwner.value, cSubject: \"I submitted my comments for\" + docTitle, cMsg: \"Hi! I submitted my comments\"});';

                                      this.setAction("WillClose", c_test);

                                      });

                                       

                                      Thanks again!!!

                                      • 16. Re: Folder-level JS that adds a doc-level JS
                                        try67 MVP & Adobe Community Professional

                                        The script that you add with setAction is not connected to the rest of your script, so any variables you define in the latter will not exist in the former. Also, the response of app.alert is just an integer, and as such has not "value" property.

                                         

                                        • 17. Re: Folder-level JS that adds a doc-level JS
                                          rombanks Level 1

                                          Hi Gilad,

                                           

                                          Thanks for your response!

                                           

                                          What do you mean exactly by "not connected"? It is part of the same function (myMenuItem()).

                                          "the response of app.alert is just an integer" = did you mean "app.response"?

                                           

                                          Thanks!