Fast way to pick the first result from google search

Hey Nikita,

Look at this:

Stripping the first link and loading it in Safari is easily done. It can be added to the AppleScript or done with Keyboard Maestro functions.

Here's how to do it with AppleScript:

--------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/10/10 19:40
# dMod: 2016/10/10 19:51
# Appl: Safari
# Task: Pick First Link from Google Search and Load It in the Front Window.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Safari, @Pick, @First, @Link, @Google, @Search, @Load, @Front, @Window
--------------------------------------------------------------------------------

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;

"
tell application "Safari"
   set linkList to (do JavaScript strJS in front document)
   
   if linkList ≠ "" then
      set AppleScript's text item delimiters to linefeed
      set firstLink to item 1 of linkList
      set URL of front document to firstLink
   end if
   
end tell

--------------------------------------------------------------------------------

This can be considerably customized of course.

-Chris

1 Like