I tend to do most of my organizational overhauling in the Autumn as opposed to the usual route of New Year’s Resolutions. This may be because I still tend to adhere to a more academic schedule with September through June being my peak productivity periods and July/August being my downtime. So, last summer I really started going to town with “Getting Things Done” (David Allen). I’d been using it for at least a year before finally finding a way to work it into my day to day routine and since September I’ve had a very nice emacs-centric GTD workflow in effect (most notably, i figured out how to get my emacs planner file to my palm via, Plucker and a bit of applescripting that you can get here). However, until this week i was missing a big piece of the workflow puzzle; namely, the integration of Mail.app into emacs/GTD. So I hacked together the following applescript.
I receive an average of 100 non-spam emails a day. Of those 100 messages, an average of 50 per day require that I actually *do something* with the message. In true GTD fashion, if the *doing something* takes less than 2 minutes (though my cutoff is closer to 1 minute), I just do it as I’m reading the message. However, if it takes more than a minute to deal with, i need to track it but don’t want it in my inbox. Up until this week, I’d been using a Mail.app folder named *requires action* to hold these messages and at any point in time it contained between 30 to 200 messages that required some kind of action. However, there was no really easy way to parse this folder to see what exactly i had to do and I wished i could integrate the messages into my emacs/GTD setup. Now, I could use emacs as my IMAP client and spent some time actually trying to do this but the fact is that Mail.app supports emacs keybindings and has a great interface, so i wasn’t about to give up my fav client just to achieve email/emacs/gtd integration.
Reading Merlin’s post on Quicksilver/appending text to an arbitrary file got me thinking about a solution (though I *still* can’t get Quicksilver to append text) that I implemented in applescript. The script below (which the adventurous could tweak for their own purposes) is executed with a keystroke via Quicksilver. Upon execution it takes the contents of the mail message I’m currently looking at, writes it to a text file in my emacs-wiki sub-dir, prompts me for a todo-subject line for the associated message and appends to my Inbox.txt file a wiki link with that subject pointing to the text copy of the message. Works totally sweet. Now I can ditch my requires action folder and actually know what i need to do with all my incoming mail ;-)
–Open this script in a new Script Editor window.
using terms from application "Mail"
on perform mail action with messages the_selection
if the_selection = {} then
beep
return
end if
tell application "Mail"
repeat with i from 1 to (count of the_selection)
set senderstamp to sender of (item i of the_selection)
set msgBody to content of (item i of the_selection)
set datestamp to date sent of (item i of the_selection)
set y to year of datestamp
set m to month of datestamp
set d to day of datestamp
set h to time of datestamp
set fname to "msg_" & y & m & d & h
set _TempFile to "home:sjwillis:Documents:txt:Plans:mail_messages:" & fname & ".txt"
try
close access file _TempFile – Make sure the file wasn’t accidentally left open
end try
set myFile to open for access file (_TempFile) with write permission
set eof file (_TempFile) to 0 – clear the file contents
write "Mail Message From: " & senderstamp & " on " & datestamp & msgBody to myFile starting at eof
set clippy to "[[./mail_messages/" & fname & ".txt][mail msg]]"
close access myFile
tell application "Finder"
activate
set result to ""
set todoText to ""
display dialog "What is the action here?" default answer "do this"
set dialogInfo to result
set todoText to text returned of dialogInfo
end tell
do shell script "echo ‘" & (ASCII character 10) & "per " & clippy & ": " & todoText & (ASCII character 10) & " ‘>> /home/sjwillis/Documents/txt/Plans/Inbox"
set the clipboard to clippy
return todoText
end repeat
end tell
end perform mail action with messages
end using terms from
using terms from application "Mail"
on run
tell application "Mail" to set sel to selection
tell me to perform mail action with messages (sel)
end run
end using terms from


Add Jim to your del.icio.us network
I had to change your script to this:
using terms from application “Mail”
on perform mail action with messages the_selection
if the_selection = {} then
beep
return
end if
tell application “Mail”
repeat with i from 1 to (count of the_selection)
set senderstamp to sender of (item i of the_selection)
set msgBody to content of (item i of the_selection)
set datestamp to date sent of (item i of the_selection)
set y to year of datestamp
set m to month of datestamp
set d to day of datestamp
set h to time of datestamp
set fname to “msg_” & y & m & d & h
set _TempFile to “untitled:Users:pdebruic:Plans:mail_messages:” & fname & “.txt” as text
try
close access file _TempFile
–close access myFile
end try
set myFile to open for access _TempFile with write permission
set eof myFile to 0 — clear the file contents
write “Mail Message From: ” & senderstamp & ” on ” & datestamp & msgBody to myFile starting at eof
set clippy to “[[./mail_messages/” & fname & “.txt][mail msg]]”
close access myFile
tell application “Finder”
activate
set result to “”
set todoText to “”
display dialog “What is the action here?” default answer “do this”
set dialogInfo to result
set todoText to text returned of dialogInfo
end tell
set asdf to “echo ” & clippy & “: ” & todoText & ” >> /Users/pdebruic/Plans/Inbox”
do shell script asdf
set the clipboard to clippy
return asdf
end repeat
end tell
end perform mail action with messages
end using terms from
using terms from application “Mail”
on run
tell application “Mail” to set sel to selection
tell me to perform mail action with messages (sel)
end run
end using terms from
but it worked great. thanks for sharing the original
Hi Jim,
I hacked together a direct mail-to-planner interface for either tasks or notes. You could check it out at http://web.uvic.ca/~jklymak/OsxPlanner.html
It calls remember or planner-create-task via emacsclient.
Let me know if you have improvements.