Can someone confirm that this script returns a list of links from a Google Search page in Safari on macOS 10.12.4?
TIA.
# Auth: Christopher Stone
# dCre: 2017/03/29 04:44
# dMod: 2017/03/29 04:44
# Appl: Safari, System Events
# Task: Extract Links from a Google Search.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Safari, @System_Events, @Extract, @Links, @URLs, @Google, @Search
------------------------------------------------------------------------------
set xpathStr to "//*[@class=\\'r\\']/a"
set strJS to "
var xpathResults = document.evaluate('" & xpathStr & "', document, null, 0, null),
nodeList = [],
oNode;
while (_oNode_ = xpathResults.iterateNext()) {
nodeList.push(oNode.href);
}
nodeList;
"
# Allow for the possibility that Safari has been hidden.
# When Safari is hidden it does NOT correctly discern window 1.
tell application "System Events"
tell application process "Safari"
if its visible is false then set its visible to true
end tell
end tell
tell application "Safari"
set linkList to (do _javascript_ strJS in front document)
if linkList ≠ "" then
return linkList
else
error "No links were found!"
end if
end tell
------------------------------------------------------------------------------