Reading time in TextMate
June 4, 2011 at 12:07 AM by Dr. Drang
Several people in the Mac blogging world have mentioned how much they like iA Writer’s reading time estimate. I have no interest in iA Writer, but the reading time estimate sounds like a good idea. I decided to add a quick command to TextMate to put the reading time estimate in a popup tooltip.
There’s not much to this command. The basic idea is to count the words in the document and divide by the average reading speed. The word count is done by the venerable Unix utility wc
, and the average reading speed is taken to be 250 words per minute, which is in the range of values I found in Wikipedia and other online sources. I don’t know, but I’d guess that iA Writer is using either that figure or a value close to it.
Here’s the command, as shown in TextMate’s Bundle Editor:
The pipeline in the Command(s) field is this:
wc | awk '{print int($2/250) ":" int($2%250/250*60)}'
wc | awk '{printf "%d:%02d", int($2/250), int($2%250/250*60)}'
The wc
command returns the word count as the second item of output, which is why you see $2
in the awk
command. The hardest part is the gymnastics in awk
that turn decimal minutes into minutes and seconds.
If text is selected, this command shows the reading time for that text; otherwise it shows the reading time for the document as a whole. I have the Key Equivalent set to ⌃⌥⌘R, which is easy to type and easy to remember. I tend to use the Control, Option, and Command modifiers for all my commands because those keys are lined up next to one another and are easy to press simultaneously.
For what it’s worth, this post comes in at 1:16.
Update 6/4/11
Reader Shawn Medero in the comments pointed out an error in my pipeline, and I’ve made the correction in the text above (the screenshot still has the error). My mistake was not to ensure a leading zero after the colon if the number of seconds was less than 10. In the original, a reading time of 3 minutes, 7 seconds would be shown as 3:7 instead of 3:07.
This particular error in formatting minutes and seconds is one I’ve made so many times that I suspect there’s some defect in my brain that’s preventing me from ever learning from the mistake. Since the error manifests itself on the average once every six times the script is run, I usually catch it pretty quickly. This time Shawn caught it for me.
Shawn also describes how to use Automator to turn this into a system-wide service instead of a TextMate-specific command. I’m beginning to think the post would have been much better if he had written it instead of me.
Now it’s 2:05.
Update 6/5/11
See this post for an improved TextMate command, a Service, and a Safari extension.