5 Replies Latest reply on Nov 4, 2014 9:37 AM by Glendan

    JavaScript:  How do you pass the currently active doc to a dialog in a menu item?

    Glendan

      I'm using Adobe Acrobat 11.0 Standard on Windows 7, and I'm having trouble with the menu item that I'm creating.  The menu item calls a dialog that prompts the user to select a page in the currently active document.  The problem is, I can't figure out how to pass the currently active document to the dialog.  I scoured the rest of the documentation, did searches on Google and here in the Adobe forums, but haven't turned anything up.  The AcroJS API gives the following solution (found under app/methods/execDialog):

       

      This function attaches the Doc object to the dialog box, then passes the dialog box to the app.execDialog method. The dialog4 object and this function can be at the document level.

         function dotheDialog(dialog,doc)

          {

              dialog.doc = doc;

              var retn = app.execDialog( dialog )

          }

      Finally, the following script can be executed from a mouse-up action, for example.

          dotheDialog( dialog4, this );

       

      I did this exact thing, but in the dialog, the value of this.numPages still comes out as undefined, indicating that the current document still hasn't been passed.  My own code looks like this:

        function dotheDialog(dialog, thisDoc){

          dialog.doc = thisDoc;

          var retn = app.execDialog(dialog);

        }

       

       

        app.addMenuItem({

          cName: "Test Dialogue Box",

          cParent: "Edit",

          cExec: "dotheDialog(templateDialogue, this);"

        });

      where templateDialogue is the name of my own dialog, rather than dialog4.  I'm not sure what the retn variable that they declare in the documentation is for, but I've tried it with and without that part, and it doesn't seem to make a difference.  Does anyone know how to do this?

        • 1. Re: JavaScript:  How do you pass the currently active doc to a dialog in a menu item?
          try67 MVP & Adobe Community Professional

          You need to share the dialog code as well.

           

          The return value is used to verify that the user pressed OK to close the dialog, or something else, before moving on to other code. If they pressed Cancel if you might want to abort the code... But if you don't care about that then you don't really need it.

          • 2. Re: Re: JavaScript:  How do you pass the currently active doc to a dialog in a menu item?
            Glendan Level 1

            Sorry, here it is.  It's also just a small modification of something directly from the API, which is why it still has the date bit in it.

             

            var templateDialogue = {
            
              initialize: function (dialog) {
                // Create a static text containing the current date.
                var todayDate = dialog.store()["date"];
                todayDate = "Date: " + util.printd("mmmm dd, yyyy", new Date());
                dialog.load({ "date": todayDate });
              },
            
              commit:function (dialog) {    // called when OK pressed
            
                var results = dialog.store();
                var inBounds = (parseInt(results["tPag"]) < this.numPages);
               
                if(!inBounds){
                  app.alert({
                    cMsg: "Please enter a valid number for the page from which to make the template." +
                    "\n" + parseInt(results['tPag']) + " = parseInt(results['tPag']) !< this.numPages = " +  // I put this in to see the value of this.numPages,
                    this.numPages,                                                                           // and it continues to be undefined.
                    cTitle: "Page out of Bounds",
                    nIcon: 0, nType: 0
                  });
                }else{
                  var nButton = app.alert({
                    cMsg: "Your template is named " + results["tNam"] + " and is made from page " + results["tPag"],
                    cTitle: "javascript",
                    nIcon: 3, cType: 0
                  });
                };
              },
            
              description:    {
               
                name: "Personal Data",    // Dialog box title
                align_children: "align_left",
                width: 350,
                height: 200,
            
                elements:
                [
                  {
                    type: "cluster",
                    name: "Add a page to this documents templates.",
                    align_children: "align_left",
                   
                    elements:
                    [
                      {
                        type: "view",
                        align_children: "align_row",
            
                        elements:
                        [
                          {
                            type: "static_text",
                            name: "Template Name: "
                          },
                         
                          {
                            item_id: "tNam",
                            type: "edit_text",
                            alignment: "align_fill",
                            width: 300,
                            height: 20
                          }
                        ]
                      },
            
                      {
                        type: "view",
                        align_children: "align_row",
                        elements:
                        [
                          {
                            type: "static_text",
                            name: "Generating Page: "
                          },
            
                          {
                            item_id: "tPag",
                            type: "edit_text",
                            alignment: "align_fill",
                            width: 300,
                            height: 20
                          }
                          ]
                      },
            
                      {
                        type: "static_text",
                        name: "Date: ",
                        char_width: 25,
                        item_id: "date"
                      },
                    ]
                  },
            
                  {
                    alignment: "align_right",
                    type: "ok_cancel",
                    ok_name: "Ok",
                    cancel_name: "Cancel"
                  }
                ]
              }
            };
            

             

            Side Question:  Out of curiosity, how would you abort if the user clicks cancel?  I get the feeling that they don't cover it because it's something you're supposed to already know how to do, but...I don't.  Would you just put whatever action you want to take into an if ( button == "Ok" ) type statement so that cancel does nothing?

            • 3. Re: Re: JavaScript:  How do you pass the currently active doc to a dialog in a menu item?
              try67 MVP & Adobe Community Professional

              The reference to the document is a property of the dialog, so you access it from inside it like this:

              this.doc

              And if you want to access the number of pages, like this:

              this.doc.numPages

              • 4. Re: Re: JavaScript:  How do you pass the currently active doc to a dialog in a menu item?
                try67 MVP & Adobe Community Professional

                Regarding your other question:

                 

                The code that actually does the work needs to be located outside the dialog object. You then use the return value of the dialog to check what the user clicked on. If they clicked on "ok" you continue. If not then you don't do anything.

                • 5. Re: Re: JavaScript:  How do you pass the currently active doc to a dialog in a menu item?
                  Glendan Level 1

                  That's it; it works now!  Thank you very much for the help!