7 Replies Latest reply on Oct 29, 2011 7:50 PM by SI_MSD7

    How to terminate remaining Actions in a script sequence if the user presses "Cancel" during SaveAs

    SI_MSD7 Level 2

      Hello,

      I'm using the following script to email a completed form to a pre-programmed email address and would like a way to cancel remaning in-line scripts if the user cancels teh first Step.

       

      Basically, after completing the form I cereated, the user would click the submit button and a series of actions will take place:

       

       

      1. Execute a menu item: SaveAs giving the user a chance to Save the current state of the form.

       

      [Here is where I need a Arcobat script that terminates all remaning actions if the user cancels the SaveAs action]

       

       

      2. Once the form has been saved (and not canceled), the next action involves prepping and generating an email to send the form using the user's default email client:

       

       

      var e = this.getField("ccAdd");  //There is a field on the form that enables the user to enter a CC: address, if applicable

       

       

      var cCCAddr = e.value;

       

       

         var cSubLine = "Subject goes Here"; //preformatted subject

         var cBody = "File attached." // preformatted Body text

       

       

         // Build the e-mail URL

         var cEmailURL = "mailto:email@emailaddress.com?cc=" + cCCAddr

                       + "&subject=" + cSubLine

                       + "&body=" + cBody;

       

      //Before sending, set ALL form fields to "readonly"

      for (var i = 0; i < this.numFields; i++) {

          var fname = this.getNthFieldName(i);

          var f = this.getField(fname);

              f.readonly = true;

      }

       

      // hide submit display button, so that it doesn’t show on “readonly” version

      var h = getField("Button1");

      h.display = display.hidden;

       

      //Generate the outgoing email using the user’s default email client. Send attachment as a PDF

         this.submitForm({cURL: cEmailURL, cSubmitAs:"PDF", cCharSet:"utf-8"});

       

       

      3. Once the form has been emailed, another SaveAs menu item fires, thereby, allowing the user to save the "readonly" version.  So now, he has a clean "open version"  that he saved before Step #2, and a "readonly" version after step #2.

       

       

       

      The only thing that's causing an issue now is if the user presses Cancel after the Step #1  (where he/she is asked to save the form), the remaining scripts in-line continue to fire, alougth the "Cancel" button was pressed. 

      SO, I need a script that terminates all remaining scripts (i.e. end process) if the user hits cancels during Step #1, thereby preventing step 2 from starting.

       

       

      Any ideas? Thanks in advance.

       

      S

        • 1. Re: How to terminate remaining Actions in a script sequence if the user presses "Cancel" during SaveAs
          dtm-1979 Level 1

          i think this is a thom parker type question personally, it's delving into the unknown!  i came across another article on acrobatusers about exception handling, but doesn't delve into catching exceptions/rc from GUI dialogs.

           

          i had an idea, posted it on the original thread where this question started . . .

           

          maybe just have app.alerts with yes/no for all three actions . . .

           

          • do you wish to save an editable form?
          • do you wish to email the file now?
          • do you wish to save the read-only form?

           

          that way, the script only executes as it needs to, and the user gets control over what the form is doing.

           

          (and you get control over which code is executed).

          • 2. Re: How to terminate remaining Actions in a script sequence if the user presses "Cancel" during SaveAs
            dtm-1979 Level 1

            i knew it was a thom parker question!  found this article:

            http://acrobatusers.com/tutorials/2008/06/executing_menu_items_from_javascript

             

            Which states:

            The app.execMenuItem() function is non-blocking, which means code execution continues whether or not the functionality run by the menu item has completed. Acrobat runs the menu item and then immediately proceeds to the next line of code in the script. If the code needs the result of whatever it was that the menu did, then a scheme has to be devised to detect when the menu item’s functionality is complete. This is not easy, and usually involves setting up some kind of timeout function that waits a specified amount of time before testing for the result.

             

            one might assume that adding the action manually to a button might adopt the same behaviour.

            1 person found this helpful
            • 3. Re: How to terminate remaining Actions in a script sequence if the user presses "Cancel" during SaveAs
              SI_MSD7 Level 2

              That makes alot of sense re: the app.execMenuItem() being non-blocking. Thanks for that info...it will help steer me in a differnet direction.

              • 4. Re: How to terminate remaining Actions in a script sequence if the user presses "Cancel" during SaveAs
                dtm-1979 Level 1

                ok, i've had a play with it . . . and come up with two options, one that breaks attempts to break any code being run if the cancel button is hit.  either way, they need to be added into a document level javascript, and applies the required action to your submit button (trust me, this becomes a lot easier than applying script to buttons directly!)

                 

                i just wrote functions that chain, so the first function calls the second, and the second calls the third.  seemed easier than trying to nest them.  i know the finaliseForm function simply calls the first in the chain, but that's just a matter of personal preference, i prefer to define functions with logical names.  also, that way, you can test each function individually, or all together, and write more code to the finaliseForm function if you need to.

                 

                not sure if you're using acrobat 9 or X, but the document level javascript is in two different places.  in 9 it's under advanced - document processing - document javascripts.  just give it a name, add it, remove any lines in the window that appear and paste in this code.

                 

                so here's the one that continues the script, with prompts, even when the no button is hit. let me know how you go!

                 

                function saveasEditable() {
                    //prompt the user to continue save as
                    var saveasPrompt = app.alert({
                                cMsg : "Do you want to save an editable version of this file before emailing?",
                                cTitle : "Save As Editable Form",
                                nIcon : 2,
                                nType : 2
                            });
                    //where 4 is yes and 3 is no
                    if (saveasPrompt == 4) {
                        app.execMenuItem("SaveAs");
                        emailForm();
                    } else {
                        emailForm();
                    }
                }
                
                function emailForm() {
                    //and invoke a second alert to prompt emailing the form
                    var emailPrompt = app.alert({
                                cMsg : "Do you wish to email the form now?",
                                cTitle : "Submit form via email",
                                nIcon : 2,
                                nType : 2
                            });
                    if (emailPrompt == 4) {
                        var e = this.getField("ccAdd"); //There is a field on the form that enables the user to enter a CC: address, if applicable
                        var cCCAddr = e.value;
                        var cSubLine = "Subject goes Here"; //preformatted subject
                        var cBody = "File attached."// preformatted Body text
                            
                            // Build the e-mail URL
                            var cEmailURL = "mailto:email@emailaddress.com?cc=" + cCCAddr
                             + "&subject=" + cSubLine
                             + "&body=" + cBody;
                        
                        //Before sending, set ALL form fields to "readonly"
                        for (var i = 0; i < this.numFields; i++) {
                            var fname = this.getNthFieldName(i);
                            var f = this.getField(fname);
                            f.readonly = true;
                            
                        }
                        // hide submit display button, so that it doesn’t show on “readonly” version
                        var h = this.getField("Button1");
                        h.display = display.hidden;
                        //Generate the outgoing email using the user’s default email client. Send attachment as a PDF
                        this.submitForm({
                                cURL : cEmailURL,
                                cSubmitAs : "PDF",
                                cCharSet : "utf-8"
                            });
                        saveasReadonly();
                    } else {
                    }
                    
                }
                
                function saveasReadonly() {
                    //and invoke a third alert to prompt save of the read only form
                    var readonlyPrompt = app.alert({
                                cMsg : "Do you wish to also save a read-only copy of this file?",
                                cTitle : "Save As Read-only Copy?",
                                nIcon : 2,
                                nType : 2
                            });
                    if (readonlyPrompt == 4) {
                        app.execMenuItem("SaveAs");
                        
                    } else {}
                }
                
                function finaliseForm() {        saveasEditable();
                    }
                
                //assign all of this to the submit button from the doc level
                this.getField("Button1").setAction("MouseDown", "finaliseForm();");
                
                • 5. Re: How to terminate remaining Actions in a script sequence if the user presses "Cancel" during SaveAs
                  SI_MSD7 Level 2

                  Dang! You are freakin AWESOME... Thank you so much for this... I can't wait to try it out.  Man, I've learned so much in such a short amount of time. Everything you've done makes perfect sense. And your method of coding is very clean, straightforward, and easy to follow. I now see what you mean about adding the code to document level vs attaching it to a button, The button now only contains a single line of code -calling the finaliseForm function - and all the other gunk is behind the scenes.

                   

                  I will try this out as soon as I can, and wil let you know how it goes.

                   

                  P.S. That's for the info re: the location of the document level JavaScript...never knew that sucker even existed...but then again, I rarely work in Acrobat.

                  • 6. Re: How to terminate remaining Actions in a script sequence if the user presses "Cancel" during SaveAs
                    SI_MSD7 Level 2

                    PS. Correction: I now understand the button do NOT conatin any code. All of the above script is to be contained inside doc level script, and the code itself "listens for" the button click based on the Button name, this starts the function.

                     

                    Obviously, if there is no such button with that "specific name", this script would not work. I guess having the Button1 point to finaliseForm();  could be advantageous. In eitehr approach, the form should be tested before being relaese so such errors should be caught.

                     

                    S

                    • 7. Re: How to terminate remaining Actions in a script sequence if the user presses "Cancel" during SaveAs
                      SI_MSD7 Level 2

                      The script you provided worked as intended. Thanks again!