0

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

1 Answers1

1

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.

Ted Wrigley
  • 2,921
  • 2
  • 7
  • 17
  • Thanks, this is super helpful. what's the best way to tell numbers to mark the adjacent cell to the email within this loop? Or would it be easier to add a mark to a 2 column array and add it into numbers after? ``` if ((count of (messages of inbox whose sender contains thisAddress)) > 0) then set colB to X``` – chuckbarry Feb 20 '20 at 19:52
  • @chuckbarry That is a separate question and should be posted as a new question. If Ted's answer has solved the original problem, you can mark this question as "solved" by selecting his answer with a green tick. – CJK Feb 21 '20 at 01:25