I am currently using extendscript to generate invoices based on downloaded plain text emails (.txt).
Within the text file, there are specific lines containing "Order Number: 123456" followed by a line break. I have a script that I've created using code snippets found on this platform which helps identify the end of "Order Number:" to determine the starting point of a substring. My goal is to use the position of the line break as the second index to complete the substring. To achieve this, I have incorporated another script from the knowledgeable members of this community to create an array of indexes for each occurrence of a character. I plan to select the array object with a higher value than the initial number for the substring.
It may seem complex, but my knowledge of Javascript is still developing, and I'm unaware of any simpler approach.
Is there a specific character I should use to simulate a line break in a txt file using Javascript for extendscript in InDesign?
Thank you.
I have attempted to use characters like \n, \r\n, and ^p, both with and without quotation marks, but they do not appear in the array upon testing.
//Load Email as String
var b = new File("~/Desktop/Test/email.txt");
b.open('r');
var str = "";
while (!b.eof)
str += b.readln();
b.close();
var orderNumberLocation = str.search("Order Number: ") + 14;
var orderNumber = str.substring(orderNumberLocation, ARRAY NUMBER GOES HERE)
var loc = orderNumberLocation.lineNumber
function indexes(source, find) {
var result = [];
for (i = 0; i < source.length; ++i) {
// If you want to search case insensitive use
// if (source.substring(i, i + find.length).toLowerCase() == find) {
if (source.substring(i, i + find.length) == find) {
result.push(i);
}
}
alert(result)
}
indexes(str, NEW PARAGRAPH CHARACTER GOES HERE)
I aim to have all line breaks as an array of indexes stored in the variable "result".
Edit: The method I initially used to import the text file resulted in the removal of all line breaks. Implementing the code below instead has proved to be more effective. Now \n is functional.
var file = File("~/Desktop/Test/email.txt", "utf-8");
file.open("r");
var str = file.read();
file.close();