MacOS – Reply emails automatically with Mail & AppleScript & Automator

applescriptautomatormacosmail.app

There are dozens of conference call numbers stored at my computer. These numbers are all numbers, such as 40027683, no other characters. These numbers are saved in a txt file.
enter image description here

Some of my users may need a number when they are trying to make a conference call with their clients. So every time the users require for a conference call number via emails, I'll reply back with a number.

Is there a way to reply those mails which requires for concall numbers automatically? I would like to try Mail, AppleScript and Automator.

I would like to ask Mail to run AppleScript when a new email (The subject or message contents contain the keyword "concall") was received, here is an example:

enter image description here

The AppleScript which I wrote will be run to invoke the first number from the txt file. The next time when another message arrived, Mail will run AppleScript to invoke the second number of the txt file. Is there any way to make it happen?

Any suggestion would be much appreciated.

Best Answer

You can do it like this:

property the_numbers : {10000, 20000, 30000}
using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
        repeat with i from 1 to count of theMessages
            tell application "Mail"
                set the_sender to sender of item i of theMessages
                set the_subject to subject of item i of theMessages
                set the_counter to item 1 of the_numbers
                -- make the outgoing message (the reply)
                set the_reply to make new outgoing message with properties ¬
                    {subject:"Re: " & the_subject, content:"Use this number-- " & the_counter}
                tell the_reply
                    make new to recipient at end of to recipients with properties {address:the_sender}
                end tell
                send the_reply
            end tell
            -- this is the step that deletes item 1 from the list 
            set the_numbers to items 2 thru -1 of the_numbers
        end repeat
    end perform mail action with messages
end using terms from

This works, but you'll have to do a little work to get your list of numbers into an AppleScript list. You'll put them up at the top, separated by commas, replacing my 10000, 20000, 30000.

I call the list of numbers "the_numbers". By calling it a property, we save the value of the_numbers from run to run. If you were to use my script as-is in place of your "Untitled 3" in the rule you've set up, the first time the script is called it will be working with {10000, 20000, 30000}. The next time it runs, the list will be {20000, 30000}. The next time it runs, it will be {30000}. It will also be {30000} for all subsequent runs. I am assuming/hoping that you have a very long list of numbers and you aren't going to run out.

A better script would let you know that the list of numbers was down to the last one.