chuckbarry
chuckbarry

Reputation: 1

How can I use apple script to step through my apple mailbox and see if addresses exist there

I'm very new to apple scripts, and honestly am not too much of a programmer. I want to do something very similar to this: How can I determine whether or not a contact with a given e-mail address exists in the Address Book? Except I want to look through all my emails, and search for specific addresses (i have a list of thousands in csv/excel/numbers format), to see whether any email communication exists. I figured out how to run through my list and search, but not sure how to return whether mail exists. I often double email people with marketing material and would rather not do that. Here's my basic search script that pulls from numbers, just need to figure out how to ask the mail application if a mail item exists after running each email search through it.

set {eAddress} to getData()

repeat with i from 1 to 4
    activate application "Mail"

    tell application "System Events"

        tell process "Mail"

            tell window 1

                keystroke "f" using {command down, option down}

                keystroke {item i of eAddress} as string

            end tell

        end tell
    end tell
end repeat

on getData()
    set colA to {}
    tell application "Numbers"

        activate
        tell table 1 of sheet 1 of document 1
            #set lastRow to 4
            set lastRow to row count
            #first row index of (get end (last cell of column 1) direction toward the top)

            repeat with i from 2 to lastRow
                set end of colA to (value of cell i of column "A")
            end repeat
        end tell
    end tell

    return {colA}
end getData

Upvotes: 0

Views: 279

Answers (1)

Ted Wrigley
Ted Wrigley

Reputation: 3184

I'm not sure if this will answer your question 100%, but if you just want to find whether or not you have messages from a particular address, you can do something like this:

set eAddresses to getData()

tell application "Mail"
    repeat with thisAddress in eAddresses
        if ((count of (messages of inbox whose sender contains thisAddress)) > 0) then
            -- there are messages from this address, so do something
        else
            -- there are no messages from this address, so do something else 
        end if
    end repeat
end tell

on getData()
    set colA to {}
    tell application "Numbers"

        activate
        tell table 1 of sheet 1 of document 1
            #set lastRow to 4
            set lastRow to row count
            #first row index of (get end (last cell of column 1) direction toward the top)

            repeat with i from 2 to lastRow
                set end of colA to (value of cell i of column "A")
            end repeat
        end tell
    end tell

    return colA
end getData

The whose command in the fourth line — (count of (messages of inbox whose...)) — asks Mail to do an internal search for messages from that sender, which is more efficient than trying to loop through the messages on your own.

Upvotes: 1

Related Questions