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]+);)/, '&')
end

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

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

input = title
puts ERB.new(ENV['TM_LINK_FORMAT']).result

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>