R – Search Mailbox of Mail.app with Applescript

applescriptemailmacossearch

I want to be able to search a mailbox in apple's mail.app for a phase or word, then somehow return or copy all of the email addresses from which the emails which have successfully returned from the result of the search have been sent from.. if you get what i mean

I thought that the only way to do this is probably applescript but if anyone else knows any other way please tell me 🙂

Best Answer

Mail.app doesn't allow searches directly via Applescript but this will do the trick, though it is a bit slow because it has to iterate through each message:

global searchTerm
property emailList : {}

set searchTerm to "aSearchTerm"

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
Related Topic