Last sent/received email dates (Applescript)

This is something I'd been looking for for some time. These scripts allow you to update your address book every time you receive or send a mail, showing the last dates on which someone wrote to you, or you wrote to them.

Received dates

This script I found here, and it works automatically, updating your addressbook every time you get a new mail. The script is copied below, or get it from the original site. Save the script as whatever you like in the folder ~/Library/Scripts/Mail Scripts. Then, create a rule in Mail set to run whenever the sender is in your addressbook, and select the script you just saved.

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
                end if
            end tell
        end repeat
    end perform mail action with messages
end using terms from

Sent dates

This is my own modification of the above script. Unfortunately, I can't get it to run automatically as it is not possible to set a rule to act on sending a mail in Mail. However, if you select all the mails in your sent mail folder, then run the script from the script menu, it will update your addressbook with the date that you last mailed the person in question. If you know how to make this automatic, let me know.

As above, save this script as whatever you like in the same folder ~/Library/Scripts/Mail Scripts, and then run it from the script menu in the toolbar.

tell application "Mail"
    set theseMails to selection
    repeat with thisMail in theseMails
        set addressRecords to address of every recipient of thisMail
        set thisSent to thisMail's date sent as date
        repeat with thisRecipient in addressRecords
            tell application "Address Book"
                set theseContacts to (get every person where the value of its email contains thisRecipient)
                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 wrote")
                    if (count of theseDates) is 0 then
                        make new custom date at the end of thisContact's custom dates with properties {label:"last wrote", 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
                end if
            end tell
        end repeat
    end repeat
end tell

If you have any questions about these scripts, drop me a line.

|