AppleScript:如何从字符串中提取数字?
AppleScript: How to extract numbers from a string?
我正在编写一个脚本,用于访问有关 Corona 的 NYT 网站,获取美国数据,提取数字(总数、死亡),并向我发送通知。我很接近,但是当我提取数字并显示它们时,它们被放在一起(即 700021 而不是 7000,21)。我的问题是:
如何提取数字以便将它们描绘出来?
代码如下:
set theURL to "https://www.nytimes.com/interactive/2020/world/coronavirus-maps.html?action=click&pgtype=Article&state=default&module=styln-coronavirus&variant=show®ion=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
您正在从 returnNumbersInString
处理程序中获取数字列表,但仅将列表强制转换为文本通常不会提供任何类型的格式。一种解决方案是使用 text item delimiters
指定加入列表项时要使用的文本。例如,当将通知转换为文本时,您可以执行以下操作:
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
与我帮助您解决的其他问题类似,目标数据 已经在 table 中,因此我会使用 table 数据 来获取信息,因为它的结构布局不太可能改变 5
的目标 'g-body '
可能并不总是是美国。
我获取 数据 的方式有点不同:
set theURL to "https://www.nytimes.com/interactive/2020/world/coronavirus-maps.html?action=click&pgtype=Article&state=default&module=styln-coronavirus&variant=show®ion=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 ,\"Cases &\",,\"Deaths\"}'"
set notificationMessage to ¬
do shell script awkCommand & "<<<" & CountriesTable's quoted form
display notification notificationMessage subtitle "US COVID-19 UPDATE" sound name "glass"
- 注意: 代码用于确定页面在Safari 已在 macOS Mojave 和更高版本中完成加载工作,但是,对于 macOS High Sierra 和一些更早的版本,添加words
buttons of
在UI elements ...
前面repeat until exists ¬ ...
code.
注意:示例 AppleScript code 就是这样,不包含任何可能适当的错误处理。用户有责任根据需要或需要添加任何 错误处理 。查看 try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay 命令 在适当的事件之间可能是必要的,例如delay 0.5
,适当设置延迟的值。
我正在编写一个脚本,用于访问有关 Corona 的 NYT 网站,获取美国数据,提取数字(总数、死亡),并向我发送通知。我很接近,但是当我提取数字并显示它们时,它们被放在一起(即 700021 而不是 7000,21)。我的问题是:
如何提取数字以便将它们描绘出来?
代码如下:
set theURL to "https://www.nytimes.com/interactive/2020/world/coronavirus-maps.html?action=click&pgtype=Article&state=default&module=styln-coronavirus&variant=show®ion=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
您正在从 returnNumbersInString
处理程序中获取数字列表,但仅将列表强制转换为文本通常不会提供任何类型的格式。一种解决方案是使用 text item delimiters
指定加入列表项时要使用的文本。例如,当将通知转换为文本时,您可以执行以下操作:
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
与我帮助您解决的其他问题类似,目标数据 已经在 table 中,因此我会使用 table 数据 来获取信息,因为它的结构布局不太可能改变 5
的目标 'g-body '
可能并不总是是美国。
我获取 数据 的方式有点不同:
set theURL to "https://www.nytimes.com/interactive/2020/world/coronavirus-maps.html?action=click&pgtype=Article&state=default&module=styln-coronavirus&variant=show®ion=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 ,\"Cases &\",,\"Deaths\"}'"
set notificationMessage to ¬
do shell script awkCommand & "<<<" & CountriesTable's quoted form
display notification notificationMessage subtitle "US COVID-19 UPDATE" sound name "glass"
- 注意: 代码用于确定页面在Safari 已在 macOS Mojave 和更高版本中完成加载工作,但是,对于 macOS High Sierra 和一些更早的版本,添加words
buttons of
在UI elements ...
前面repeat until exists ¬ ...
code.
注意:示例 AppleScript code 就是这样,不包含任何可能适当的错误处理。用户有责任根据需要或需要添加任何 错误处理 。查看 try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay 命令 在适当的事件之间可能是必要的,例如delay 0.5
,适当设置延迟的值。