April 2008 Archives

Pastie from the Mac Clipboard

Giles Bowkett brought us Pastie of the Mac clipboard from IRB via the Utility Belt gem. If you aren’t using it already, ‘sudo gem install utility_belt’. I highly recommend it.

That said, I’ve since longed for the same capability but from the command line.

First, I tried

  require 'rubygems'
  require 'utility_belt'

  pastie

but Utility Belt depends upon Wirble which depends upon IRB. Meh.

So I peeked at the Utility Belt code, borrowed macclipboard.rb, and made a few tweaks. But then I thought to do a google (which I really should’ve done first).

Thus began the yak shaving.

I happened upon this nifty little article by inestimable Dr. Nic about using sake (“System-wide Rake”) with Pastie which in turn led me to Chris Wansrath’s initial post describing sake.

And, thus, this little monster was born in my TextMate:

  namespace :pastie do
    desc 'Copies the contents of the Mac OS X clipboard to pastie; borrowed from the Utility Belt gem'
    task :clip do
      if RUBY_PLATFORM =~ /darwin/
        require 'net/http'
        class MacClipboard
          class << self
            def read
              IO.popen('pbpaste') {|clipboard| clipboard.read}
            end
            def write(stuff)
              IO.popen('pbcopy', 'w+') {|clipboard| clipboard.write(stuff)}
            end
          end
        end

        pastie_url = Net::HTTP.post_form(URI.parse("http://pastie.caboo.se/pastes/create"),
                                         {"paste_parser" => "ruby",
                                          "paste[authorization]" => "burger",
                                          "paste[body]" => MacClipboard.read}).body.match(/href="([^\"]+)"/)[1]
        MacClipboard.write(pastie_url)
        system("open #{pastie_url}")
        pastie_url
      else
        puts "Sorry, get a Mac ;-)"
      end
    end
  end

To obtain, just do this:

  sudo gem install sake
  sake -i http://pastie.caboo.se/187520.txt

Unless you really enjoy typing, modify your .bashrc (or whatever floats your boat) with:

  alias pastie='sake pastie:clip'

Finally, on your Mac, just select whatever you want pastied, CMD-C, then ‘pastie’ in your terminal. And done.

That said, it would be pretty sweet to have the ability to access that feature via a right click/context menu from any app that permits a copy, wouldn’t it?

Update 5/10/07: This is totally obviated by the latest updates to the Utility Belt gem – which now includes a pstie script

Posted by evan on Apr 26, 2008