Skip to Content

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!



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

Add a Comment

*0 / 3000 Character Maximum

9 Comments

Filter by:
MIchael

Bret & Jean,

Thanks for this GREAT Applescript. I have been looking for something like this for years. I am loyal user and supporter of TextExpander and other SmileOnMyMac products.

Bret--would love to see to see the use of a curl call to randomly grab quotes from iheartquotes.com to keep my signatures always interesting and forever changing!

Any luck with iheartquotes.com's API going back up?

Take care.

February 05 2009 at 10:43 AM Report abuse rate up rate down Reply
Jason Miller

Very nice. Thanks for this!

February 03 2009 at 9:15 PM Report abuse rate up rate down Reply
Rob

I like T.E. but had to disable the Tidbits auto correct... it would not let me type "website"... it always changed it to "Web site" (capitalized) drove me flippin nuts!
Any one know of a way to edit the Tidbits dictionary?

February 02 2009 at 9:10 PM Report abuse rate up rate down Reply
2 replies to Rob's comment
Mike

This annoyed me as well. You can stop it by making a new entry before tidbits that replaces website with website.

February 03 2009 at 12:32 AM Report abuse rate up rate down Reply
Jean MacDonald

Jean from SmileOnMyMac here.

Your best bet is to make a local duplicate of the TidBITs dictionary and then delete the linked one from your snippet groups. Then you can edit the local duplicate as you wish.

Normally, the drawback of using a local duplicate is that your snippet group won't be updated when we make changes to the group. But in the case of the TidBITs dictionary, TidBITs is not making further changes. I've made a couple tweaks in the years since we added the dictionary so that it works better with TextExpander (it was originally developed to work with Eudora). But regarding he question of how to spell website, TidBITs uses "Web site" so I have to leave that alone.

February 03 2009 at 12:54 PM Report abuse rate up rate down Reply
Bakari

I did a similar article about using TextExpander and AppleScript:
http://www.applematters.com/article/use-textexpander-to-launch-applications-and-more/

February 02 2009 at 8:22 PM Report abuse rate up rate down Reply
Justin

Mail.app has a pull down for random signatures - for free.

http://docs.info.apple.com/article.html?path=Mail/2.0/en/ml454.html

Even though the article is for Mail 2.0 it still applies for the latest Mail.app

February 02 2009 at 7:15 PM Report abuse rate up rate down Reply
2 replies to Justin's comment
Alexsander Akers

Burn…

February 02 2009 at 7:39 PM Report abuse rate up rate down Reply
Brett Terpstra

I'm not sure, but I don't think that pulldown will help much if you're using something that's not Mail.app, or perhaps in a web browser, which I've heard happens on occasion :).

The original point, though--as I mentioned near the end--was to use an API to automatically provide dynamic content. That part is, for now, to be continued...

February 02 2009 at 8:22 PM Report abuse rate up rate down Reply
Buy an ad here

Hot Apps on TUAW

Tweets

© 2012 AOL Inc. All Rights Reserved.