Link up the site you’re looking at in TextMate

My buddy ReinH asked me in an off-handed way if I knew how to link up a url dragged from safari into TextMate.

I couldn’t think of an easy way, so I hacked something up. It doesn’t do the original goal, but gets pretty close. It grabs the url of the foremost safari window, and makes a textmate link (like how command+shfit+l does).

If you want to download it, you can grab the “My Junk Bundle”. If you’d rather just put the code into your own bundles, here it is:

#!/usr/bin/env ruby -wKU
require ENV['TM_SUPPORT_PATH'] + '/lib/escape.rb'
require 'erb'
require 'open-uri'
require 'net/http' 

def entity_escape(text)
  text.gsub(/&(?!([a-zA-Z0-9]+|#[0-9]+|#x[0-9a-fA-F]+);)/, '&')

url = %x{osascript <<\EOF
tell app "Safari"
  return the URL in document 1
end tell

title = url
if url =~ /^http:\/\// and url != ''
  eval 'title =<title>([^<>]*)<\/title>/i).to_a[1].strip rescue nil' if fp = open(url) rescue nil

input = title