Q:

Set the OS X clipboard to a rich text link in a Ruby or shell script

I'm trying to generate a report by using a Ruby script to parse an XML document. I'm pulling link text and the actual URL and I'd like to be able to format those in a way that I could simply copy/paste the results into Pages/Numbers and have the hyperlink with correct link text immediately set. Is there a way to do this?

A:

echo -En '<a href="http://t.co">tco</a><br><a href="http://g.co">gco</a>' | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf
The output will always include styles. For some reason pbcopy -Prefer rtf doesn't include a plain text version on the clipboard so you won't be able to paste anything in plain text views.

A:

If you want to copy / paste a link with a descriptive text different from the URL into Pages, the OS X pasteboard needs to contain an URL object (not plain text or HTML). In Ruby, you can create such a URL pasteboard object using the pasteboard gem (gem install pasteboard) and the following code:
require 'pasteboard'
pasteboard = Pasteboard.new
pasteboard.put_url '<link URL>', '<link text>'

See the gem documentation, where I lifted this example wholesale.

