just_dabbling
just_dabbling

Reputation: 51

AppleScript: How to extract numbers from a string?

I am writing a script to go to the NYT website on Corona, get the US data, extract numbers (total, death), and to send me a notification. I am close, but when I extract numbers and display them, they are put together (ie 700021 instead of 7000,21). My question is:

How do I extract the numbers so that they are delineated?

Here is the code:

set theURL to "https://www.nytimes.com/interactive/2020/world/coronavirus-maps.html?action=click&pgtype=Article&state=default&module=styln-coronavirus&variant=show&region=TOP_BANNER&context=storyline_menu"

tell application "Safari" to make new document with properties {URL:theURL}

tell application "System Events"
    repeat until exists (UI elements of groups of toolbar 1 of window 1 of application process "Safari" whose name = "Reload this page")
        delay 0.5
    end repeat
end tell

to getInputByClass(theClass, num)
    tell application "Safari"
        set input to do JavaScript "
document.getElementsByClassName('" & theClass & "')[" & num & "].innerText;" in document 1
    end tell
    return input
end getInputByClass

set myVar to getInputByClass("g-body ", 5)

on returnNumbersInString(inputString)
    set s to quoted form of inputString
    do shell script "sed s/[a-zA-Z\\']//g <<< " & s
    set dx to the result
    set numlist to {}
    repeat with i from 1 to count of words in dx
        set this_item to word i of dx
        try
            set this_item to this_item as number
            set the end of numlist to this_item
        end try
    end repeat
    return numlist
end returnNumbersInString

set theNums to returnNumbersInString(myVar) as text

display notification "COVID-19 UPDATE" subtitle theNums sound name "glass"

tell application "Safari"
    close its front window
end tell

Upvotes: 1

Views: 1062

Answers (2)

user3439894
user3439894

Reputation: 7565

Similar to your other question I helped you with, the target data is already in a table and as such I'd use the table data to get the information as its structure layout is not likely to change where target 'g-body ' of 5 may not always be the United States.

I get my data a little different way:

set theURL to "https://www.nytimes.com/interactive/2020/world/coronavirus-maps.html?action=click&pgtype=Article&state=default&module=styln-coronavirus&variant=show&region=TOP_BANNER&context=storyline_menu"

tell application "Safari" to make new document with properties {URL:theURL}

tell application "System Events"
    repeat until exists ¬
        (UI elements of groups of toolbar 1 of window 1 of ¬
            application process "Safari" whose name = "Reload this page")
        delay 0.5
    end repeat
end tell

tell application "Safari" to tell document 1 to set CountriesTable to ¬
    do JavaScript "document.getElementsByClassName('svelte-f9sygj')[0].innerText;"

tell application "Safari" to close its front window

set awkCommand to ¬
    "awk '/United States/{print $3,\"Cases &\",$4,\"Deaths\"}'"

set notificationMessage to ¬
    do shell script awkCommand & "<<<" & CountriesTable's quoted form

display notification notificationMessage subtitle "US COVID-19 UPDATE" sound name "glass"


  • NOTE: The code used to determine when the page in Safari has finished loading works in macOS Mojave and later, however, for macOS High Sierra and some earlier versions, add the words buttons of in front of UI elements ... in the repeat until exists ¬ ... code.

Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

Upvotes: 0

red_menace
red_menace

Reputation: 3422

You are getting a list of numbers from the returnNumbersInString handler, but just coercing the list to text doesn't normally provide any kind of formatting. One solution would be to use text item delimiters to specify the text to use when joining the list items. For example, when converting to text for the notification you could do something like:

set tempTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ", "
set theNums to returnNumbersInString(myVar) as text
set AppleScript's text item delimiters to tempTID

Upvotes: 1

Related Questions