6 Replies Latest reply on Nov 29, 2016 7:48 AM by ChristianBahnsen

    acrobat xi -- use javascript to determine if a file exists

    ChristianBahnsen Level 1

      Another post from 2011, https://forums.adobe.com/thread/833146, seems to say there's no direct way to determine whether a file exists in the user's file system and that this is due to security concerns.  A reply by try67, however, says "You can use openDoc on a certain path. If it returns null, the file doesn't exist. If it throws a security error, it exists but is not disclosed."

       

      I'm hoping that between 2011 and now, Adobe (or someone) has come up with a simple way to test for file existence using javascript.

       

      If not, I took try67's suggestion and came up with the following code which does seem to be able to test for file existence:

       

      var myPathAndFile = "/serverName/faxes/christianBahnsen/test.pdf"
      try {
         existingDoc = app.openDoc({cPath: myPathAndFile, bHidden: true});
         app.alert("this document name already exists!");
         existingDoc.closeDoc();
      } catch (e) {
         var myFolder = "complete";
         var myPathAndName = "./" + myFolder + "/newName2.pdf";
         this.saveAs(myPathAndName);
      };

       

       

      It seems cumbersome to have to open the file to test for its existence and then immediately close it.  I fail to see the problem/harm/risk with being able to test for a file's existence since I am able to open it.  This script is also able to save the file with a new name if the file doesn't already exist.

       

      I'm still learning adobe javascript.  If someone has a more efficient way to accomplish the same end I'd love to see it.

       

      Thanks

       

      Christian Bahnsen

        • 1. Re: acrobat xi -- use javascript to determine if a file exists
          try67 MVP & Adobe Community Professional

          It's not only not easier to do now, it's even more difficult... The method I described before doesn't work anymore, unless a script is embedded in the file you're trying to open, and you can't open it as a hidden file anymore, too.

          • 2. Re: acrobat xi -- use javascript to determine if a file exists
            gkaiseril MVP & Adobe Community Professional

            No it has not changed.

             

            As for security, if you open a PDF file to you want an unknown person looking through all your PDF files?

             

            Or better yet, saving a copy of them somewhere else without your permission?

            • 3. Re: acrobat xi -- use javascript to determine if a file exists
              ChristianBahnsen Level 1

              They're not really my files.  We have a fax device that saves incoming faxes in a shared folder.  The faxes are automatically assigned a name, e.g., FaxReceived_2016-10-12-05;17;17.7430_b622123f-17a3-4b6b-ab28-11c14a6d70a4.pdf.

               

              The users who process the faxes need to open them, look them over, perform various actions depending on the content in the fax, and ultimately save them in a different folder.

               

              I'm working on an "Action" that may be able to help users, particularly new users, process the documents more efficiently.  I collect several pieces of data along the way using app.response() dialogs.  At the end of the process I'm going to build a meaningful file name and save the renamed PDF into a different folder, hence my need to verify that the file name I cook up isn't already in use.

               

              The users already can open and saveAs the PDFs, so I'm not concerned about security, per se.  I'm focused on efficiency.

               

              Christian

              • 4. Re: acrobat xi -- use javascript to determine if a file exists
                Test Screen Name Most Valuable Participant

                The thing about security is that it inconveniences all of us all of the time, because there are bad people who might exploit weaknesses. It cannot in general be turned off (that would leave this system open to attack as well as able to easily do what you want).


                JavaScript has never been intended as a general solution for working on the filesystem, and there are deliberate omissions to make many things difficult or impossible (and hence, not exploitable).

                 

                I notice your end result is to "save the PDF to a different folder". How do you propose to do that? If you're using an external controller, it can also be checking for files.

                • 5. Re: acrobat xi -- use javascript to determine if a file exists
                  try67 MVP & Adobe Community Professional

                  Since your process is not fully automated anyway, just set the bPromptToOverwrite parameter of the saveAs method as true.

                  That way, if a file with that name already exists the user will be informed of it and could change the file-name.

                  1 person found this helpful
                  • 6. Re: acrobat xi -- use javascript to determine if a file exists
                    ChristianBahnsen Level 1

                    Thanks for pointing out the bPromptToOverwrite parameter.  Yes, that will let the user know that the file already exists.

                     

                    From various articles I've looked at I take it that app.execMenuItem("SaveAs") will open a dialog to save the file but there's no way to set the path or file name via scripting.  Too bad, that would seem to be the optimum solution.

                    acrobatSaveAs11292016.JPG