DotSlashSlash
DotSlashSlash

Reputation: 5203

Search Mailbox of Mail.app with Applescript

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 :)

Upvotes: 2

Views: 4439

Answers (2)

Anno2001
Anno2001

Reputation: 1363

you could also invoke a real search in the interface and then collect the items

    [Applications launch:@"Mail"];
    [Keyboard command_alt_press:'f'];
    [Keyboard paste:term];


+(void) command_alt_press:(char)c{
    [self runScript:[NSString stringWithFormat:@"tell application \"System Events\" to keystroke \"%c\" using command option down",c]];
}

you seem competent enough to complete the rest of the code.

Upvotes: 0

Philip Regan
Philip Regan

Reputation: 5055

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

Upvotes: 3

Related Questions