[TxMt] Lookup word in google and link bundle

Scott Haneda talklists at newgeo.com
Tue Oct 20 03:19:17 UTC 2009


Anyone want to take a shot at this one...
Write "foodbar" in textmate, select it, and inspire lookup word in  
google and link, it will be replaced with 'Received exception:  
undefined method '.

A lot of the time it also crashes TM.  I am on 10.5, but this has been  
happening for a long time.  I just learned of this feature, and it is  
very handy.

#!/usr/bin/env ruby -wKU
# an evolution of the code from skiadas.dcostanet.net/afterthought
# retrieves link from Google automatically and creates XHTML link
# improved call to progress bar, added ability to retrieve title
# of page and create title attribute in link using idea
# borrowed from iamrice.org.  Improved regex to handle title tags
# newlines, spaces and capitalization differences
require ENV['TM_SUPPORT_PATH'] + '/lib/progress.rb'
require ENV['TM_SUPPORT_PATH'] + '/lib/escape.rb'
require 'net/http'
require 'open-uri'
require 'erb'

def getLinkWithTitle(phrase)
   TextMate.call_with_progress(:title => "Contacting Google", :message  
=> "Retrieving Link for “#{phrase}”…") do
     response = Net::HTTP.get_response(URI.parse(URI.escape("http://www.google.com/search?q=# 
{phrase}&btnI=I'm+Feeling+Lucky")))
     url = response.to_hash['location'].to_a.first
     fp = open(url)
     title = fp.read.match(/<title>([^<>]*)<\/title>/i).to_a[1]
     title = title.strip
     return url, title, phrase
   end
end

url, title, input = getLinkWithTitle(STDIN.read)
print ERB.new(ENV['TM_LINK_FORMAT']).result
-- 
Scott * If you contact me off list replace talklists@ with scott@ *




More information about the textmate mailing list