13 Replies Latest reply on Jan 11, 2018 8:59 AM by Thom Parker

    Help with Save As using JavaScript

    sebastianb99835257 Level 1

      Hi All,

       

      This is my first attempt at using Javascript...

       

      I am attempting to create a Save As button on a form that will:

      • Create a file name based on fields;
      • Save to a specific folder (And if it doesn't exist then create folder);
      • Bring up the "Save As" box for confirmation (as opposed to a silent save);
      • Bring up a warning IF Adobe is going to save over an existing pdf (aka has the exact same name);
      • Close Adobe after all of the above is executed.

       

      So far I have been fairly successful. I have been able to do a silent save, saving to a specific location and saving the file name based on fields. I need help building the rest of the functionality into the code if possible.

       

      Thanks in advanced!

      ------------------------------------------------------------------------------------------ -------------------------------

      The code I have so far is:

       

      Trusted Level Function

      saved in Notepad under the Adobe / Javascript folder

       

      mySaveAs = app.trustPropagatorFunction(function(doc,path) {

      app.beginPriv();

      doc.saveAs(path);

      app.endPriv();

      })

       

       

      myTrustedSpecialTaskFunc = app.trustedFunction(function(doc,path) {

      // Privileged and/or non-privileged code above

      app.beginPriv();

      mySaveAs(doc,path);

      app.endPriv();

      // Privileged and/or non-privileged code below

      });

       

      PDF Button

      Code under button that is executed on Mouse up click

       

      // build file name

      var myFileName = getField("Work_Pack").value + " - " + getField("Form_Name").value + " - " + getField("Todays_Date").value + ".pdf";

      // add folder name

      myFileName = "/c/temp/Saved Forms/" + myFileName

      myTrustedSpecialTaskFunc(this, myFileName);

       

      this.closeDoc() ;

       

      ------------------------------------------------------------------------------------------ -------------------------------

        • 1. Re: Help with Save As using JavaScript
          Bernd Alheit Adobe Community Professional & MVP

          Points 3 and 5 are not possible. Info to point 2: Acrobat JavaScript can't create folders.

          • 2. Re: Help with Save As using JavaScript
            JR_Boulay Adobe Community Professional

            Point 3 :

            app.execMenuItem("SaveAs");

             

            Point 5 :

            JavaScript can close the document but cannot quit Acrobat.

            • 4. Re: Help with Save As using JavaScript
              sebastianb99835257 Level 1

              Hi JR_Boulay,

               

              Can the app.execMenuItem("SaveAs"); function be inserted into the code to bring up the “Save As” box along with the predefined saving location and save name that I have coded above?

               

              Cheers,

              Seb.

              • 5. Re: Help with Save As using JavaScript
                try67 MVP & Adobe Community Professional

                No. It's essentially the same as clicking the Save As command under the File menu of the application.

                • 6. Re: Help with Save As using JavaScript
                  sebastianb99835257 Level 1

                  So is there any way to bring up the Save As box AND pre fill the save name and save location?

                   

                  Or is it a silent save as I have done in the above code OR what you describe with the app.execMenuItem("SaveAs"); which is essentially clicking the save as button in the menu? which wont pre fill the save name and location?

                   

                  • 7. Re: Help with Save As using JavaScript
                    try67 MVP & Adobe Community Professional

                    So is there any way to bring up the Save As box AND pre fill the save name and save location?

                     

                    No.

                     

                    Or is it a silent save as I have done in the above code OR what you describe with the app.execMenuItem("SaveAs"); which is essentially clicking the save as button in the menu? which wont pre fill the save name and location?

                     

                    Yes, those are your only options.

                    • 8. Re: Help with Save As using JavaScript
                      sebastianb99835257 Level 1

                      Hmm well that's a bit annoying.. but ultimately good to confirm.. I'm going to use a work around to point 4 by making each file name unique by including a time stamp (date and time fields). YYYYMMDDHHMM or something like that.

                      • 9. Re: Help with Save As using JavaScript
                        JR_Boulay Adobe Community Professional

                        For saving I use a script that displays an alert box containing the new unique name before displaying the Save As dialog box.

                        So, the end user can copy-paste the new unique name from the alert box to the Save As dialog box.

                         

                        Here is a sample, you just have to adjust the "strFixe" var (line 2), the "Name" field name (line 3), and the "Date" field name (line 4):

                         

                        /******************************************************/
                        var strFixe = "Note-de-frais";
                        var strNom = this.getField("NOM").value;
                        var strNum = this.getField("DATE").value
                        /* language detection */
                        if (app.language == "FRA") {
                            var strTitre = strFixe + " - Enregistrement";
                            var strQuestion = "COPIEZ le nom propos\u00E9 ci-dessous et COLLEZ le dans la fen\u00EAtre suivante pour l'utiliser comme nom de fichier :";
                        } else {
                            var strTitre = strFixe + " - Save As";
                            var strQuestion = "COPY below proposed text and PASTE it in the following box to use it as file name:";
                        }
                        /* new file name concatenation */
                        var strDisplay = strNum + "_" + strNom + "_" + strFixe;
                        /* alert box */
                        var saveRep = app.response({cQuestion: strQuestion, cTitle: strTitre, cDefault: strDisplay,});
                        if (saveRep != null) {
                            /* Acrobat Pro detection */
                            if (app.viewerVariation == "Full") {
                                /* edit the document Title */
                                this.info.Title = strNum + " - " + strNom + " - " + strFixe;
                            }
                            /* hide the target button and save */
                            event.target.display = display.hidden; // optional
                            app.execMenuItem("SaveAs");
                        }
                        /******************************************************/
                        
                        • 10. Re: Help with Save As using JavaScript
                          sebastianb99835257 Level 1

                          Hi JR,

                           

                          Quick question I tried this on DC Pro and it worked! yay

                           

                          I tried it on a different PC with Adobe Reader DC and it doesn't work as intended. I can copy the text but as soon as I click Ok it wipes what I have saved from my clipboard..

                           

                          Any thoughts?

                           

                          Cheers,
                          Seb.

                          • 11. Re: Help with Save As using JavaScript
                            JR_Boulay Adobe Community Professional

                            No, hélas.

                            I never got this issue.

                            • 12. Re: Help with Save As using JavaScript
                              sebastianb99835257 Level 1

                              FYI for those interested I used the following:

                               

                              Trusted Level Function

                              saved in Notepad under the Adobe / Javascript folder

                               

                              mySaveAs = app.trustPropagatorFunction(function(doc,path) {

                              app.beginPriv();

                              doc.saveAs(path);

                              app.endPriv();

                              })

                               

                              myTrustedSpecialTaskFunc = app.trustedFunction(function(doc,path) {

                              // Privileged and/or non-privileged code above

                              app.beginPriv();

                              mySaveAs(doc,path);

                              app.endPriv();

                              // Privileged and/or non-privileged code below

                              });

                               

                              PDF Button

                              Code under button that is executed on Mouse up click

                               

                              // build file name

                              var myFileName = getField("SAP_Work_Order").value + " - " + getField("Form_Name").value + " - " + getField("Todays_Date").value + ".pdf";

                              // add folder name

                              myFileName = "/c/Offline Forms/" + myFileName

                              myTrustedSpecialTaskFunc(this, myFileName);

                               

                               

                              app.alert("This Form (" + getField("SAP_Work_Order").value + " - " + getField("Form_Name").value + " - " + getField("Todays_Date").value + ") has saved successfully to the following location: /C/Offline Forms/" , 3);

                               

                               

                              Further Explanations

                              The "Todays_Date" field has the following:

                              • Add in Document level JavaScript > Script Name = Todays_Date > Add > getField("Todays_Date").value = util.printd("yyyymmddHHMM", new Date()); > Ok > Close
                              • field Script > this.getField("Today").value = util.printd("mm/dd/yyyy", new Date());

                               

                              The above code allowed me to fill most of my requirements to a point that I was happy with:

                              • Create a file name based on fields; YES
                              • Save to a specific folder (And if it doesn't exist then create folder); YES/NO (Have to save to a specific hard coded folder)
                              • Bring up the "Save As" box for confirmation (as opposed to a silent save); YES - Have to do silent save but by using a pop up box I can bring up a message saying the file saved successfully and the location it saved.
                              • Bring up a warning IF Adobe is going to save over an existing pdf (aka has the exact same name); YES/NO - Using the "Todays_Date" field which gives a time stamp down to the minute ensures that no file gets accidentally saved over silently.
                              • Close Adobe after all of the above is executed. I didn't actually do this in the end but I think the following function works "this.closeDoc(true);"
                              • 13. Re: Help with Save As using JavaScript
                                Thom Parker Adobe Community Professional

                                I don't think anyone pointed this out to you, but the "app.trustPropagatorFunction" function doesn't do anything. You don't need it. You have one nested function too many.