6 Replies Latest reply on Sep 14, 2011 12:34 AM by PihlDK

    Check user input

    mp-landbjorn

      Have this small script (my first attempt in Scripting), which is loaded with Acrobat 9 at startup.

       

      app.addMenuItem({ cName: "Gem &KMS-skrivelse", cParent: "File", cExec: "SaveAs('registreringsmeddelelse', 'produkter/')", nPos: 3 });
      app.addMenuItem({ cName: "Gem &dokument i sag", cParent: "File", cExec: "SaveAs('', '')", nPos: 4 });

       

      function SaveAs(cName, cDirectory)
      {
          //Get full year
          var date=new Date();
          cYear = date.getFullYear();

       

          //Get year, case number and file name from user input
          var cYear = app.response({ cQuestion: "Indtast sagsår:", cTitle: "Sagsår", cDefault: cYear });

          var cCase = app.response({ cQuestion: "Indtast sagsnr.:", cTitle: "Sagsnr."});
          var cFile = app.response({ cQuestion: "Indtast filnavn (uden .pdf):", cTitle: "Filnavn", cDefault: cName });

       

          if (cFile == null)
               return;

       

          //Concatenate path string
          var cPath = "/h/digi/s" + cYear + "/" + cYear + "-" + cCase + "/" + cDirectory + cFile + ".pdf";

       

          //Try to save the document, if success, inform the user where to
          try {
              this.saveAs({ cPath: cPath, bPromptToOverwrite: true });
              app.alert("Dokumentet blev gemt i " + cPath, 3);
          }
          catch(e) {
              app.alert("Kunne ikke gemme dokument i " + cPath + ". Kontroller at mappen er oprettet, og at du har skriverettigheder til denne, samt at Acrobat/Reader er sat korrekt op.");
          }

       

          //So long and thanks for all the fish
          return;
      }

       

      Based on user input it saves documents to folders on a network share.

       

      It  works just fine, but if the user cancel the input dialog, null is  returned, and eventually will be part of the filename. So I use a if  statement to check if cFile is null, and if true just returns the  function. But this fails with this error message:

       

      GeneralError: Handlingen mislykkedes.

      Root.(null):17:Menu Gem &dokument i sag:Exec

      script terminated

       

      Why is this so?

       

      Best regards
      mp

        • 1. Re: Check user input
          BarlaeDC Level 4

          Hi,

           

          if you changed your code a little it may be avoidable

           

          if (cFile != null)

          {        

              //Concatenate path string
              var cPath = "/h/digi/s" + cYear + "/" + cYear + "-" + cCase + "/" + cDirectory + cFile + ".pdf";

           

              //Try to save the document, if success, inform the user where to
              try {
                  this.saveAs({ cPath: cPath, bPromptToOverwrite: true });
                  app.alert("Dokumentet blev gemt i " + cPath, 3);
              }
              catch(e) {
                  app.alert("Kunne ikke gemme dokument i " + cPath + ". Kontroller at mappen er oprettet, og at du har skriverettigheder til denne, samt at Acrobat/Reader er sat korrekt op.");
              }

          }

           

          this should allow the code to complete as expected without saving the document if cFile is null.

           

          Hope this helps

           

          Malcolm

          • 2. Re: Check user input
            mp-landbjorn Level 1

            Thanks, Malcolm.

             

            But it still doesn't work. I don't know if it is my Acrobat installation that is unable to run the script, or I am doing it wrong (guess it's me).

             

            Look at this small script:

             

            app.addMenuItem({ cName: "Null Check", cParent: "File", cExec: "NullCheck()", nPos: 0 });

             

            function NullCheck()
            {
                var cInput = app.response({ cQuestion: "Input something:", cTitle: "Random Input" });

             

                if (cInput == null)
                    return;

             

                return;
            }

             

            If I input someting in the response dialog the script runs just fine, but if I cancel the response dialog I get this error message:

             

            GeneralError: Handlingen mislykkedes.
            Root.(null):8:Menu Null Check:Exec
            script terminated

             

            What am I doing wrong?

             

            Regards

            Martin

            • 3. Re: Check user input
              try67 MVP & Adobe Community Professional

              I'm getting the same behavior, but here's the really weird thing.

              Add this line just after the app.response line:

              app.alert(cInput);

               

              What do you know... No more errors being thrown.

              Take it out, the same thing happens again.

               

              I would say just ignore it, if possible...

              • 4. Re: Check user input
                mp-landbjorn Level 1

                Haha, you're right, try67. It is indeed weird!

                 

                The user input is used as a filename, so if the user cancels the input and null is returned, the pdf is saved as "null.pdf". That's why I want to check the input, and just exit the function if the user cancels.

                • 5. Re: Check user input
                  PihlDK

                  The script still continues after return;

                  Try this

                   

                  app.addMenuItem({ cName: "Gem &KMS-skrivelse", cParent: "File", cExec: "SaveAs('registreringsmeddelelse', 'produkter/')", nPos: 3 });

                  app.addMenuItem({ cName: "Gem &dokument i sag", cParent: "File", cExec: "SaveAs('', '')", nPos: 4 });

                   

                  function SaveAs(cName, cDirectory)

                  {

                      //Get full year

                      var date=new Date();

                      cYear = date.getFullYear();

                   

                      //Get year, case number and file name from user input

                      var cYear = app.response({ cQuestion: "Indtast sagsår:", cTitle: "Sagsår", cDefault: cYear });

                      var cCase = app.response({ cQuestion: "Indtast sagsnr.:", cTitle: "Sagsnr."});

                      var cFile = app.response({ cQuestion: "Indtast filnavn (uden .pdf):", cTitle: "Filnavn", cDefault: cName });

                   

                     if (cFile == null)

                           {}

                  else

                  {

                      //Concatenate path string

                      var cPath = "/h/digi/s" + cYear + "/" + cYear + "-" + cCase + "/" + cDirectory + cFile + ".pdf";

                   

                      //Try to save the document, if success, inform the user where to

                      try {

                          this.saveAs({ cPath: cPath, bPromptToOverwrite: true });

                          app.alert("Dokumentet blev gemt i " + cPath, 3);

                      }

                      catch(e) {

                          app.alert("Kunne ikke gemme dokument i " + cPath + ". Kontroller at mappen er oprettet, og at du har skriverettigheder til denne, samt at Acrobat/Reader er sat korrekt op.");

                      }

                   

                      //So long and thanks for all the fish

                      return;

                  }

                  }

                  1 person found this helpful
                  • 6. Re: Check user input
                    PihlDK Level 1

                    Or perhaps just

                     

                        if (cFile != null)

                     

                    {

                        //Concatenate path string

                        var cPath = "/h/digi/s" + cYear + "/" + cYear + "-" + cCase + "/" + cDirectory + cFile + ".pdf";

                     

                        //Try to save the document, if success, inform the user where to

                        try {

                            this.saveAs({ cPath: cPath, bPromptToOverwrite: true });

                            app.alert("Dokumentet blev gemt i " + cPath, 3);

                        }

                        catch(e) {

                            app.alert("Kunne ikke gemme dokument i " + cPath + ". Kontroller at mappen er oprettet, og at du har skriverettigheder til denne, samt at Acrobat/Reader er sat korrekt op.");

                        }

                     

                        //So long and thanks for all the fish

                        return;

                    }

                    1 person found this helpful