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:


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.

5 Responses to “Markdown reference links from Safari tabs”

  1. Kaushik says:

    minor suggestion on line 5/line 6?

        & the URL of t & "

    to just

        & the URL of t & return
  2. Dr. Drang says:

    I’ve used return in other scripts, Kaushik, and have had trouble with it (although I can’t for the life of me remember what the trouble was). My tendency, then, is to type "\n" when I need a linefeed, and the AppleScript Editor, which is where I write and test scripts, turns it into what you see. I agree that it looks stupid, but it always does what I expect, a rarity when writing in AppleScript.

  3. Lauri Ranta says:

    return is the constant for carriage return; most of the time you should use linefeed instead. AppleScript Editor currently uses CR line endings by default, but LF line endings in strings. In any case, it’s often better to use the constants to avoid errors.

    The gist was probably something like this:

    set links to {}
    tell application "Safari" to tell window 1
        repeat with i from 1 to number of tabs
            set end of links to "[" & i & "]: " & URL of tab i
        end repeat
    end tell
    set text item delimiters to linefeed
    links as text
  4. Dr. Drang says:

    Welcome back, Lauri! I wondered if it was a CR/LF thing but was too lazy to look it up. I’ve changed my script to use linefeed instead of "\n".

    If it were any other language, I’d like the way you put the links in a list and then combine them at the end, but whenever I see AppleScript’s set text item delimiters I feel like an animal that has to chew its leg off to escape from a trap.

  5. Daniel Driver says:

    This is wonderfully useful. Thanks for posting the details.