Jim Frisby
I write tests that pass.

  • https://jimfrisby.com
  • @frisby
  • say hi
  • subscribe

July 9, 2010


Mail • Send Flagged Mail to Things

Because I’m a fan of Inbox Zero, and I hate checking two places for my list of things to do, I cobbled together some AppleScript that will turn the flagged mail in my inbox into To Do items in Things. Here it is, if you’re interested.

I can’t take much credit. It’s really just a hack based on John Gruber’s script for flagging iPhone mail. It cycles through the IMAP accounts in Mail, and for each flagged message it finds in the Inbox, it creates a new To Do in Things’ Inbox with the name set to the subject line of the message. Additionally, it adds the message as an attachment, then archives it.

tell application "Mail"
    repeat with _acct in imap accounts
        if (_acct is enabled) then
            set _acct_name to name of _acct
            set _inbox to _acct's mailbox "INBOX"

            try
                set _archive_box to _acct's mailbox "Archive"
            on error
                -- Archive doesn't exist; maybe this is Gmail?
                try
                    set _archive_box to _acct's mailbox "[Gmail]/All Mail"
                on error
                    display alert "No “Archive” mailbox found for account “" ¬
                        & _acct_name & "”."
                    return -- Stop the script
                end try
            end try

            set _msg_list to (every message of _inbox whose flagged status is ¬
                true and read status is true)

            if (_msg_list's length > 0) then
                repeat with _msg in _msg_list
                    set _msg_subj to subject of _msg
                    set _msg_id to message id of _msg
                    set _msg_url to "[url=message:%3C" & _msg_id & "%3E]" ¬
                        & _msg_subj & "[/url]"

                    try
                        tell application "Things"
                            make new to do with properties ¬
                                {name:_msg_subj, notes:_msg_url}
                        end tell

                        move _msg to _archive_box
                    on error
                        display alert "Could not create To Do for message “" ¬
                            & _msg_subj & "”."
                        return -- Stop the script
                    end try
                end repeat
            end if
        end if
    end repeat
end tell

Of course, the best way to use this is to create a Snow Leopard service. Speaking of which: I guess the URL format for messages changed in Snow Leopard, so this script won’t work as-is in Leopard.


  • ← Home