Applescript to filter the clipboard

applescriptcopy/paste

Let say I have this in my clipboard :

************TH4F    fsdfssdggg0878  Ex.US   Unknown 0   $100.00 05/24/17 04:40  No Bonus    
************TH4F    fsdfssdggg0878  Un.UK   Unknown 0   $50.00  05/20/17 04:44  No Bonus    

who can I only keep the total and add it in a list and same for the date ?

I'm a bit stuck on the script for now :

repeat with i in (get (the clipboard)'s text items)
    return i
end repeat

Best Answer

You can't really (or shouldn't) use text items without first defining your text item delimiters.

If each field in the formatted text is separated by a tab character, then the task is trivial:

set my text item delimiters to tab

set cbTotals to {}
set cbDates to {}

repeat with row in the paragraphs of (the clipboard as text)
    set the end of cbTotals to text item 6 of the row
    set the end of cbDates to text item 7 of the row
end repeat

If the fields are, as they appear here, actually separated by a variable number of spaces, then one might be tempted to count each space between each field, and enumerate the text items in the same way we just did above. However, this is impractical, and more importantly, it is not a very robust way to approach this in a general context, because the content of your data might change slightly at some point. For example, you may, on one occasion, select the text flush with the start of the line, whilst on another occasion, over-select and include some leading whitespace.

One way to ensure consistency is to deal with the spaces first:

set my text item delimiters to space

set |cb₀| to the text items of (the clipboard as text)

repeat with cbField in |cb₀|
    if cbField's contents = "" then set cbField's contents to missing value
end repeat

set cb to the strings in |cb₀| as text

Now cb contains an homogeneously-delimited version of your clipboard data, where each field is separated by a single space character. Now we can employ the same technique from earlier, and obtain the relevant text items. Without changing your text item delimiters, which will currently be set to space:

set cbTotals to {}
set cbDates to {}

repeat with row in the paragraphs of cb
    set the end of cbTotals to text item 6 of the row
    set the end of cbDates to text item 7 of the row
end repeat