Copy link to clipboard
Copied
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
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...
Copy link to clipboard
Copied
Are you sure the file path is correct?
What happens when you run just this code from the console:
app.openDoc("/D/test.pdf");
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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...
Copy link to clipboard
Copied
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.replace(/(\r\n|\n|\r)/gm,"").replace("\uFEFF", ""));
otherDoc.closeDoc();
}