Some New Year’s Hacking

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

2 Responses to “Some New Year’s Hacking”


  1. 1 paul

    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

  2. 2 Jody

    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.

Leave a Reply


Subscribe

    RSS El Camino de Los Internets

    • Morality: 2012: Online Only Video: The New Yorker - The social and cultural psychologist Jonathan Haidt talks with Henry Finder about the five foundations of morality, and why liberals often fail to get their message across. From ?2012: Stories from the Near Future,? the 2007 New Yorker Conference.

    • Life With Alacrity: Community by the Numbers, Part II: Personal Circles - Unlike the group limits, personal limits actually measure something different: the number of connections that an individual can hold. They're yet another thing that you must consider when thinking about communities of people. (I'm looking for more info on Familiar Strangers).

    • Findings - For Good Self-Control, Try Getting Religious About It - NYTimes.com - "Religious people, he said, are self-controlled not simply because they fear God?s wrath, but because they?ve absorbed the ideals of their religion into their own system of values, and have thereby given their personal goals an aura of sacredness. He suggested that nonbelievers try a secular version of that strategy." . . . "So what?s a heathen to do in 2009? Dr. McCullough?s advice is to try replicating some of the religious mechanisms that seem to improve self-control, like private meditation or public involvement with an organization that has strong ideals."

    • Single Spot Camping - SingleSpotCamping.com? is a brand new website which offers all land- and lot owners to present their own "single-spot-camping". The idea is: Anyone who owns land (it can even consist of your garage entrance) and would like to welcome one or a couple of camping guests are welcome to connect to www.singelspotcamping.com.

    • Lunch Break, Red Bank, NJ - If you're looking to make an end-of-year donation, you could do worse than Red Bank's Lunch Break. These guys need a website overhaul, badly. No online donations and I had to use google to find out whether or not my donation was tax-deductible.

    • Review: Radioshift Touch for iPhone | iPhone Central | Macworld - listen to radio stations on your iphone

    • EveryTrail - iPhone Application - With this geotracking application, you can record your movements, take geotagged photos, make notes and immediately upload it all to EveryTrail, the leading online community for travel storytelling

    • E-Democracy.Org - Discussion Forums with Political, Elections, News, and Government Links

    • Helder Luis -

    • What is Equally Shared Parenting? - Equally sharing the care of your children with your partner is about balancing your life, balancing your family's collective life and sharing equally in the joys of raising a family.

    Add Jim to your del.icio.us network