Blue and gold Cloud patterns Dawn at the pier Abstract weed Capybara

24th September, 2004

Using Ruby one-liners with Quicksilver

Filed under: Technology, — bsag @ 03:09 PM

Sometimes a Unix-based system can be a beautiful thing. Take a little problem and solution I came across today. I had been writing some notes on a paper I was reading in the Vim Outliner, and wanted to paste a paragraph or two into the notes section of Endnote (a reference manager). But there was immediately an irksome problem: Endnote doesn’t understand Unix line endings, and my notes were also hard-wrapped to 72 characters, with a ‘pipe’ character (|) and tab before each line of text. Worse still, Endnote doesn’t have a find and replace command1. I could have opened up BBEdit or something similar and constructed the appropriate find and replace there, but that seemed annoyingly long-winded. Then I remembered that you can access the contents of the system clipboard directly using the commands pbpaste and pbcopy. Because they work like standard Unix commands, you can pipe other commands to them, and so build up a useful tool from a chain of smaller commands. This example shows you how to filter the text on the clipboard through a command to munge it in some way, and then put it back on the clipboard ready for pasting—no intermediate files required!

So—as I’m learning Ruby—I thought I’d knock up a quick Ruby one-liner to strip out the line feeds, the pipes, tabs and superfluous space, and replace them with a single space:

pbpaste | ruby -00 -pe 'gsub!(/nt+|s+/, " ")' | pbcopy

That just passes the contents of the clipboard to the Ruby code which replaces all the unwanted gubbins with a single space, then puts the modified result back on the clipboard. Cool, isn’t it? But not quite cool enough for me. I would have bring forward my Terminal window and type this lengthy command, which again seemed like too much work, particularly as I’m likely to need to do this many times in the future. I realized that I could select the command I’d just constructed in the Terminal and drag it onto Quicksilver’s shelf to make a clipping. Then I could run the whole thing from the clipboard. Here’s how it works:

  1. Copy the text you want to munge.
  2. Activate Quicksilver (Command-Space on my machine) and type a full stop (period) to put Quicksilver into text mode.
  3. Hover over the shelf to activate it, and drag the clipping of the command you made earlier onto the text box in the Quicksilver command window.
  4. Activate Quicksilver again—yes, your clipping is still there.
  5. Type Tab, then ‘ru’ to bring up the ‘Run in Shell’ action. Hit return.
  6. Switch to wherever you want to paste your modified text and type Command-V.

It’s much more laborious to describe than do, and there’s another benefit. Once you’ve dragged the clipping onto the command window, you can hit ‘.’ again to edit the contents. This means that you can easily make slight modifications to the command, but you can use your stored command as a guide to remind you of the correct format.

There are many other commands you could use. Want to shout at people? Try:

pbpaste | ruby -00 -ne 'puts $_.upcase' | pbcopy

There are lots of great Ruby one-liners here, but you could just as easily use Perl, or any other scripting language or Unix command. I think it’s a great demonstration of the way that you can blend traditional Unix tools with some of the newer MacOS X utilities like Quicksilver, which share much of the same philosophy, but work in a slightly different way.

1 In many ways, Endnote is unbelievably primitive—the developers seem only just to have realised that some people have scroll wheels on their mice, and that it would be nice to be able to use them.

Update: I forgot that there’s another way to get the Shelf items into Quicksilver. In the Catalog, look under Modules > Quicksilver, and you’ll find an item for Shelf & Clipboard. Make sure the the checkbox is checked and rescan. now you should be able to search for ‘shelf’ in Quicksilver, the press the right arrow to descend into the clipboard and choose your clipping. No dragging required! Or you could go the whole hog and set up a trigger for the whole sequence if there’s a one-liner you use a lot.

  1. 1

    Hey, cool. I read about Ruby in The AppleScript Definitive Guide (Matt Neuburg likes Ruby) and you taught me all I know about Quicksilver, so I thought this would be good to dip my toe into.

    At the moment, the second one-liner works fine, the first one returns the text with "nt" and "s" removed. I'm working on it...

    Also, I think you can start dragging text from (say) Safari, then - while dragging - invoke Quicksilver and drag the text into the left bezel. (No pressing . required?)

    At last, a geeky thing I can play with. (Vim will always be an abrasive cleaning powder to me...)----- Not understanding exactly what Quicksilver is, or indeed having a Mac, I can't quite understand what exactly the amazing revelation is here. However, the gist of it seems pretty cool, so I may have to bookmark this entry for when I get my hands on a Powerbook myself...

    by Matthew @ 24/09/2004 5:09 pm • Permalink

  2. 2

    Thank you!

    I was just wondering last night how to make QS run shell commands. But didn't have the time to investigate it.

    It's also worth noting that the triggers introduced in the latest version of QS make this way more useful. I have it set up now so I can press cmd-F11 QS runs a tiny Ruby script that checks my Emacs .timelog file and reports via LanOSD a nice pretty message in the middle of the screen telling me if I'm clocked in and what I'm clocked in to.

    I love it when apps play nice together.

    by Christian Metts @ 24/09/2004 6:10 pm • Permalink

  3. 3

    pete: Heh, you know why? For some reason, the backslashes got stripped out by Wordpress when I posted it. That's the reason it was replacing the n, t and s characters, rather than the line break, tab and space in your script. I've fixed it with the HTML entity for the backslash now. But it will only work if you have that pattern in your test selection, of course: new line, tab, pipe, space, text.

    Yes, the dragging works, but only if the last thing you had on the left of the command window was text (I didn't know that!), but if you had an application or a file selected last, it doesn't work, and you need to press '.'. But you can still do that while in the middle of a drag, so you don't need to save a clipping.

    Matthew: Yes, sorry, there's a bit of assumed knowledge in this post. If you do get a Powerbook, the VERY FIRST thing you must install on it is Quicksilver grin Take a look at the manual, or at Merlin's concise and very helpful introduction, which will give you a good feel for what it does.

    by bsag @ 24/09/2004 6:10 pm • Permalink

  4. 4

    Glad you fixed it so quick - I was going to investigate tomorrow!

    Quicksilver mania: in the same way that the medieval alchemists used to get strange mental hallucinations after playing with Mercury, Mac users go a bit funny after getting addicted to Quicksilver. I suppose it's because it fuses the GUI and CLI paradigms in a cool way. People do get carried away though.

    And why not.

    by pete @ 24/09/2004 9:10 pm • Permalink

  5. 5

    Christian Metts: Yes, the triggers are really useful—I've been using them since they were in alpha, and they've got better and better. The various notification systems like Growl and LanOSD (and the built in one in QS are great too.

    pete: Hehe. Yes, it's true; QS does inspire some mania. Justly in my view.

    When I realised what had happened to my code fragment, I had a sudden vision of a Sun style headline: "WORDPRESS ATE MY BACKSLASHES" :-D

    by bsag @ 25/09/2004 8:09 am • Permalink

  6. 6

    Oooooh. pbpaste and pbcopy sound really nice. I can see I'm going to be hooking those up to random perl in the same way. DIY Services, except maybe even less clunky. That along with working out how to make an 'Upload to my website' action for QS, anyway. QS and Saft are smoothing off the corners of my mac a great deal currently.

    by Howard Jones @ 25/09/2004 10:09 am • Permalink

  7. 7

    Using Ruby (or Perl or Whatever) One-Liners with Quicksilver There’s no way you can appreciate this if you don’t use a Mac and can try it out immediately, but trust me: Quicksilver is so amazingly cool it alone is a reason for getting one....

    by Stefan Tilkov's Random Stuff @ 26/09/2004 9:09 pm • Permalink

  8. 8

    What Stefan says is perfectly reasonable, at least to me!

    What finally tipped me over the edge to buying a Mac was one single app: the LifeBalance Desktop (it was only available for the Mac at the time.) There are a few others who converted for that same application, if you check out the forums.

    Oh, and I love your Ruby one-liners! ;o)

    by Nathan @ 28/09/2004 6:10 am • Permalink

blog comments powered by Disqus

Powered by ExpressionEngine :: © www.rousette.org.uk, 2002-2008 :: [XHTML] [CSS] [508]