One thing I missed in Mail.app was the ability to create new tasks from incoming mail. But the “Run AppleScript” action comes in handy for that. I’m not that save in AppleScript, so with some inspiration from here and here I hacked together a little script, which gets the trick done.

using terms from application "Mail"

    on perform mail action with messages theMessages
        tell application "Mail"
            set thePrefix to "TODO: "
            repeat with eachMessage in theMessages
                if the subject of the eachMessage begins with ¬
                    thePrefix then
                    set theSubject to trim_line(the subject ¬
                        of the eachMessage, thePrefix) of me
                else
                    set theSubject to the subject of eachMessage
                end if
                tell application "iCal"
                    set theCalendar to (first calendar ¬
                        whose title is "calendar")
                    make new todo at the end of todos of theCalendar ¬
                        with properties {summary:theSubject}
                end tell
            end repeat
        end tell
    end perform mail action with messages
end using terms from

-- taken from apple.com and modified
on trim_line(this_text, trim_chars)
    set x to the length of the trim_chars
    repeat while this_text begins with the trim_chars
        try
            set this_text to characters ¬
                (x + 1) thru -1 of this_text as string
        on error
            -- the text contains 
            -- nothing but the trim characters
            return ""
        end try
    end repeat
    return this_text
end trim_line

What does it do? It takes the messages provided by the rule and looks if the subject starts with “TODO:”. If so, it trims off “TODO:” and creates a new task with the resulting string as the tasks summary. In case it’s not starting with “TODO:” it just uses the mail subject as the summary for the new task. And thats it. If you want to use this, just copy and past it to Script Editor, compile, save somewhere and use it with the “Run AppleScript” action in a rule of your choice.

Posted Wednesday, January 16th, 2008 at 11:01 pm
Filed Under Category: Apple
You can leave a response, or trackback from your own site.

1

Response to “Create tasks from incoming mail”

PublicFarley

Thanks for the script. It worked perfectly. It has now been incorporated in to my workflow. Much appreciated!

Leave a Reply