The Pug Automatic

Batch-open URLs from clipboard

Written July 16, 2006. Tagged Ruby, OS X.

I often find myself wanting to batch-open several URLs from somewhere, typically an IM message log.

I've previously written a Greasemonkey script, Batch URL input, in an attempt to solve this in at least the IM case. However, this requires an effort on the part of the person you're talking to, and also that they send their URLs all at once.

Recently, I came up with a better and more general solution.

I simply have to copy some text containing URLs from wherever, and then run a Ruby script. Using Quicksilver, running the script is only a matter of double-tapping the Command key, typing the first few letters of the script name, and hitting Return. The URLs should open in your default browser.

The script

#! /usr/bin/env ruby

url_pattern = %r{https?://\S+}i
url_antipattern = %r{[^\w/]+$}i

`pbpaste`.scan(url_pattern) do |url|
url.sub!(url_antipattern, '')
url.gsub!('"', '\"') # Avoid command injection
system(%Q{open "#{url}"})

The url_pattern is a regular expression that matches a http:// or https:// and onwards until it runs into whitespace or out of text. The url_antipattern is used to strip any trailing punctuation from this. This is obviously not a very correct method for matching URLs, but it works fine for my purposes. If you often find yourself exchanging prospero:// URLs with friends, though, you might want to use this behemoth instead…

Make the script runnable with e.g. chmod u+x clipurl.rb.

Hooking it up with Quicksilver

Install the Terminal plug-in if you don't have it already.

Make sure the catalog is updated to include the script. This should happen automatically within ten minutes, or can be done manually from within the Quicksilver preferences.

Open the Quicksilver command interface. Start typing the name of the script. Mine is called Clipboard open URLs.rb.

Tab to the action slot. Select "Run". I moved "Run" above "Run in terminal" in the General > Actions > Files & Folders settings, so that it is the default action when selecting a script.

And that's it. Just copy some text containing URLs and then run the script.

Test with this:

Frou, frou (