我正在尝试编写一个脚本,该脚本将自动打开网页[http://www.legislation.gov.uk/new/uksi],然后单击“所有新立法”表中的所有链接。
到目前为止,我已经设法打开它,但是单击还是没有运气。
到目前为止是我的脚本:激活应用程序“ Safari”打开位置“ http://www.legislation.gov.uk/new/uksi”
要clickID()
do JavaScript "document.getElementById(id=per).click();" in document 1
end tell
以下示例 AppleScript 代码将在新的Safari 窗口中打开targetURL
,等待页面完成加载,在目标页面上检索所有URLs,在它们中搜索指向今天发布的各种法定文书的URLs,然后在新的[[tab中打开每个相同的[[window的targetURL
已打开。
set targetURL to "http://www.legislation.gov.uk/new/uksi"
set theseURLs to {}
set grepSearchPattern to ".*\\.uk/uksi/.*\\|.*\\.uk/ssi/.*\\|.*\\.uk/wsi/.*\\|.*\\.uk/nisi/.*"
tell application "Safari"
make new document with properties {URL:targetURL}
activate
end tell
tell application "System Events"
repeat until exists ¬
(buttons of UI elements of groups of toolbar 1 of window 1 of ¬
process "Safari" whose name = "Reload this page")
delay 1
end repeat
end tell
tell application "Safari"
set allURLs to (do JavaScript "var a = document.links; var x = ''; var i; for (i = 0; i < a.length; i++) { x = x + a[i].href + '|'; }; " in document 1)
end tell
try
set theseURLs to paragraphs of (do shell script "tr '|' '\\12' <<< " & allURLs's quoted form & " | grep " & grepSearchPattern's quoted form)
end try
if (length of theseURLs) is greater than 0 then
tell application "Safari" to tell front window
repeat with thisURL in theseURLs
set current tab to (make new tab with properties {URL:thisURL})
end repeat
set current tab to first tab
end tell
else
display dialog " Nothing published on this date." buttons {"OK"} ¬
default button 1 with title "All New Legislation" with icon note
end if
提示提示:将鼠标悬停在水平和垂直滚动上可查看完整代码。在注意:
do JavaScript
命令
targetURL
页面上创建所有do shell script
命令接受所有tr
可以返回与grep
匹配的URLs。grepSearchPattern
变量当前仅搜索法定文书,因为我认为这将显示在[[页面的grepSearchPattern
中的targetURL
,以及我在您发布问题以来在该URL处观察到的内容,C0]打开。如果您还希望links用于其他类型的立法,则可以调整/new/uksi
variable以适应。注意:示例 AppleScript 错误处理
的情况下,不包含任何其他的错误处理) 。用户有责任添加任何适当的,需要的或想要的。看一下targetURL
中的grepSearchPattern
statement和try statement。另请参见error。