At the request of listeners I’m going to be publishing a big list of links with future Let’s Talk Apple shows. The logical format for me to create those notes in is Markdown – it’s plain text, and quick and easy for me to add new items and re-arrange them into logical groupings. for the most part markdown has little to no overhead, but when it comes to links there is a little work. What I wanted was a way of automatically taking a URL, and turning it into a markdown link where the text for the link is the site the story is from with /… after it.

When all is done I want to turn a url like http://www.macobserver.com/tmo/article/every-important-link-from-apples-9-9-event-on-one-page into a link that looks like: www.macobserver.com/…. In other words, I need to take the URL above as input, and turn it into the following Markdown code:

My reason for choosing this format is that I want to give obvious credit to the sources of the stories, but not waste screen real-estate on long URLs.

Perl’s URI module can interpret URLs, and easily extract the host part of the URL, OS X Services can take selected text as input and replace it with processed output, Automator can create OS X Services, and Automator can execute Perl code. By putting all these pieces together I was able to solve my problem in just 20 minutes with a few clicks and a few lines of code.

You can just download the service with the link below, or you can read on to see how it’s done.

Download OS X Service …

The first step is to open Automator, create a new Service. At the top of the workflow configure the service so it receives selected text in any app, and replaces the existing text:

Automator Text Service

Next drag and drop in a Run Shell Script action and confiture it to use Perl and take input as arguments:

Automator Shell Script Action

Finally, just add in the code to do the work:

[perl]
use strict;
use warnings;
use URI;

# loop through the passed args (expecting to get just one)
ARG:
foreach my $arg (@ARGV) {
# create a URI object form the received text
my $url = URI->new($arg);

# if we didn’t get a valid URL, just print back what we got in and call it a day
unless($url->scheme() =~ m/^https?$/sx){
print $arg;
next ARG;
}

# assemble and print the markdown link
print q{[}.$url->host().q{/…](}.$url->canonical().q{)};
}
[/perl]

What this code does it take the input, try parse it as a URL (using Perl’s URI module), if it’s not a URL, just return it exactly as it came in (so our service doesn’t gobble up text when used on a non-URL). If the URL is valid, print the Markdown code for the URL in the desired format.

Once the code is in, save the service with a name of your choice (I went with md - linkify URL), and then just select some text in an app, and invoke it either by right-clicking the selecting the service, or access it via the Services menu in the app:

Using OS X Service

You can also add a keyboard shortcut for your new service by going to the Shortcuts tab in the Keyboard system preference pane:

Setting Keyboard Shortcut