0

I read with interest the article regarding sending an email based upon parsing through a csv file to pick up the name and email address. This looks like most of what I need to accomplish my objective with the exception that I would like for the script to run once daily and send the email one day before a date stored in the csv file. This is to remind someone that they need to perform a a certain task the next day. Emails will be sent using Apple Mail as the MTA through my gmail account.

The csv will contain the following:

firstname, lastname, action date

The script would need to pick up the first name and date from the csv file. I have coded the email text into the script as shown in the previous example.

Pseudocode:

  1. Script runs just after midnight every day parsing the csv to determine who should receive the email on what date.
  2. For the selected names, create and send an email the day prior to the required action.
  3. As a bonus, the script could resend the reminder on the due date as well as any new reminders for the following day.

I suppose I could manually subtract one day in my csv to make the coding easier.

I am just getting started with Applescript, and do not know how to modify the existing script to accomplish this objective.

1
  • You understand that this approach is quite fragile? If you Mac is off (maybe asleep, no wifi, no internet) it doesn't do a thing. In general (depending on the case) you want something more fail-safe, like running on a dedicated on a server.
    – Roger
    Jul 13, 2013 at 9:06

1 Answer 1

1

Try something like this:

set csv to "name@example.com,7/14
name2@example.com,7/15"
--set csv to read "/Users/username/Documents/file.csv" as «class utf8»

set text item delimiters to ","
set y to year of (current date)
repeat with l in paragraphs of csv
    set d to (date (text item 2 of l & "/" & y))
    if date string of (current date) is date string of (d - 1 * days) then
        tell application "Mail"
            tell (make new outgoing message)
                set subject to "subject"
                set content to "content"
                make new to recipient at end of to recipients with properties {address:text item 1 of l}
                send
            end tell
        end tell
    end if
end repeat

You can run the script every day after midnight (if the computer is awake) by running EDITOR=nano crontab -e and adding a line like 1 0 * * * osascript ~/Scripts/some\ script.scpt.

The recognized date formats depend on the settings selected in System Preferences. I don't know how to get an email address for a first name and last name.

1
  • Thanks for the speedy answer. I am aware of the potential sleep problem. That said, my Mac Pro never sleeps, only my display so that shouldn't be a problem. I am familiar with cron from Linux/Unix. I believe this code, when integrated with what I already have, should do the trick as the other code parses the csv for the name and a personalized greeting. At most, I need only send a maximum of 6 emails each day so resource consumption should be light.
    – Rod Cole
    Jul 14, 2013 at 19:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .