# Safari tab URLs via TextExpander

For some time now I’ve been using a TextExpander snippet to insert the URL of the frontmost Safari tab into whatever I’m writing.1 Initially I was using TypeIt4Me to do this, but because of some funny results on my G4 iBook, I switched to TextExpander. Today it dawned on me that I don’t have to restrict myself to the frontmost tab—I can write snippets to get the URL of any tab. So I did.

Why would I want to do this? Quite often, when gathering background information for a post, I end up with several tabs open in Safari. As I write the post, I want to insert links to those pages. Before today, I had two ways of doing this:

1. In Safari, go to each tab and copy its URL. Because I use Jumpcut, a clipboard history utility, all of these URLs will still be available to me. Switch to my text editor and start inserting the links. This method allows me to stay in the text editor, but it does force me to scroll through the clipboard history to find the appropriate URL.
2. When I need to insert a link, switch to Safari and click on the tab of interest. Switch back to the text editor and use the ;furl snippet2 to insert the URL of the frontmost tab. With this method, there’s no scrolling, but there is a lot of switching between Safari and the editor.

Clearly, it would be more efficient to have several snippets like ;furl: one that inserts the URL of tab 1, one that inserts the URL of tab 2, and so on. It turns out these are very easy to write, even with some error handling. I have them set up as AppleScript snippets of the form

1:  try
2:    tell application "Safari" to get URL of tab 1 of front window
3:  on error
4:    "None"
5:  end try


where the tab number in Line 2 changes as appropriate. I have 6 of these snippets defined in TextExpander, with triggers ;1url, ;2url, etc.

The tab numbers are based on the left-to-right position of the tabs—even if you rearranged them. So if you take the third tab, accessible via ;3url, and slide it all the way to the left, it becomes tab 1 and is then accessed by ;1url.

I’m not sure how valuable the error handling is. I put it in because it seemed like the right thing to do—serious coders practice defensive programming, don’t they?

As long as I position my text editor window low enough on the screen to keep the Safari tabs visible, I can now write my posts with no scrolling through the clipboard history and no switching back and forth to Safari.

I’m sure there are ways to do the same thing with other browsers, but to borrow a phrase I remember from math textbooks, I leave that as an exercise for the reader.

1. I also have a snippet for inserting a shortened URL—using xrl.us—for the frontmost tab. It’s described in this post↩︎

2. ;furl is my TextExpander snippet for inserting the frontmost URL. It’s a one-line AppleScript snippet: tell application "Safari" to get URL of front document↩︎