运行 AppleScript 上列表的每个项目的子脚本
run a sub-script for every item of a list on AppleScript
我有一个列表,我想要这个列表的每个元素(订单号)
运行 搜索,从 chrome 抓取文本并使用此订单号浏览网站。
我已经有了两个脚本(A 和 B)
我想我可以将我的长脚本 (B) 添加到第一个
中
repeat with theItem in theResult
Script B
end repeat
但这不起作用,我收到错误
Expected “end” but found “property"
脚本 B 例如:
tell application "Google Chrome"
tell front window's active tab to set infoGrab to execute javascript "document.getElementsByClassName('even')[2].innerHTML;"
end tell
set theText to Unicode text
set theSource to infoGrab
property leftEdge72 : "<a href=\"/"
property rightEdge72 : "\">"
set saveTID to text item delimiters
set text item delimiters to leftEdge72
set classValue to text item 2 of theSource
set text item delimiters to rightEdge72
set uniqueIDKey to text item 1 of classValue
set text item delimiters to saveTID
uniqueIDKey
等等。
然后我尝试将脚本 B 保存在一个独立的脚本中,并从脚本 A 中 运行 像这样
repeat with theItem in theResult
set the clipboard to theItem
set myScript to load script file ((path to desktop folder as text) & "SEARCH.scpt")
tell myScript
end tell
delay 30
end repeat
但这也不起作用,因为脚本 B 忽略所有重复和延迟,只是 运行 一切都是即时的,对 goole 没有任何操作 chrome
问题:如何对列表的每个元素执行其他操作,包括文本分隔符等。
PS :抱歉,如果我的 post 令人困惑。
运行 脚本别名((桌面文件夹路径为文本)& "SEARCH.scpt")
我认为您的脚本有缺陷,因为 "Script B" 没有使用重复循环中的 "theItem" 变量。因此,"Script B" 将 return 每个项目的相同结果。
你的objective不清楚。也许如果您提供更多详细信息,以及源 HTML 和预期结果的真实示例数据,我们可以提供更好的帮助。
IAC,为什么要调用脚本而不是使用处理程序?
作为示例,这是重构为使用处理程序的脚本。
repeat with theItem in theResult
## You don't seem to use "theItem" in the Script B ##
# if so, then the getID() handler will return the same results for all items in this loop
set myID to my getID()
end repeat
on getID() -- was Script B
-- put in same script file, or in script library
-- does this need to have a parameter?
tell application "Google Chrome"
tell front window's active tab to set infoGrab to execute javascript "document.getElementsByClassName('even')[2].innerHTML;"
end tell
set theText to Unicode text
set theSource to infoGrab
set leftEdge72 to "<a href=\"/"
set rightEdge72 to "\">"
set saveTID to text item delimiters
set text item delimiters to leftEdge72
set classValue to text item 2 of theSource
set text item delimiters to rightEdge72
set uniqueIDKey to text item 1 of classValue
set text item delimiters to saveTID
return uniqueIDKey
end getID
我有一个列表,我想要这个列表的每个元素(订单号) 运行 搜索,从 chrome 抓取文本并使用此订单号浏览网站。
我已经有了两个脚本(A 和 B)
我想我可以将我的长脚本 (B) 添加到第一个
中repeat with theItem in theResult
Script B
end repeat
但这不起作用,我收到错误
Expected “end” but found “property"
脚本 B 例如:
tell application "Google Chrome"
tell front window's active tab to set infoGrab to execute javascript "document.getElementsByClassName('even')[2].innerHTML;"
end tell
set theText to Unicode text
set theSource to infoGrab
property leftEdge72 : "<a href=\"/"
property rightEdge72 : "\">"
set saveTID to text item delimiters
set text item delimiters to leftEdge72
set classValue to text item 2 of theSource
set text item delimiters to rightEdge72
set uniqueIDKey to text item 1 of classValue
set text item delimiters to saveTID
uniqueIDKey
等等。
然后我尝试将脚本 B 保存在一个独立的脚本中,并从脚本 A 中 运行 像这样
repeat with theItem in theResult
set the clipboard to theItem
set myScript to load script file ((path to desktop folder as text) & "SEARCH.scpt")
tell myScript
end tell
delay 30
end repeat
但这也不起作用,因为脚本 B 忽略所有重复和延迟,只是 运行 一切都是即时的,对 goole 没有任何操作 chrome
问题:如何对列表的每个元素执行其他操作,包括文本分隔符等。
PS :抱歉,如果我的 post 令人困惑。
运行 脚本别名((桌面文件夹路径为文本)& "SEARCH.scpt")
我认为您的脚本有缺陷,因为 "Script B" 没有使用重复循环中的 "theItem" 变量。因此,"Script B" 将 return 每个项目的相同结果。
你的objective不清楚。也许如果您提供更多详细信息,以及源 HTML 和预期结果的真实示例数据,我们可以提供更好的帮助。
IAC,为什么要调用脚本而不是使用处理程序? 作为示例,这是重构为使用处理程序的脚本。
repeat with theItem in theResult
## You don't seem to use "theItem" in the Script B ##
# if so, then the getID() handler will return the same results for all items in this loop
set myID to my getID()
end repeat
on getID() -- was Script B
-- put in same script file, or in script library
-- does this need to have a parameter?
tell application "Google Chrome"
tell front window's active tab to set infoGrab to execute javascript "document.getElementsByClassName('even')[2].innerHTML;"
end tell
set theText to Unicode text
set theSource to infoGrab
set leftEdge72 to "<a href=\"/"
set rightEdge72 to "\">"
set saveTID to text item delimiters
set text item delimiters to leftEdge72
set classValue to text item 2 of theSource
set text item delimiters to rightEdge72
set uniqueIDKey to text item 1 of classValue
set text item delimiters to saveTID
return uniqueIDKey
end getID