Archive mail when it is older than a certain time frame


property secondsIn120Days : 10368000


tell application "Mail"

set theInbox to inbox

set dateToday to current date

set firstMessage to 1
set lastMessage to (get count of messages in theInbox)

repeat with thisMessage from lastMessage to firstMessage by -1
set currentMessage to message thisMessage of theInbox
set messageDate to date received of currentMessage

set timeDifference to dateToday - messageDate

if timeDifference ≥ secondsIn120Days then
beep
end if
end repeat

end tell



Search messages in an inbox for a word


Searching isn't specifically allowed via Applescript in Mail.app, but this will do the trick. It is a bit slow, though, and I'm not sure there really is a way to speed it up. Sometimes you just have to brute force your way with Applescript despite all of its conveniences.


global searchTerm
property emailList : {}

set searchTerm to "calendar"

tell application "Mail"

set theInbox to inbox

set firstMessage to 1
set lastMessage to (get count of messages in theInbox)

repeat with thisMessage from firstMessage to lastMessage
set currentMessage to message thisMessage of theInbox

set messageContent to content of currentMessage

if messageContent contains searchTerm then
set end of emailList to sender of currentMessage
end if

end repeat

end tell

return emailList