Skip to main content
Known Participant
March 21, 2016
Answered

Open and close PDF files from a CVS list

  • March 21, 2016
  • 1 reply
  • 1342 views

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);

  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

This topic has been closed for replies.
Correct answer try67

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);

  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?


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.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...

1 reply

try67
Community Expert
Community Expert
March 21, 2016

Are you sure the file path is correct?

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

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

ashdflkjhAuthor
Known Participant
March 21, 2016

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.

ashdflkjhAuthor
Known Participant
March 21, 2016

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);

  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