TextExpander snippets for Google Chrome

Two things kept me from giving Google Chrome a decent tryout as my main browser:

  1. The lack of 1Password integration. I can’t live without 1Password anymore.
  2. The lack of AppleScript support. It’s not that I’m a big AppleScripter, but every day I use one or more TextExpander snippets that grab and transform the URLs of the pages I’m browsing for insertion into blog posts, notes to myself, and tweets. I can’t live without these snippets, either.

The first of these has been solved by 1Password’s Chrome extension, and the second by the recent addition of a decent AppleScript library. There’s very little to do to get 1Password working, but all my Safari-specific snippets had to be rewritten to get them to work with Chrome.

Actually, instead of just rewriting them for Chrome, I wanted to make the snippets work for Safari or Chrome, whichever happened to be the browser in use.1 This is a little bit trickier than writing snippets that are browser-specific or are set to work with the default browser. I’m not sure I have the best solution, but it’s working so far.

First up is a snippet that inserts the URL of the current page. The abbreviation is ;furl2 and the AppleScript content is

 1:  tell application "System Events"
 2:   set numSafari to count (every process whose name is "Safari")
 3:   set numChrome to count (every process whose name is "Google Chrome")
 4:  end tell
 5:  
 6:  if numSafari > 0 then
 7:   tell application "Safari" to get URL of front document
 8:  else
 9:   if numChrome > 0 then
10:     tell application "Google Chrome"
11:       set frontIndex to active tab index of front window
12:       get URL of tab frontIndex of front window
13:     end tell
14:   end if
15:  end if

Lines 1-4 see which browser is running. If it’s Safari, then the URL is obtained by calling Safari in Line 7. If it’s Chrome, the URL is obtained from Lines 10-13. Note that the two browsers have different ways of getting the URL and the code can’t be unified; Chrome doesn’t know what the front document is, and Safari has no active tab index command.

If both browsers are running, Safari “wins,” and the URL will be taken from it. This is an arbitrary choice and somewhat unsatisfying. If I were a little less lazy, I’d try to figure out a way to use browser that’s closer to the “front” of the stack of windows on my screen.

The next snippet is an example of a series of snippets that get the URLs of particular tabs. These are particularly useful when I’m writing in a textarea field on a page in one tab and I need to insert the URL of the page in another. This example has an abbreviation of ;1url and an AppleScript content of

 1:  tell application "System Events"
 2:   set numSafari to count (every process whose name is "Safari")
 3:   set numChrome to count (every process whose name is "Google Chrome")
 4:  end tell
 5:  
 6:  set tabNum to 1
 7:  
 8:  if numSafari > 0 then
 9:   tell application "Safari" to get URL of tab tabNum of front window
10:  else
11:   if numChrome > 0 then
12:     tell application "Google Chrome" to get URL of tab tabNum of front window
13:   end if
14:  end if

It gets the URL of leftmost tab, which always has an index of 1. I have similar snippets for ;2url, ;3url, ;4url, ;5url, and ;6url; the only difference between them is the number in Line 6. It uses the same browser selection logic as ;furl—in fact, all the snippets in this post use that same logic, and I won’t bother mentioning it again.

You’ll notice in this case that Safari and Chrome use the same command to get the URL. I could have refactored the code to use a line like

tell application theBrowser to get URL of tab tabNum of front window

but I chose to keep the Safari and Chrome code separate in case their commands diverge in a future release. Chrome’s AppleScript support is still young, and I wouldn’t be surprised to see it change in a later version.

Next up is a snippet that gets the frontmost URL and passes it through the Metamark (xrl.us) shortener maintained by the folks at perl.org. This service isn’t as popular as shorteners like bit.ly but has a good record for longevity—unlike, say, tr.im. The abbreviation is ;surl and the AppleScript content is

 1:  tell application "System Events"
 2:   set numSafari to count (every process whose name is "Safari")
 3:   set numChrome to count (every process whose name is "Google Chrome")
 4:  end tell
 5:  
 6:  if numSafari > 0 then
 7:   tell application "Safari" to set longURL to URL of front document
 8:  else
 9:   if numChrome > 0 then
10:     tell application "Google Chrome"
11:       set frontIndex to active tab index of front window
12:       set longURL to URL of tab frontIndex of front window
13:     end tell
14:     
15:   end if
16:  end if
17:  
18:  set safeURL to do shell script "~/bin/escapeurl " & "'" & longURL & "'"
19:  set cmd to "curl 'http://metamark.net/api/rest/simple?long_url=" & safeURL & "'"
20:  do shell script cmd

The bulk of the script is about the same as that of ;furl. The main difference comes in the last three lines, which take the address, url encode it, and pass it to Metamark for shortening according to the Metamark API. The encoding is done by a Python script called “escapeurl” that I have in my ~/bin directory. That script is

1:  #!/usr/bin/python
2:  
3:  from urllib import quote
4:  from sys import argv
5:  
6:  if argv[1]:
7:    print quote(argv[1],'/:')

If you want to use this snippet but prefer different shortening service, you’ll have to look into your service’s API and work out a new curl call for Line 19.

As with the unshortened addresses, I have a series of short address snippets that access specific tabs. Their abbreviations are ;1surl through ;6surl and their AppleScript content looks like this

 1:  tell application "System Events"
 2:   set numSafari to count (every process whose name is "Safari")
 3:   set numChrome to count (every process whose name is "Google Chrome")
 4:  end tell
 5:  
 6:  set tabNum to 1
 7:  
 8:  if numSafari > 0 then
 9:   tell application "Safari" to set longURL to  URL of tab tabNum of front window
10:  else
11:   if numChrome > 0 then
12:     tell application "Google Chrome" to  set longURL to  URL of tab tabNum of front window
13:   end if
14:  end if
15:  
16:  set safeURL to do shell script "~/bin/escapeurl " & "'" & longURL & "'"
17:  set cmd to "curl 'http://metamark.net/api/rest/simple?long_url=" & safeURL & "'"
18:  do shell script cmd

with the number in Line 6 set appropriately.

Lastly, I have a snippet that works just like ;surl except that it puts the shortened URL in parentheses, a format I find helpful when tweeting. The abbreviation is (;surl and the AppleScript content is

 1:  tell application "System Events"
 2:   set numSafari to count (every process whose name is "Safari")
 3:   set numChrome to count (every process whose name is "Google Chrome")
 4:  end tell
 5:  
 6:  if numSafari > 0 then
 7:   tell application "Safari" to set longURL to URL of front document
 8:  else
 9:   if numChrome > 0 then
10:     tell application "Google Chrome"
11:       set frontIndex to active tab index of front window
12:       set longURL to URL of tab frontIndex of front window
13:     end tell
14:     
15:   end if
16:  end if
17:  
18:  set safeURL to do shell script "~/bin/escapeurl " & "'" & longURL & "'"
19:  set cmd to "curl 'http://metamark.net/api/rest/simple?long_url=" & safeURL & "'"
20:  set shortURL to do shell script cmd
21:  
22:  get "(" & shortURL & ")"

I think this one is self-explanatory.

That’s 15 TextExpander snippets in one blog post. Such a bargain!

Update 11/1/10
There’s now a GitHub repository for all these snippets as well as a couple of snippets for Flickr pages.


  1. Because I’m still testing, I don’t know yet whether I’ll switch from Safari to Chrome, and I’m likely to be using both of them off and on for a while. ↩︎

  2. My snippet naming convention is described in this post↩︎