AppleScript & JavaScript – Get Numbers from Chrome and Calculate Total

applescriptgoogle-chromejavascriptjavascript-automation

I'm trying do get every totals from an internal webpage and get the total.

for getting a single result I wrote this :

tell application "Google Chrome"
    tell tab 1 of window 1 to set orderTotal to execute javascript "document.getElementsByClassName('total')[0].innerHTML;"
end tell 

the output on this example is :

"<td>1,200.00</td><td>JPY</td>"

(PS the currency is not always the same)

for getting every value this work

tell application "Google Chrome"
    tell tab 1 of window 1 to set r to execute javascript "var outPut=[]; var arr=document.getElementsByClassName('total');for (var i in arr) {outPut.push(arr[i].innerHTML)};outPut;"
end tell
 return r

which give me something like this :

"<td>4,000.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", "<td>3,800.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", missing value, missing value, missing 

I would like to clear the text and do a total,

Tried this for clearing the values, but obviously not working :

set theText to Unicode text
property leftEdge1 : "<td>"
property rightEdge1 : "</td>"
try
    set saveTID to text item delimiters
    set text item delimiters to leftEdge1
    set classValue to text item 2 of r
    set text item delimiters to rightEdge1
    set singleValue to text item 1 of classValue
    set text item delimiters to saveTID
    singleValue
end try

and for the total I'm not sure yet how to proceed.

Best Answer

In that absence of the actual URL and what appears to be incomplete output shows in the OP the following example AppleScript code defines r as a list and sums its numeric values.

set r to {"<td>4,000.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", "<td>3,800.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", "<td>5,000.00</td><td>JPY</td>", missing value, missing value, missing value}

set theText to Unicode text
property leftEdge1 : "<td>"
property rightEdge1 : "</td>"
set totalValue to 0

repeat with i from 1 to count r
    try
        set saveTID to text item delimiters
        set text item delimiters to leftEdge1
        set classValue to text item 2 of item i of r
        set text item delimiters to rightEdge1
        set singleValue to text item 1 of classValue
        set text item delimiters to saveTID
        set totalValue to totalValue + singleValue
    end try
end repeat

log totalValue

-- Result:
-- 42800

Note: The example AppleScript code is just that and does not employ any other error handling then what's shown and is meant only to show one of many ways to accomplish a task. The onus is always upon the User to add/use appropriate error handling as needed/wanted.