OS X Service for natural language dates

[Tweet : ADN : nvALT]

Updated to 1.3 on 6/1/13, more info here.

I knocked out a quick project before I got around to my first App Review, so I figured I’d go ahead and share it…

calendar illustrationA few people have requested a TextExpander set for dates. The problem is, getting the current date is easy with TextExpander variables, so that one’s not worth the time to me. Anything else requires some scripting, and to be really flexible the set would need to include a very large array of snippets; more than I could possibly keep straight. It could be done with one snippet if you could use the Fill In dialog as an input to a shell script, but you can’t (are you listening Smile?).

Instead, I whipped up a System Service this morning that lets you use keywords and natural language date input to return the specified date in various formats. You type out something like date next tuesday, select the text and run the Service and it will give you back 8/23/11. If there’s no matching keyword at the beginning, it will assume %c (strftime formatting) and give you back Tue Aug 23 12:00:00 2011.

The keywords are:

date = ‘%m/%d/%y’ (slashed date)
“date today” gives you “8/20/11”
local = ‘%F’ (localized date)
“local today” gives you (well, me) “2011-8-20”
short = ‘%a, %b %d, %Y’ (abbreviated full date)
“short today” gives you “Sat, Aug 20, 2011”
long = ‘%A, %B %d, %Y’ (long full date)
“long today” gives you “Saturday, August 20, 2011”

The language you can use depends on whether or not you have the “chronic” rubygem installed. If you can install rubygems, go to Terminal and run sudo gem install chronic. If that fails, no worries, it will use PHP’s strtotime to parse your string. It has a slightly more limited vocabulary, but anything like “wednesday,” “yesterday” or “next friday” will work.

It installs like any other service, and you can set a keyboard shortcut for it in System Preferences -> Keyboard -> Keyboard Shortcuts -> Services. Refer to my how-to on Installing System Services for a step-by-step explanation. This service was written on Lion, but should work fine on Snow Leopard, too.

Feel free to extend the script with more keywords and formats. It’s not too technical, just open up the workflow in Automator and find the case block for the keywords (around line 52). Then, copy the when 'long' then '%A, %B %d, %Y' format and paste new lines (or edit existing ones) before the end statement, keyword first then format (again, see PHP’s documentation for strftime). Both Chronic and strtotime accept times as well, so if you wanted to do something like date friday 3pm, you could add the format string for a time specification to an existing or new keyword.

Convert Date - Natural Language Date Service v1.3

A System Service which takes selected text and parses it into a standard date format, using keywords at the beginning to define which format to use.

Updated Sat Jun 01 2013.

DonateMore info…

Update: If you’re looking for something similar in Python, Macdrifter has posted an alternative version which works with Keyboard Maestro. Check it out.