Chrome autolinking

See Hacks for info about pre-requisites and source code.

A few years ago, John Gruber created a little script, in the form of a service, which could automatically look up the text you had selected in Google, and paste in a link to the top hit. When you blog or edit wikis, you often want to link to something like the University of Toronto, or Boing Boing, which would be the first hit on Google, but yet you have to Alt+Tab, search, copy and paste etc. This script made it much faster.

I wanted to do something similar, but I extended it in two ways. First, I took the idea of a menu from the Bibdesk Google Scholar script. You simply select some text, type Ctrl+Alt+Cmd+G, and a menu is shown with the four best choices (sometimes you might want a choice). You then type Ctrl+Alt+Cmd+14 to choose the right link, and it is automatically inserted. Second, I use AppleScript to ask Chrome which page I am on right now, and intelligently format the link as a wikilink, a rich text link (for my blog and other rich text editors), or as a pure A HREF link.

I can also add a few words that need to be in the search, but should not be part of the link, if for example I want to link to Engineers without Borders in Canada, I can type

Engineers without Borders|Canada

and the link text will just be Engineers without Borders. Sometimes I might want to search for something completely different than the link text. I did that on this wiki page, I used

older projects on my blog[tools and hacks reganmian

to insert a link.

The two relevant scripts are imfeelinglucky.rb and imfeelinglucky-y.rb. You need a keyboard shortcut program, and create shortcuts for Ctrl+Alt+Cmd G for searching, and Ctrl+Alt+Cmd 1…4, which launch

ruby <path>/imfeelinglucky-y.rb <number>

The RTF links were the trickiest ones.

pbpaste -Prefer rtf

is supposed to print what's on the pasteboard right now in RTF, so that I could inspect an example, but it only prints plain text. I currently use this snippet

text = %q|{\rtf1{\field{\*\fldinst{HYPERLINK "URL"}}{\fldrslt TEXT}}}|

to generate the RTF, which works, but generates a line shift before and after. Any ideas on how to improve this would be very welcome!

Google AJAX/json

In the Bibdesk script, I manually parse Google Scholar, which is quite annoying. In this case, I found out about a great json API for Google searches (not sure if they have one for Google Scholar as well). I use the one-line json parser found here

def json_parse(json)
  null=nil
  return  eval(json.gsub(/(["'])\s*:\s*(['"0-9tfn\[{])/){"#{$1}=>#{$2}"})
end

and after that, the relevant section of my script is as clean and easy as

json_parse(a)["responseData"]["results"].each do |item|
  c += 1
  file << "#{item["unescapedUrl"]}\n"
  out << "#{c}: #{item["titleNoFormatting"]}\n\t#{item["unescapedUrl"]}\n"
end

It only returns 4 hits per page, which is fine for me, and you could easily request the next page if you wanted to show more hits.

I am very glad to find this script, because it was my impression that Google had gone away from providing these kinds of pure APIs to programmers.