Fast way to pick the first result from google search

I have this macro, that does this job :

The I is needed as I use sVim.

My question is whether it is possible to make this faster somehow as right now it takes about 2 seconds for this action, I would love for that to be instant.

This is what I want :

Thank you for any help.

1 Like

Actually I can bring the timer to 0.2 s which is actually much better. Still though, I am curious, if there is any other way?

You might be able to use JavaScript to click on the first link, identified by XPath or the first element of the class. Right-click on the first result, and select “Inspect” to view the element, then right-click on the element to copy the XPath.

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

Wow, wow.

This is instant. Thank you so much Christopher.

This forum makes me really envious of knowing all the applescript wizardry. How does even come up with things like this.

Also I am quite curious, in all your scripts you make this form of author, creation date, tags and so on. This makes me wonder, how do you use this after. Do you file these scripts somewhere and index them or what?

Thank you for this wonderful solution. I actually use it quite often now, I hope to extend it to few other sites that give results from search queries like youtube or reddit :

I will try to figure out how to make it work for those websites too and ask you for help if I get stuck if you don’t mind. :slight_smile:

Speaking of it, I am trying to modify the script to work for youtube results. How would I go about changing it.

I am really confused as to what this is responsible for :

set xpathStr to "//*[@class=\\'r\\']/a" set strJS to "

Hey Nikita,

Years of following the Applescript Users List, MacScripter.net, reading AppleScript books, and scripting.

Yes, I file anything that's usefully different than what I already have on file.

Even if I don't keep a local copy it gives me a point of reference (dates, etc).

Xpath is useful but a bit difficult to wrap your head around.

Use Google Chrome.

Open the web inspector to the link you want to look at.

Then right-click and copy as Xpath.

That gives you a start.

The person on the forum who knows the most about Xpath is @ComplexPoint.

-Chris

1 Like

Get the XPath from above, then use with the below script:

See clickOnLink(document.kmvar.xPath);

But isn’t the Xpath dynamic? As in it changes with every new result?

I will try it now.

I had trouble using XPath, but found a simple search on element class worked fine.
Try this:

##Macro Library   [SEARCH] Click on First Link of Search Results [Example]


####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/c/cc4a4f1d80fa4a3222f066ae43ce120027f58998.kmmacros">[SEARCH] Click on First Link of Search Results [Example].kmmacros</a> (2.7 KB)

---

###ReleaseNotes

Do a Google Search in Chrome, then trigger this macro.

Technically the `click()` method of the HTML `anchor` element did not work, for some unknown reason.
So I had to get the  target `href` from the element and open it in a new tab/window.
Also, trying to use the `window.open()` method caused my popup blocker to kick in.
So, I just returned the URL, and used KM to open a new Chrome Tab.

---

<img src="/uploads/default/original/2X/2/29337ee5918930c09a81246c2befc3f8eb5acb1c.png" width="680" height="757">

---

###Script

```javascript
//debugger;

(function run () {

  'use strict';

  var divTopClass = "srg"
  var divClassList = document.getElementsByClassName(divTopClass);

  if (divClassList) {

    var firstLink = divClassList[0].getElementsByTagName("a")
    var urlStr = firstLink[0].getAttribute("data-href");
    
    return urlStr;
  }
  else {
    alert('Class NOT Found:\n' + divTopClass);
  }

})();
```
1 Like

Hi Chris,

I am trying to modify your script that fetches the urls of first, second, third url from google to also apply for youtube, github and reddit. Starting from youtube but I really can't decipher what is going on in this script to produce the results it produces. I have this macro right now

Let's say I search for youtube for mathematics and get this result :

Also :

I hope if you can provide a solution to this or some guidance I can try and understand more of how this script operates to extend it to GitHub, Reddit and perhaps other services with search functionality.

Hey Nikita,

I don’t understand XPath well enough yet to get it working on YouTube, et al.

The Google Chrome inspector will let you copy the XPath of elements, but I’m not getting it to work so far.

I’ve had more luck parsing the raw source, but it’s more than I want to fight with right now.

--------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: ?
# dMod: 2016/12/09 14:29
# Appl: Safari
# Task: Get the source of the front Safari page.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Safari, @Source, @ccs, @ccstone
--------------------------------------------------------------------------------

SAFARI_SOURCE()

--------------------------------------------------------------------------------
--» HANDLERS
--------------------------------------------------------------------------------
on SAFARI_SOURCE()
   tell application "Safari"
      tell front document
         set pageSource to do JavaScript "document.body.parentNode.outerHTML"
      end tell
   end tell
   
   return pageSource
   
end SAFARI_SOURCE
--------------------------------------------------------------------------------

-Chris

1 Like

Hey Chris,

Yes, I understand. Thank you for trying. I tried following @JMichaelTX approach with getting the Xpath but didn’t succeed myself unfortunately.

If you will have some time though, I would really appreciate if you or someone else can get this working. Your google search result macro saves me hours a week for sure.