This afternoon, Merlin Mann tweeted a link to a set of his TextExpander snippets:

As requested, here’s a few of the random TextExpander 4 snippets I mentioned in Back to Work #116:

i-0.us/18vNRnM

Merlin Mann (@hotdogsladies) Thu May 2 2013 2:41 PM CDT

I will not comment on how foolish I think it is for Merlin use abbreviations without some signaling character. I will not mention how really smart people like John Gruber and Jason Snell (semicolon men) and Brett Terpstra and Gabe Weatherhead (double comma men) design their abbreviations with signaling characters because doing so frees you from the necessity of devising unique, nonword abbreviations, a necessity that seems easy at first but turns into a pain in the ass when you have dozens of abbreviations to wrangle. No, I won’t say that because that would be churlish and discourteous to someone who’s always been very generous to me. Besides, I’ve already said it (more than once).

Instead, I’m going to suggest a possible improvement to one of Merlin’s snippets that will save countless seconds. The snippet in question is his “Better YouTube Link,” which is defined this way:

[**%filltext:name=artist% - "%filltext:name=Title%"**](http://www.youtube.com/watch?v=%clipboard) %|


You see that it requires the clipboard to be prefilled with the unique ID string of the video you want to link to. Which means you have to select the ID from the address field of your browser and then copy it to the clipboard before switching back to wherever you’re typing to invoke the snippet. Wouldn’t it be nicer if TextExpander could just extract that ID from your browser on its own? That’s the improvement I have in mind.

What’s needed is another snippet, an AppleScript snippet that extracts the ID from the URL of the frontmost tab of your frontmost browser window. If you use Safari, that snippet would look like this:

applescript:
tell application "Safari" to set ytURL to URL of front document
do shell script ("perl -e 'if ($ARGV[0]=~/[?&]v=([^&]+)/){print$1}' " & ytURL)


And if your browser is Chrome, it would look like this:

applescript:
set frontIndex to active tab index of front window
set ytURL to URL of tab frontIndex of front window
end tell
do shell script ("perl -e 'if ($ARGV[0]=~/[?&]v=([^&]+)/){print$1}' " & ytURL)


If you sometimes use Safari and sometimes use Chrome, you could use this:

applescript:
tell application "System Events"
set numSafari to count (every process whose name is "Safari")
set numChrome to count (every process whose name is "Google Chrome")
end tell

if numSafari > 0 then
tell application "Safari" to set ytURL to URL of front document
else
if numChrome > 0 then
set frontIndex to active tab index of front window
set ytURL to URL of tab frontIndex of front window
end tell

end if
end if

do shell script ("perl -e 'if ($ARGV[0]=~/[?&]v=([^&]+)/){print$1}' " & ytURL)


If you have both Safari and Chrome running at the same time, this one will try to get the ID from the frontmost tab of Safari. This may not be what you want, but I don’t know how to make the script read your mind.

Anyway, give that snippet a good abbreviation, like ;ytid, and then define a new snippet that’s very much like Merlin’s:

[**%filltext:name=name:default=Artist% - "%filltext:name=Title%"**](http://www.youtube.com/watch?v=%snippet:;ytid%) %|


Notice that this one uses the ;ytid snippet instead of the clipboard to fill in the video ID string. TextExpander’s ability to call one snippet from within another allows you to use snippets like subroutines.

Obviously, I’m assuming that the most common situation in which you’d use the Better YouTube Link snippet is when you have the video in the frontmost tab and you want to create a Markdown link to it in your text editor. If that’s not the case—if, for example, you usually want to create the link in a text entry field in another browser tab—then this won’t be an improvement for you. Stick with Merlin’s original. (But change the abbreviation to start with a semicolon.)