4 Replies Latest reply on Nov 15, 2010 3:47 AM by Cookie4ddict

    Problem with checking file existence by using app.openDoc

    Cookie4ddict

      Hi Folks,

       

      I have to create a script for a big project with many PDF's. (Just like Ingrimm - it's the same company http://forums.adobe.com/thread/747022?tstart=0) This PDF's are made of scanned images. I need to rename the files to names that are extracted from within the file. This is working without problems.

      Now there could be the case, that a filename exists already. I don't want the files to be overwritten, so the idea is, that already existing filenames are saved as filename_1, filename_2, etc... (Before I merged them, but this isn't what is wanted.)

      For this reason I need to use app.openDoc to check, if a file exists. Yesterday my script worked fine, but after starting it today, I discovered that it started again to overwrite files which are already existing.

      I added an app.alert to my catch method to see what error is thrown. The message is: 'NotAllowedError: Security settings prevent access to this property or method.'.

      After using google, I found out, that Acrobat is not finding any files. But I checked and there are files which should be doubled.

      I have no idea how to fix that problem. Anybody here who can help me?

       

      Regards,


      Steffi

       

       

      /**

      * regular expression for search
      */

      var idNumber = /08\d\d\d\d\-\d\d\d\-\d\d\d\d\d-\d\d\d/g;


      /**

      * if possible this function extracts the searched number as string

      *

      * @param rematch string which should be searched in document

      * @return null if rematch is not found or string if rematch is found

      */

      function ExtractFromDocument(reMatch) {

        try {

               var Out = new Object();

               for (var i = 0; i < 1; i++)

            {

                numWords = this.getPageNumWords(i);

                var PageText = "";

                for (var j = 0; j < 30;j++) {

                    var word = this.getPageNthWord(i,j,false);

                    PageText += word;

                }

                var strMatches = PageText.match(reMatch);

                if (strMatches == null) continue;

            }

            return strMatches;

        } catch(e)

        {

            app.alert("Processing error: "+e)

        }

      }


      /**

      * tries to load given filename (extracted number)

      *

      * @param filename string of file which should be checked

      * @param n number to iterate while checking for files

      * @return true if file exists or false if not

      */

      function checkIfFileExists(filename, n) {

          var existingDoc = false;

          try {

              if( n == 0) {

                  var checkDoc = app.openDoc({cPath : "../GAG-out/"+filename+"-001.pdf"});

              } else { 

                  var checkDoc = app.openDoc({cPath : "../GAG-out/"+filename+"-001_"+n+".pdf"});

              }

              checkDoc.closeDoc();

              existingDoc = true;

          } catch (e) {  

               app.alert("Processing error: "+e)

          }

          if( existingDoc == true ) {

              n = n+1;

              n = checkIfFileExists(filename, n);

          }

          return n;

      }


      var filename = ExtractFromDocument(idNumber);

      if(filename == null || filename == undefined) {

        filename = Math.round(Math.random()*999999999999);

        this.extractPages({nEnd:(this.numPages-1), cPath : "../GAG-out/n_"+filename+".pdf"});


      } else {

          fileExistence = checkIfFileExists(filename, 0);

          if(fileExistence != 0) {

              this.extractPages({nEnd:(this.numPages-1), cPath : "../GAG-out/"+filename+"-001_"+fileExistence+".pdf"}); 

          } else {

              this.extractPages({nEnd:(this.numPages-1), cPath : "../GAG-out/"+filename+"-001.pdf"});

          }

      }