7 Replies Latest reply on Mar 21, 2016 5:38 AM by ashdflkjh

    Open and close PDF files from a CVS list

    ashdflkjh Level 1

      I would like to perform some actions on a list of files. I have them in a CSV file. I can read it in OK in the Console but cannot open them. The paths are correct, I can open one. But in the loop it does not work. Why?

       

      var oFile = util.readFileIntoStream("/C/test.txt");
      var cFile = util.stringFromStream(oFile, "utf-8");
      
      
      var n = cFile.split(",");
      
      
      //second part
      for (var i in n) {
        var otherDoc = app.openDoc(n[i]);
        otherDoc.closeDoc();
      }
      

       

      undefinedInvalid Location "/D/test.pdf". File or folder does not exist.

      NotAllowedError: Security settings prevent access to this property or method.

      App.openDoc:2:Console undefined:Exec

       

      undefined

        • 1. Re: Open and close PDF files from a CVS list
          try67 MVP & Adobe Community Professional

          Are you sure the file path is correct?

          What happens when you run just this code from the console:

          app.openDoc("/D/test.pdf");

          • 2. Re: Open and close PDF files from a CVS list
            ashdflkjh Level 1

            If I run it in a fresh console window it works as a single line. After I try the loop it does not. I thought maybe this is some security restriction on iteration...

             

            I am generally struggling with accessing files. The idea is to list all files in a directory, then manually remove some files on the list, then perform some operations on the remaining files. If I am missing a better way, I am all ears.

            • 3. Re: Open and close PDF files from a CVS list
              ashdflkjh Level 1

              Here is the evidence try67

               

              var oFile = util.readFileIntoStream("/C/test/test.txt");

              var cFile = util.stringFromStream(oFile, "utf-8");

               

              var n = cFile.split("\n");

              console.println(n);

               

              /C/test/test.pdf

               

              true

               

              for (var i in n) {

                var otherDoc = app.openDoc(n[i]);

                otherDoc.closeDoc();

              }

               

              Invalid Location "/C/test/test.pdf". File or folder does not exist.

              NotAllowedError: Security settings prevent access to this property or method.

              App.openDoc:2:Console undefined:Exec

               

              undefined

              • 4. Re: Open and close PDF files from a CVS list
                try67 MVP & Adobe Community Professional

                This doesn't really prove anything... Can you open this file path directly, as I asked?

                Also, change this line:

                console.println(n);

                To:

                console.println(n.toSource());

                And see what the output is... My guess is you're not removing all of the line-breaks from the input file.

                • 5. Re: Open and close PDF files from a CVS list
                  ashdflkjh Level 1

                  Thanks, try67

                   

                  //opening directly

                  var otherDoc = app.openDoc("/C/test/test.pdf");

                  undefined

                  //and the file opens

                   

                  //running this second

                  var oFile = util.readFileIntoStream("/C/test/test.txt");

                  var cFile = util.stringFromStream(oFile, "utf-8");

                   

                  var n = cFile.split("\n");

                  console.println(n.toSource());["\uFEFF/C/test/test.pdf"]

                   

                  true

                   

                  //now running this

                  for (var i in n) {

                    var otherDoc = app.openDoc(n[i]);

                    otherDoc.closeDoc();

                  }Invalid Location "/C/test/test.pdf". File or folder does not exist.

                  NotAllowedError: Security settings prevent access to this property or method.

                  App.openDoc:2:Console undefined:Exec

                   

                  undefined

                   

                  //i guess this looks like you are right. then how can i output a list of file and read them back into an array with those breaks removed?

                  • 6. Re: Open and close PDF files from a CVS list
                    try67 MVP & Adobe Community Professional

                    OK, it's actually not a line-break, but something called BOM, which appears as the first character of some UTF-8 text files, especially those created on a Mac.

                    All you have to do is remove it from the strings. You can use something like this in your loop:

                     

                    var filePath = n[i].replace("\uFEFF", "");
                    var otherDoc = app.openDoc(filePath);
                    

                     

                    You're benefiting from my experience here because I recently encountered this same issue and it took me several hours of frustration to solve it...

                    • 7. Re: Open and close PDF files from a CVS list
                      ashdflkjh Level 1

                      I see. Many thanks try67 !!! Here is how it looks now, as I was getting line breaks in multi-line files too

                       

                      for (var i in n) {
                        var otherDoc = app.openDoc(n[i].replace(/(\r\n|\n|\r)/gm,"").replace("\uFEFF", ""));
                        otherDoc.closeDoc();
                      }