I’m fascinated by http://to. It’s a url shortener with no TLD. Some browsers, and apparently some DNS setups, don’t like the urls it creates (they want a .com or .org or anything at the end), so it’s not exactly in heavy usage. Still, I wish they had an API. In lieu of that, here’s a quick Ruby script that will run on a stock OS X install as a System Service. It’s so simple that I’m not even going to package it up… It’ll be a good chance to show the steps for creating your own utilities using Snow Leopard Services:

  • Open Automator and choose “Service” from the first menu.
  • Tell it that the service “receives text” in “any application”.
  • Drag the “Run Shell Script” action from the left side.
  • Set the Shell to Ruby
  • Paste in the code below (or something of your own, hopefully far more interesting)
  • Save it with an intuitive name
  • Use it.
require 'net/http'
require 'cgi'

input = STDIN.read
http = Net::HTTP.new('to.')
path = '/'

data = "url=#{CGI.escape(input.strip)}"
headers = {'Content-Type' => 'application/x-www-form-urlencoded'}

resp, data = http.post(path, data, headers)

if resp.code.to_i == 200
  print data.split(/value="/)[1].split(/"/)[0]
else
  print input
end

Basically, anything you can do to or with text or files using a major scripting language, you can make into a Snow Leopard service. Now that I’ve set the bar this low, I’d love to see what new Services everyone else is cooking up!

Update: Yeah, that didn’t work out so well. It works sometimes, but leaves url parameters in the shortened url half the time. I haven’t figured out why, but I’ll leave it up and see if someone can point out the error of my ways…Fixed it, just neglected to notice it needed a POST, not a GET

Update 2: Here, this one’s shorter and more useful :)

require 'open-uri'
require 'cgi'

input = STDIN.read
print open("http://is.gd/api.php?longurl=#{CGI.escape(input.strip)}").read