Hampton Lintorn Catlin

Macruby Clipboard / Pasteboard

I am currently working on a macruby app for OS X and I needed to read off the Clipboard (aka “Pasteboard” in OS X parlance). At first, I started reading the nutty Cocoa native implementation… but it did way more than I needed and was far more complex than I wanted. I found this little gem though and thought I’d share it with you. We can do it easily in the Ruby half of macruby!

IO.popen('pbpaste') do |clipboard|
          puts clipboard.read
        end

If you want to write:

IO.popen('pbcopy', 'w').print "Text to go on clipboard"

Simplez!


Comments

Nov 2, 2010
Carl said...
I ended up doing the equivalent thing in Python. I had a bunch of crazy Carbon imports, but it turned out to be simpler to just use a process. One of things about this is though that in earlier versions of OS X the pb-utils didn't support Unicode, so you really did have to go through all that crap with Carbon and whatnot. Nowadays though pretty much everything is UTF-8, so the problem has solved itself.