furia furialog · Every Noise at Once · New Particles · The War Against Silence · Aedliga (songs) · photography · other things
Note the Date (Apple Mail rule script)
Here is a very minor Applescript that performs a simple action I wanted done. Each time I get an email from a friend, this script updates a "last email" date in their Address Book entry. Download NoteTheDate.scpt (12k) to your ~/Library/Scripts/Mail Scripts folder, then add a Mail rule that matches on "Sender is in my Address Book" and runs this script. If you want to initialize some values from existing email, select some old messages in Mail and do Message > Apply Rules.
The script is simply this:

using terms from application "Mail"
    on perform mail action with messages theseMails
        repeat with thisMail in theseMails
            set thisSender to (extract address from thisMail's sender)
            set thisSent to thisMail's date sent as date
            tell application "Address Book"
                set theseContacts to (get every person where the value of its email contains thisSender)
                if (count of theseContacts) is 1 then
                    set thisContact to first item of theseContacts
                    set theseDates to (thisContact's custom dates whose label is "last email")
                    if (count of theseDates) is 0 then
                        make new custom date at the end of thisContact's custom dates with properties {label:"last email", value:thisSent}
                    else
                        if thisSent is greater than the value of the last item of theseDates then
                            set the value of the last item of theseDates to thisSent
                        end if
                    end if
                    save
                end if
            end tell
        end repeat
    end perform mail action with messages
end using terms from

"perform mail action with messages" is the event handler that Mail triggers, and "using terms from" is necessary because the script is receiving parameters rather than doing a "tell". The script launches Address Book if it isn't already running, finds all the people who have this message's From email, and if there is exactly one of them (which there ought to be) it either adds or updates the "last email" custom date. This function is ratcheted so the date is only updated if the new one is later than the existing one, which allows you to re-apply the rule to old emails in bulk without erroneously backdating anything. Since the script searches the Address Book anyway, having the rule filter on people who are in it is technically redundant, but it prevents a lot of pointless running of the script for other messages.
That's all there is to it, but feel free to email furiatech@furia.com if you have comments or questions, and I'll do what I can to help.
Site contents published by glenn mcdonald under a Creative Commons BY/NC/ND License except where otherwise noted.