How to read each line of a .txt and save it in an array?
I need to extract each line from a text file to assign it as a variable value. Thanks in advance.
I need to extract each line from a text file to assign it as a variable value. Thanks in advance.
var txt = File("path/to/file.txt");
txt.open('r');
var str = txt.read();
var arr = str.split('\n');
More on the File object and the methods and props available: https://www.indesignjs.de/extendscriptAPI/indesign-latest/#File.html There are other ways to skin the cat. You can do a while statement on !txt.eof and use txt.readln() to push a string to a fresh array, but I find split using the new line character quick and easy. You may want to set txt.encoding to 'UTF-8'. Another good practice is to check if (txt.exists) before opening it.
Already have an account? Login
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.