Markdown reference links from Safari tabs

There is absolutely nothing original in this post. The idea for grabbing all the open browser tabs and turning them into Markdown reference links is stolen directly from the Tabs to References command in Brett Terpstra’s Blogsmith Bundle. I don’t use Blogsmith because over the years I’ve developed my own idiosyncratic set of blogging commands, and I don’t want to teach my fingers new ones. But ever since I learned of Tabs to References, I knew I needed to make one of my own. A few days ago, I finally got around to it.

The motivation is simple: you’re going to write a post, and your research links are open in tabs in your browser. Instead of creating the links one at a time as you write the post, you create them all in one fell swoop down at the bottom of the file and then refer to them as you write. Late last year, I wrote a TextMate command that lets you choose from among the reference links defined in the current file, so all I needed was the command to put all the reference links there in the first place.

I decided to go with an AppleScript TextExpander snippet.

Markdown reference links to all Safari tabs

The content of the snippet is

 1:  tell application "Safari"
 2:    set i to 1
 3:    set links to ""
 4:    repeat with t in every tab in front window
 5:      set links to links & "[" & i & "]: " & the URL of t & linefeed
 6:      set i to i + 1
 7:    end repeat
 8:    return links
 9:  end tell

and the abbreviation is ;links. If you’re a Chrome user, just change “Safari” in Line 1 to “Google Chrome.” If you’re a Firefox user, you’ll have to do some JavaScript thing that writes to the clipboard; Firefox doesn’t have an application-specific AppleScript library.

(If you look in the comments to my post about the reference link chooser, you’ll see that the mysterious Lauri Ranta (Lri) wrote an AppleScript that presumably does exactly what mine does and made it available as a Gist. I’d have been happy to use his script, but he’s taken it down. I’m sure mine is very much like his; there just aren’t that many ways to do this.)

Update 7/3/12
Based on Lauri’s comment below, I edited Line 5 to use linefeed. The two comments before his, which discussed the script before that change, won’t make much sense now. Just trust me that they made sense originally.

Putting the cursor at the bottom of the file and typing ;links generates text that looks (for this post) like this:

[1]: http://bundle.weblogzinc.com/docs/tabs%20to%20references%20(safari).php
[2]: http://bundle.weblogzinc.com/docs/index.php
[3]: http://www.leancrew.com/all-this/2006/03/markdown-links-in-textmate-the-final-frontier/
[4]: http://www.leancrew.com/all-this/2006/07/lucky-linking/
[5]: http://www.flickr.com/photos/drdrang/7484191672/
[6]: http://www.leancrew.com/all-this/2011/09/markdown-reference-links-in-textmate/

I don’t think this will eliminate my need for the other reference link commands I made—commands I’ve been using since way back in 2006 and described here and here—because I often think of new things to link to as I write. But it should cut down on some of the back-and-forth between TextMate and Safari.