Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I don’t know AppleScript at all so thanks in advance for any help offered on this question. I’m on my Macbook Pro laptop with the latest version of OSX installed. I have a csv file that looks like this: FirstName,Email Ken,blah@blah.com Mike,blahblah@blahblah.com

This is my customer list and I want to send them an email. Unfortunately I don’t have this list in an autoresponder so I have to send the emails one by one.

I want to write an AppleScript that processes my .csv file one record at a time and sends a message. The message would be something like this:

Subject: How’s it going?

Hi Ken

It’s been a while since I sold you that defective widget from China. If you need more defective elctronics I’m here for you. Just give me a call at xxx-xxx-xxxx.

Sincerely

Ken

The AppleScript would read the name and email address from the file one record at a time and send this email, filling in name and email address, using the standard apple mail program.

After sending the message I want the script to wait 60 seconds. Then send another email.

This needs to happen until the end of file is reached.

My first question… Is this possible? If possible how do I do it?

Also is there a better way to do what I’m trying to do?

Thanks

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

Here an AppleScript that does what you want :

property secsBetweenMails : 60 -- seconds
property csvHasHeaders : true
property mailSubject : "How’s it going?"
property mailBody : "Hi %NAME%

It’s been a while since I sold you that defective widget from China.
If you need more defective elctronics I’m here for you.
Just give me a call at xxx-xxx-xxxx.

Sincerely

Ken"

set csvData to "FirstName,Email
Ken,blah@blah.com
Mike,blahblah@blahblah.com"

--> or:
-- set csvData to read file "path:to:file.csv"

set countSent to 0

-- Parse .csv files with Applescript
-- Adapted from : http://macscripter.net/viewtopic.php?id=19676
set csvEntries to paragraphs of csvData
if csvHasHeaders then
    set startAt to 2
else
    set startAt to 1
end if
repeat with i from startAt to count csvEntries
    set {theName, theEmail} to parseCsvEntry(csvEntries's item i)
    set theBody to replaceName(mailBody, theName)
    tell application "Mail"
        set theNewMessage to make new outgoing message with properties {subject:mailSubject, content:theBody, visible:true}
        tell theNewMessage
            make new to recipient at end of to recipients with properties {address:theEmail}
            send
            set countSent to countSent + 1
        end tell
    end tell
    delay secBetweenMails
end repeat
display dialog (countSent & " mails sent.")

to parseCsvEntry(csvEntry)
    set AppleScript's text item delimiters to ","
    set {theName, theEmail} to csvEntry's text items
    set AppleScript's text item delimiters to {""}
    return {theName, theEmail}
end parseCsvEntry

to replaceName(aBody, aName)
    set AppleScript's text item delimiters to "%NAME%"
    set parts to aBody's text items
    set newBody to item 1 of parts & aName & item 2 of parts
    set AppleScript's text item delimiters to ""
    return newBody
end replaceName
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.