0

I am using extendscript to build some invoices from downloaded plaintext emails (.txt)

At points in the file there are lines of text that look like "Order Number: 123456" and then the line ends. I have a script made from parts I found on this site that finds the end of "Order Number:" in order to get a starting position of a substring. I want to use where the return key was hit to go to the next line as the second index number to finish the substring. To do this, I have another piece of script from the helpful people of this site that makes an array out of the indexes of every instance of a character. I will then use whichever array object is a higher number than the first number for the substring.

It's a bit convoluted, but I'm not great with Javascript yet, and if there is an easier way, I don't know it.

What is the character I need to use to emulate a return key in a txt file in javascript for extendscript for indesign?

Thank you.

I have tried things like \n and \r\n and ^p both with and without quotes around them but none of those seem to show up in the array when I try them.

//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 want all my line breaks to show up as an array of indexes in the variable "result".

Edit: My method of importing stripped all line breaks from the document. Using the code below instead works better. Now \n works.

var file = File("~/Desktop/Test/email.txt",  "utf-8");  
file.open("r");  

var str = file.read();  

file.close();  
RadioMan
  • 3
  • 5

3 Answers3

0

Please try \n and \r

Example: indexes(str, "\r");

madoho
  • 1
  • 2
  • Oops. Sorry that's what I meant. I tried \n and \r. I just mistyped it in the question. Just in case, I did try them both again and the two together. Alert box still turns up empty. – RadioMan May 15 '19 at 18:00
0

If i've understood well, wat you need is to str.split():

function indexes(source, find) {
    var order;
    var result = [];
    var orders = source.split('\n'); //returns an array of strings: ["order: 12345", "order:54321", ...]
    for (var i = 0, l = orders.length; i < l; i++)
    {
        order = orders[i];
        if (order.match(/find/) != null){
            result.push(i)
        }
    }
    return result;
}
Miguel
  • 709
  • 7
  • 21
  • I used **indexes(str, 'Order')** and it returned undefined. I'm pretty new to Javascript so I may be doing something wrong? That being said, this is _much_ simpler code than what I had in mind. I was going to use a for loop to compare the index number of "Order Number" to every index number of a line break and make a substring from whichever index is higher. Thank you for this! – RadioMan May 16 '19 at 18:00
  • Maybe I didn't understood what exactly do you want to do. If what you want is to find indexes that match some order number, you have to pass the order number instead 'Order': `indexes(str, 12345)`. If you need other thing, a more human friendly explanation would be great :) – Miguel May 16 '19 at 18:17
  • I'm sorry. I can explain myself better. We have these emails that come in for orders of varying items. We manually take the information from the email and put them in an InDesign document in an aesthetically pleasing manner. So, I want to be able to search for terms like Order Number:, Address:, and things like that in the email. That information in the following text fields can then be fed into text boxes in InDesign. The problem is, when I download the emails, I do that in pain text, but luckily every text field is on it's own line automatically. So I need to split them. (Hope that's better) – RadioMan May 16 '19 at 19:19
  • Ok, thats a better explanation. What do you need is to use regular expressions. It's not a trivial thing when you are begginer, but I'll try to help you. Posible answer below – Miguel May 16 '19 at 20:13
0

You need to use Regular Expressions. Depending on the fields do you need to search, you'l need to tweek the regular expressions, but I can give you a point. If the fields on the email are separated by new lines, something like that will work:

var str; //your string
var fields = {}
var lookFor = /(Order Number:|Adress:).*?\n/g;

str.replace(lookFor, function(match){
    var order = match.split(':'); 
    var field = order[0].replace(/\s/g, '');//remove all spaces
    var value = order[1];
    fields[field]= value;
})

With (Order Number:|Adress:) you are looking for the fields, you can add more fields separated the by the or character | ,inside the parenthessis. The .*?\n operators matches any character till the first break line appears. The g flag indicates that you want to look for all matches. Then you call str.replace, beacause it allows you to perfom a single task on each match. So, if the separator of the field and the value is a colon ':', then you split the match into an array of two values: ['Order number', 12345], and then, store that matches into an object. That code wil produce:

fields = {
    OrderNumber: 12345,
    Adresss: "my fake adress 000"
}
Miguel
  • 709
  • 7
  • 21
  • This is great! Took me a little bit to figure out how to call up the properties, but now it works _very_ well. Incidentally, while trying this out, I realized that my method of importing the text also stripped it of all line breaks which is why \n wasn't working. Thank you! – RadioMan May 17 '19 at 16:28