Google lucky links in BBEdit

Last summer, I wrote a couple of scripts for creating Markdown reference-style links in BBEdit. One of them was for new links, the other was for reusing a link that’s already in the file. What I didn’t do, for reasons that aren’t clear to me, was write scripts for adding links from selected text via Google’s “I’m Feeling Lucky” service. Yesterday I rectified that oversight.

I’d had lucky linking working in TextMate since 2006. It was based on a Ruby script I’d stolen from Haris Skiadas (one of the stars of the TextMate community at the time) that handled the Google interaction and a few commands and macros that handled the cursor movement. It was complicated to build, but it worked really well for the six years I used it.

This time I decided to do the Google interaction in Python, using Kenneth Reitz’s Requests module. Requests is a smarter, simpler version of the standard urllib2 module. The script I ended up with takes the search terms as the first item on the command line and returns to standard output the URL of the “I’m Feeling Lucky” result. I call the script glucky.

python:
 1:  #!/usr/bin/python
 2:  
 3:  import requests
 4:  import sys
 5:  
 6:  terms = sys.argv[1]
 7:  search = {'q': terms, 'btnI': "I'm Feeling Lucky"}
 8:  url = "http://www.google.com/search"
 9:  
10:  r = requests.get(url, params=search, allow_redirects=False)
11:  print r.headers['location']

From the command line, you’d use glucky like this:

glucky "scipy integration"

and it would return

http://docs.scipy.org/doc/scipy/reference/integrate.html

Line 6 gets the glucky’s first argument, which is the string of search terms. Line 7 constructs a dictionary of the parameters we’re going to pass to Google’s search command, whose URL is given in Line 8.

Line 10 performs the search via an HTTP GET request. The get function builds the search URL from url and the search parameters. It’s smart enough to do all the URL escaping for us. Normally, get would follow all redirects to take us to the final page, but we don’t need that, so setting allow_redirects to False keeps us from loading pages unnecessarily. The URL of the “I’m Feeling Lucky” page is in the header of the initial result, identified as the location of the redirect we didn’t follow.

OK, that’s a nice little script, but it doesn’t create a Markdown reference-style link. For that, we go to AppleScript. BBEdit has a nice AppleScript dictionary of cursor control commands, and since I’d worked out most of the logic for this type of linking back in August, it wasn’t hard to adapt one of my earlier scripts to this new purpose. I call the new script “Google Lucky Link” and have it bound to the ⌃⌥⌘L keystroke combination.

applescript:
 1:  tell application "BBEdit"
 2:    if length of selection is 0 then
 3:      beep
 4:    else
 5:      -- Get the number of the next reference link.
 6:      set myText to contents of front document
 7:      set myRef to do shell script "~/bin/bbstdin " & quoted form of myText & " | ~/bin/nextreflink"
 8:      
 9:      -- Get the selection and find the Google Lucky search result.
10:      set myTerms to selection as text
11:      set myURL to do shell script "~/bin/glucky " & quoted form of myTerms
12:      
13:      -- Turn selected text into link and put cursor after the reference.
14:      add prefix and suffix of selection prefix "[" suffix "]" & "[" & myRef & "]"
15:      select insertion point after last character of selection
16:      
17:      -- Add the reference at the bottom of the document and reset cursor.
18:      set savePt to selection
19:      select insertion point after last character of front document
20:      
21:      set selection to "[" & myRef & "]: " & myURL & return
22:      select savePt
23:    end if
24:  end tell

I use it this way: Select the text that will be the link and type the ⌃⌥⌘L combo. The selected text will be turned into a link, and the URL of the “I’m Feeling Lucky” result will be placed with the reference number at the bottom of the file.

As I said, the bulk of this script is a direct copy of the New Reference Link script I wrote back in August. The significant changes are:

Now that I have analogs of all three of the Markdown linking commands that I used for years in TextMate, I should probably bundle them together into a BBEdit package and put them up on GitHub. I’ll do that soon, but I want to spend a little more time with this new one first, to make sure it works the way it’s supposed to.