Advertisement

Random Signatures with TextExpander and AppleScript

TextExpander, a $29.95US utility which inserts snippets of text or images when you type a preset string of characters, has long been a TUAW favorite. I only recently learned that it can run AppleScripts within a snippet, which opened up a world of fun for me. Here's a quick script demonstrating how AppleScript can be used to randomize quotes within your email signature.

Create a new TextExpander snippet and set the content type to "AppleScript." Copy and paste the code that follows into the snippet content box. I'll run through what it does in at the end.

Read on for the script!

set _date to do shell script "date +'Sent on %A, %b %d at %l:%M %p'|tr -s ' '" 
set _out to "
-Brett
" & _date & "
________________________________
Brett Terpstra | myemail@mydomain.com
The Unofficial Apple Weblog  http://www.tuaw.com

"
set quotelist to ¬
{"Never ask what sort of computer a guy drives. If he's a
Mac user, he'll tell you. If not, why embarrass him?
-- Tom Clancy", ¬
"The surest way to corrupt a youth is to instruct him to hold in
higher regard those who think alike than those who think differently.
-- Friedrich Nietzsche", ¬
"I wrote an ad for Apple Computer:
\"Macintosh - We might not get everything right, but at least we
knew the century was going to end.\"
-- Douglas Adams", ¬
"Well, to create a new standard it takes something that's not just
a little bit different, it takes something that's really new, and
really captures people's imagination. And, the Macintosh, of all
the machines I've ever seen, is the only one that meets that standard.
-- Bill Gates", ¬
"Good artists copy...great artists steal.", ¬
"Appearance Version 1.1: Does that Theme Switchin' Thang.", ¬
"Macintosh Jr.: The Power to Crush the Other Kids"}

set _out to _out & item (random number from 1 to count quotelist) of quotelist

return _out

Ok, so let's take a quick look at what we've got.

Setting the date

set _date to do shell script "date +'Sent on %A, %b %d at %l:%M %p'|tr -s ' '" 

This line sets the date by calling the date command of the shell. Yes, this can be done with pure AppleScript, but this is way faster. Cory covered integrating shell commands into AppleScript a while back. This line includes the text "Sent on" at the beginning of the format string, so no further formatting is necessary.

The static content

This is the part of the signature which doesn't change:

set _out to " -Brett "[...]

We're creating a variable called _out to hold our output, and using line breaks in the script to format it. When a quoted section ends followed by an ampersand (&), whatever follows that ampersand is concatenated (added) to the string. This happens in the middle of the sig where we insert the contents of the _date variable we created in the first line.

A list of quotes

The line set quotelist to {...} creates a "list", AppleScript's version of an array. We're populating it with a series of quotes, you can insert whatever you want to randomize here. Items in a list are surrounded by double quotes, and separated by commas. If you want to include actual double quotes within a list item, you'll need to escape them with a backslash, i.e. "\"".

The "¬" character tells AppleScript that the line is continued, and is used here only for formatting. You can remove those characters and bring the lines together in your script, if you like.

Randomizing

set _out to _out & item (random number from 1 to count quotelist) of quotelist

This line picks a random number between 1 and the number of items in your list, adding the results to the end of your _out variable. There, you just randomized your signature. All that's left to do is return the value of _out to TextExpander, which will paste it into your document when it's triggered.

return _out

I'm triggering this with the string '--='. '-=' triggers "-Brett", but '--=' triggers the full signature.

The quotes used above were collected from iheartquotes.com. I was originally using iheartquotes because they have an API, and I could just grab a quote from the web with a curl call. The API broke yesterday, so this is my replacement. If it comes back up, I'll do an update showing how to grab text from the web for TextExpander.

Also... I just saw Willow come down the wire, another text replacement app (in beta) which can also execute AppleScript, as well as Python and other shell scripts. You have to request access to the beta, but I noted in their feedback documentation that they'll be giving out free licenses to the most helpful beta testers!