10 Replies Latest reply on Jan 27, 2014 12:55 PM by try67

    Problem with SaveAs function using random paths.

    Anonymous.123

      The Setup:

       

      Part 1) I have a folder-level script that allows me to perform a SaveAs function from within forms. I do this as a way to save the document quietly in the background. The effect is that the file is overwritten by a copy of itself. The script is:

       

      var mySaveAs = app.trustedFunction( function(oDoc,cPath,cFlName)

      {

          app.beginPriv();

          cPath = cPath.replace(/([^\/])$/, "$1/");

          try{

              oDoc.saveAs(cPath + cFlName);

          }catch(e){

              app.alert("Error During Save");

          }

          app.endPriv();

      });

       

       

      Part 2) From within my documents, I perform the save by calling the function below:

       

      function runSave()

      {if(typeof(mySaveAs) == "function") {

          var pathArray = this.path.split("/");

          var myFileName = pathArray[pathArray.length-1];

          var cPath = this.path.slice(0,myFileName);

          mySaveAs(this, cPath, myFileName);

      } else {

          app.alert("Missing Save Function\n" + "Please contact forms administrator");

      }

       

      }

       

      Part 3) I have several large forms that utilize an autosave to call the save function according to a time interval of 5 minutes, using:

       

      app.setInterval(runSave(), 300000);

       

       

      The Problem:

       

      Part 1) The autosave feature works fine and it works silently in the background. However, I start to experience problems when I have two forms open at the same time. If, for example, I have a form from one folder open, and open a form from a second folder, the save feature will sometimes save the active document to it's original folder (as intended, overwriting itself and creating an autosave) or sometimes save to the second document's original folder. This leaves me with an updated (autosaved) copy of the document in the wrong folder, and a non-updated copy in the original folder. This seems to vary depending on which document I opened last and/or which document is currently active. Though I can't seem to find the right combination.

       

      It's as thought the "this.path" is getting confused.

       

      Part 2) Worse still, if the two documents have the same name, as is often the case with these forms, any incorrect filing when saving causes an overwrite of the second document and loss of data.

       

      Part 3) To makes things maddening, this also happens at times when no second folder or second document is open. Instead, the misfiling saves the single, active document to a recently accessed folder. For example, I'll open a local folder and open a Word doc, close the doc and the folder, go to a different folder in a different root (a networked folder), open the form, and it autosaves to the local machine in the folder with the Word doc. So now not only do I have a non-updated copy in my correct folder, I have no idea where the updated copy was actually saved to until I come across it some time later.

       

      Part 4) Again, worse still, the previously accessed folder might happen to be one that contains a document with the same name, and that document gets overwritten by the autosave. I'll have no idea that the form has been overwritten until I happen to open it some time later and see that it contains data from a totally different form.

       

      What is going on and how do I stop it?

       

      Adobe Acrobat X Pro on a PC.

        • 1. Re: Problem with SaveAs function using random paths.
          George_Johnson MVP & Adobe Community Professional

          It's probably a problem of scope related to how the setInterval method works. As a test, instead of naming the runSave function the same in each document, give it a unique name in each document. Also, use the app.clearInterval method when the document is closed.

          • 2. Re: Problem with SaveAs function using random paths.
            Anonymous.123 Level 1

            Well, the problem is occuring primarily with distributed forms, so each document is identical, and thus must contain the same names for functions. A simplified scenario is that there are 10 folders, each with a blank copy of the distributed form. Individual users open and complete a form. When I open the completed forms to review them, I run into this overwrite issue.

            • 3. Re: Problem with SaveAs function using random paths.
              Test Screen Name Most Valuable Participant

              I think you need to avoid "this". I don't think the meaning of "this" is defined in this context.

              • 4. Re: Problem with SaveAs function using random paths.
                Anonymous.123 Level 1

                That's what I think the problem is. The meaning of "this" is getting confused by the program when switching between active documents (although it wouldn't explain the problem with using the path of a recently accessed document that isn't currently open). But is there another way to refer to the document in a reliable and permanent way? Specifically, when calling the path?

                • 5. Re: Problem with SaveAs function using random paths.
                  Anonymous.123 Level 1

                  Looking at Part 2 of the Setup, you can see that the script for the runSave function uses the "this" object to find a path (and subsequently a filename off that path) and to indicate the doc object to be passed to the mySaveAs function in the folder-level script. So it's unclear to me how the function can use the "this" object to locate the correct path initially, in order to obtain the correct filename, and also pass the correct document object to the mySaveAs function, but then pass an incorrect path to the mySaveAs function.

                  • 6. Re: Problem with SaveAs function using random paths.
                    try67 MVP & Adobe Community Professional

                    The code in that tutorial is sadly no longer working, I've discovered recently.

                    This is my own personal code for a trusted saveAs method. Notice it takes 2 parameters, not 3.

                     

                    safeSaveAs = app.trustPropagatorFunction(function(doc,vPath){

                        app.beginPriv();

                        doc.saveAs({cPath:vPath});

                        app.endPriv();

                    });

                    myTrustedSaveAs = app.trustedFunction(function(doc,vPath){

                        app.beginPriv();

                        safeSaveAs(doc,vPath);

                        app.endPriv();

                    });

                     

                     

                    You call it like this:

                    myTrustedSaveAs(this, "/c/temp/test.pdf");

                    • 7. Re: Problem with SaveAs function using random paths.
                      Anonymous.123 Level 1

                      Is this meant to be separated into folder- and document-level scripts? That is, the safeSaveAs is contained in a folder-level script and the myTrustedSaveAs is contained in a document-level scripted and called separately? Or are both functions meant to be contained in a folder-level script? In which case, I'm wondering why you'd nest one trusted function in another like that.

                       

                      Sorry if this isn't clear, I'm still shakey with folder-level scripts and trusted functions.

                       

                      Also, what was the rationale for using 2 parameters instead of 3?

                      • 8. Re: Problem with SaveAs function using random paths.
                        try67 MVP & Adobe Community Professional

                        The first part is a folder-level script, the second can be used anywhere.

                         

                        Regarding the parameters: I just prefer it that way... If you want you can split it to three parameters. Shouldn't be too complex...

                         

                        The reason for the nesting is that it works that way, as opposed to the other alternatives...

                        1 person found this helpful
                        • 9. Re: Problem with SaveAs function using random paths.
                          Anonymous.123 Level 1

                          Thank you, that is helpful to understand. I'd like to keep it as 3 parameters, as all my documents currently output 3 to the trusted function. And I'd still like to keep the error catch, but I'm not sure why the previous trusted function from the tutorial is no longer working, so I just want to run this by you:

                           

                          safeSaveAs = app.trustPropagatorFunction(function(theDoc,thePath,theFileName){

                           

                              app.beginPriv();

                              var fullPath = (thePath + theFileName);  

                              try {

                                  theDoc.saveAs({cPath:fullPath});

                              } catch(e) {

                                  app.alert("Error During Save");

                              }

                              app.endPriv();

                           

                          });

                           

                           

                          myTrustedSaveAs = app.trustedFunction(function(theDoc,thePath,theFileName){

                           

                                  app.beginPriv();

                                  safeSaveAs(theDoc,thePath,theFileName);

                                  app.endPriv();

                           

                          });

                          • 10. Re: Problem with SaveAs function using random paths.
                            try67 MVP & Adobe Community Professional

                            Yes, that looks fine...