AppleScript:在 javascript 中循环
AppleScript: loop in javascript
如何循环此代码以重复并为每次出现的 class 'auto-date-time' 设置不同的变量?
tell application "Safari"
set myValue to do JavaScript "document.getElementsByClassName('auto-date-time')[0].innerHTML;" in current tab of window 1
end tell
e.g : myValue1 document.getElementsByClassName('auto-date-time')[1]
MyValue2 document.getElementsByClassName('auto-date-time')[2] MyValue3
document.getElementsByClassName('auto-date-time')[3
我知道这个工作很好
tell application "Safari"
set myValue to do JavaScript "var outPut=[]; var arr=document.getElementsByClassName('sortable fraudScoringTransactionCCSummary');for (var i in arr) {outPut.push(arr[i].innerHTML)};outPut;" in current tab of window 1
end tell
但我正在尝试做不同的事情,可以吗?
我尝试了这个,但很明显看起来很糟糕 + 不能解决变量的问题
repeat with counter from 1 to 10
tell application "Safari"
set myValue to do JavaScript "document.getElementsByClassName('auto-date-time')[" & counter & "].innerHTML;" in current tab of window 1
end tell
if myValue = missing value then
exit repeat
end if
end repeat
找到它:
我需要使用重复/加号列表,虽然很确定这仍然不是正确的方法。
set myList to {}
try
repeat with counter from 1 to 1000
tell application "Safari"
set myValue to do JavaScript "document.getElementsByClassName('auto-date-time')[" & counter & "].innerHTML;" in current tab of window 1
end tell
if myValue = missing value then
exit repeat
else if myValue is not equal to "" then
set the myList to the myList & myValue
end if
end repeat
on error
--
end try
return myList
我想说的是,为 HTML 元素的集合获取 innerHTML
属性 值的 AppleScript 列表的最简洁和最有效的方法是:
tell application "Safari" to tell ¬
the front document to set ¬
myList to do JavaScript ¬
"Array.from(
document.getElementsByClassName('auto-date-time'),
x => x.innerHTML
);"
编辑 (2019-04-02):
解决下面您报告没有 return 值的评论,并代替您为上面的 JavaScript 提供控制台 return 值,这是您可以使用的替代方法尝试:
tell application "Safari" to tell document 1 to set myList to ¬
do JavaScript "[...document.getElementsByClassName('auto-date-time')]
.map( x => x.innerText );"
两个区别是使用属性 innerText
代替了innerHTML
(也可以应用到第一个JavaScript),以及用于构造的方法数组(不过,假设您是 运行 最新版本的 Safari,应该不会有什么不同)。
关于错误捕获的说明
尝试改掉以您惯用的方式使用 try
积木的习惯。如果它们的目的是防止脚本因您不希望发生的错误而终止,则不应使用它们或不明白为什么 发生错误。它们用于捕获您预测在特定情况下会出现的错误,了解它发生的原因,并允许您利用它来发挥自己的优势。否则,您所做的只是阻止意外错误提醒您脚本中的缺陷,并且 where/when 它正在出现。
如何循环此代码以重复并为每次出现的 class 'auto-date-time' 设置不同的变量?
tell application "Safari"
set myValue to do JavaScript "document.getElementsByClassName('auto-date-time')[0].innerHTML;" in current tab of window 1
end tell
e.g : myValue1 document.getElementsByClassName('auto-date-time')[1] MyValue2 document.getElementsByClassName('auto-date-time')[2] MyValue3 document.getElementsByClassName('auto-date-time')[3
我知道这个工作很好
tell application "Safari"
set myValue to do JavaScript "var outPut=[]; var arr=document.getElementsByClassName('sortable fraudScoringTransactionCCSummary');for (var i in arr) {outPut.push(arr[i].innerHTML)};outPut;" in current tab of window 1
end tell
但我正在尝试做不同的事情,可以吗?
我尝试了这个,但很明显看起来很糟糕 + 不能解决变量的问题
repeat with counter from 1 to 10
tell application "Safari"
set myValue to do JavaScript "document.getElementsByClassName('auto-date-time')[" & counter & "].innerHTML;" in current tab of window 1
end tell
if myValue = missing value then
exit repeat
end if
end repeat
找到它:
我需要使用重复/加号列表,虽然很确定这仍然不是正确的方法。
set myList to {}
try
repeat with counter from 1 to 1000
tell application "Safari"
set myValue to do JavaScript "document.getElementsByClassName('auto-date-time')[" & counter & "].innerHTML;" in current tab of window 1
end tell
if myValue = missing value then
exit repeat
else if myValue is not equal to "" then
set the myList to the myList & myValue
end if
end repeat
on error
--
end try
return myList
我想说的是,为 HTML 元素的集合获取 innerHTML
属性 值的 AppleScript 列表的最简洁和最有效的方法是:
tell application "Safari" to tell ¬
the front document to set ¬
myList to do JavaScript ¬
"Array.from(
document.getElementsByClassName('auto-date-time'),
x => x.innerHTML
);"
编辑 (2019-04-02):
解决下面您报告没有 return 值的评论,并代替您为上面的 JavaScript 提供控制台 return 值,这是您可以使用的替代方法尝试:
tell application "Safari" to tell document 1 to set myList to ¬
do JavaScript "[...document.getElementsByClassName('auto-date-time')]
.map( x => x.innerText );"
两个区别是使用属性 innerText
代替了innerHTML
(也可以应用到第一个JavaScript),以及用于构造的方法数组(不过,假设您是 运行 最新版本的 Safari,应该不会有什么不同)。
关于错误捕获的说明
尝试改掉以您惯用的方式使用 try
积木的习惯。如果它们的目的是防止脚本因您不希望发生的错误而终止,则不应使用它们或不明白为什么 发生错误。它们用于捕获您预测在特定情况下会出现的错误,了解它发生的原因,并允许您利用它来发挥自己的优势。否则,您所做的只是阻止意外错误提醒您脚本中的缺陷,并且 where/when 它正在出现。