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.)

## 7 Responses to “Unsolicited TextExpander advice”

1. Neal Sheeran says:

Very minor, but a semicolon requires an extra tap to get to (and one more to back) on an iOS keyboard, where—for me at least—snippets are almost more useful because I dislike typing on them. A period as always worked for me, no false positives yet.

2. Neal,
I don’t use leading semicolons on my iPhone snippets, I use a trailing z. See my second annoying link. And no, it doesn’t bother me to have different sets of snippets on the two devices. So many of my snippets are scripts that they couldn’t work on iOS anyway.

And to be even more annoying, I’ll point out there is no “iOS keyboard.” The iPad lets you type periods and commas without a extra tap or slide, but the iPhone doesn’t. I have only an iPhone.

3. Neal says:

Quite correct. Just goes to show that my disdain speaks to my unfamiliarity.

4. Steffan says:

Hi,

This is such a neat idea as I already made a snippet that creates Textile formatted links to paste into nvALT notes that eventually get posted in Basecamp Classic.

Currently

1. visit Web site
3. switch to nvALT and invoke snippet to paste the textile formatted URL (“”:URL)
4. go back to the webpage and copy the alt text that I went to represent the URL
5. switch to nvALT and paste the alt text between the “”

Plan

1. visit Web site
2. copy the alt text of the item/label
3. invoke snippet to paste the clipboard between the “” and fetch the foremost URL and combine it in the snippet

Problem

I can’t even get the snippet to pull the URL of the foremost tab. Here is the verbatim of what I am using in my snippet (that has type = AppleScript)

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


My snippet is named ;geturl.

If I invoke it in a Textedit document it runs and replaces the abbreviation but leaves nothing.

Once I can get that to work I will have a plain text snippet like this:


"%clipboard"%snippet:;geturl%


Any ideas what I am doing wrong?

Thanks,

Steffan

5. I’ve recently moved to having a colon in front of almost all of my snippets. It’s the one key that my fingers are already resting on that is not a letter character. No shift required.

Wish I had any idea what all this shell business was. Not on my mac often enough to use it.

6. Steffan,
Your scripts got turned into blockquotes instead of code blocks, but I think I got the formatting straightened out. Let me know if I messed it up.

The problem is the do shell script line, which shouldn’t be there at all. That’s an artifact of my script which wanted only part of the URL—you want the whole thing.

I think you can do the whole thing in one snippet:

tell application "Safari" to set ytURL to URL of front document
set out to quote & (the clipboard) & quote & ":" & ytURL


Remember to set the TextExpander Content type to AppleScript.

7. Steffan says:

Dr Drang,

Re: blockquotes vs code block ⇒ probably down to my Markdown incompetence :^(