Adding Date Suffixes With TextExpander

Disclaimer: I don't know what I'm doing. I don't know AppleScript, code, programming or anything else of the sort. I bodged this by Googling for 'AppleScript date suffix', visiting various blogs and jamming stuff together until it worked. I'm sure there's a better way (if there is, let me know).

I use TextExpander, the text expansion app, a lot. Combined with keyboard shortcuts on iOS, I don't think I've typed my full email address in years. Mostly it helps me with repetitive / easy to forget / easy to misspell stuff (watch Merlin Mann or MacSparky in action to get the idea), but I do use one or two little tricks that I've not found elsewhere.

One of these is for dates. TextExpander's date features are great - it can drop in today's date in a bunch of different formats really easily. You can also do simple maths and slightly less simple things like generating urls for sites with custom date ranges (I'll share my 'today' Analytics snippet sometime). The only problem I've found is that TextExpander doesn't have a feature for adding suffixes (y'know 'st', 'nd', 'rd' & 'th', as in '1st', '2nd', '3rd' & '4th').

This bugged me. I like to include the full date (eg. '22nd November 2012') on invoices because I do some work for US clients and marking something '02/03/12' can mean 2nd of March to me and 3rd of February to them. I've often confused myself and likely confused others.

By default, the closest TextExpander'll come is: '22 November 2012'. That's pretty good, but I wanted '22nd'. Using a little bit of AppleScript, I came up with this:

set {day:d} to (current date)
set suffix to "th"
if d is 1 then set suffix to "st"
if d is 2 then set suffix to "nd"
if d is 3 then set suffix to "rd"
if d is 21 then set suffix to "st"
if d is 22 then set suffix to "nd"
if d is 23 then set suffix to "rd"
if d is 31 then set suffix to "st"
if d is 32 then set suffix to "nd"
(d) & (suffix) as string

That'll give you '1st' on the 1st, '2nd' on the 2nd and '3rd' on the 3rd. Just make that an AppleScript snippet, then add that snippet it to the start of your existing date generator and Bob's your uncle, today's full date.

As I said, that might be sloppy code - I don't know. It's Greek to me. I just slapped away at it until it worked. It does work, though. The above screencast maybe explains things a little more clearly (or maybe it doesn't, I'm not the clearest speaker in the world). Let's date.