Skip to content

Instantly share code, notes, and snippets.

@chadbailey59
Created February 21, 2014 17:54
Show Gist options
  • Save chadbailey59/9139467 to your computer and use it in GitHub Desktop.
Save chadbailey59/9139467 to your computer and use it in GitHub Desktop.
Use TextExpander to create a quick Markdown link by entering link text and the first Google result for a search term.

Save this as glucky-markdown.rb somewhere...

#!/usr/bin/env ruby

require 'uri'
require 'json'
require 'net/http'

def goog(msg)
  uri=URI.parse "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=%s"\
        % URI.escape(msg)
  req = Net::HTTP::Get.new(uri.request_uri)
  http = Net::HTTP.new(uri.host)
  http.read_timeout = 5
  http.open_timeout = 5
  res = http.start { |server|
        server.request(req)
        }
  JSON.parse(res.body)['responseData']['results'][0]['url']
end

text = ARGV[0]
term = ARGV[1]

puts "[#{text}](#{goog(term)})"

Now, create a TextExpander Shell Script snippet with the following...

#!/bin/bash
source ~/.bashrc; ~/Scripts/glucky-markdown.rb "%filltext:name=Link Text:default=Link Text%" "%filltext:name=Search Text:default=Search Term%"

I use the source ~/.bashrc to get rbenv, and you'll need to update the path to point to your copy of that script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment