November 22, 2013

Mutt to Taskpaper

One of the things that I liked about using Taskwarrior was its integration with mutt: there were a few scripts that took messages from mutt and added them as tasks or notes in Taskwarrior. I looked around to see if I could find anything similar for Taskpaper, but had no luck. So I thought I would try to write a simple script myself. And here, without further ado, it is:

#!/bin/sh
# mutt_to_taskpaper.sh
# http://larryhynes.net/2013/11/mutt-to-taskpaper.html
# Important disclaimer: I have no idea what I'm doing
# This script will take the subject line of an email in mutt and add it to your
# Taskpaper inbox provided that "Inbox:" is the first line of your taskpaper
# file. The script can be called by a macro in mutt by adding `macro index T
# "<pipe-entry>~/mutt_to_taskpaper.sh<enter>"` to your .muttrc file, then T
# will add the todo item. (T clashes with Mutt's "Tag all" key, so you can
# remap this to whatever you like.) You may need to make sure that the script
# is executable. You can adjust the path and name of your taskpaper file below
# to suit your setup.

TODOFILE=/Users/larry/todo.taskpaper
TEMPTODO=$(basename "$0")
LINE=1
TMPFILE=$(mktemp /tmp/"${TEMPTODO}".XXXXXX) || exit 1
grep '^Subject:' | awk -F '^Subject:' '{print $2;exit;}' | sed 's/^/    - Email:/' | unexpand -t4 > "$TMPFILE" || exit 1
ed -s $TODOFILE <<SHAZAM
${LINE}r $TMPFILE
w
SHAZAM
rm -f "$TMPFILE"

I’ve tested this with a lot of different subject lines and email types (replies, forwards, etc.) and it has worked OK for me. I’m indebted to Dr. Drang for the unexpand command that he details here, and to this thread on Stack Overflow that provided the ed heredoc that inserts the todo item into the list; really all I did here was some half-decent Googling. There’s a gist of the script here if you prefer line numbering and such, I hope it’s useful to someone out there.