Birthday reminders - AppleScript

(closed account) says:
I know I could just add repeating tasks for birthday reminders, but I wanted a bit more control over it, so it could be automated from my address book. i.e. if I added a contact to my mac address book then their birthday would automatically get sent to RTM as a reminder.
So, I wrote a little AppleScript to do this.
It will automatically send a task email to your RTM inbox a week in advance of someone's birthday, with details of the day & date of their birthday, the age they will be, and their postal address as a note in the task!
Just paste the following code into your AppleScript editor, and run it to test. Make sure you replace the relevant fields below, i.e. the From (your email address) and To (your RTM inbox email address) addresses in the "theemail" section and the "sendmail -bm " should also be to your RTM inbox email address.
You can also tweak the subject and body of the email message to be as you like, i.e. task name will be taken from the subject, and you can change the tags to whatever is relevant for you.
I don't claim to be an expert at AppleScripting, in fact this is my first attempt, so there may be a better way of doing this.
Once you've set it up and successfully tested it, you can create an Automator action to run the script every day, then you will get your reminders a week in advance of someone's birthday, and you need never forget that important day again.
Here's the code:
copy from below the line:
--------------------------------------------
set m to month of (current date)
set d to day of (current date)
set w to weekday of (current date)
set y to year of (current date)
tell application "Address Book" to tell (people whose birth date is not missing value)
repeat with thisPerson in it
set thebirthday to thisPerson's birth date as date
set theyear to year of thebirthday
set year of thebirthday to (current date)'s year
set shortbirthday to date string of thebirthday
set ab_m to month of thebirthday
set ab_d to day of thebirthday
set ab_w to weekday of thebirthday
if ab_m = m and ab_d ≥ d and ab_d < d + 15 then
set theage to (y - theyear)
set thestreet to street of (addresses of thisPerson)
set thetown to city of (addresses of thisPerson)
set thecounty to state of (addresses of thisPerson)
set thezip to zip of (addresses of thisPerson)
set thecountry to country of (addresses of thisPerson)
set thename to thisPerson's name
set thefirstname to thisPerson's first name
set birthdayfile to ".birthdayemail.txt"
set theemail to "Subject: It's " & thename & "'s Birthday this " & ab_w & " (" & theage & " years old on " & shortbirthday & "), send a card
From: Me
To: Remember The Milk
P:1
D:today
L:Home
S:.birthdays,@errand
---
Address:
---
" & thestreet & "
" & thetown & "
" & thecounty & "
" & thezip & "
" & thecountry & "
---
"
set filelocation to my createemail(birthdayfile, theemail)
do shell script "sendmail -bm my+rtm+inbox@rmilk.com < " & filelocation
do shell script "rm " & filelocation
end if
end repeat
end tell
on createemail(the_filename, the_list)
set docFolder to (path to desktop as string)
set docName to docFolder & the_filename as list
set docunix to POSIX path of docFolder & the_filename
set docPointer to (open for access docName with write permission)
set eof of docPointer to 0
write the_list to docPointer as string
close access docPointer
return docunix
end createemail
So, I wrote a little AppleScript to do this.
It will automatically send a task email to your RTM inbox a week in advance of someone's birthday, with details of the day & date of their birthday, the age they will be, and their postal address as a note in the task!
Just paste the following code into your AppleScript editor, and run it to test. Make sure you replace the relevant fields below, i.e. the From (your email address) and To (your RTM inbox email address) addresses in the "theemail" section and the "sendmail -bm " should also be to your RTM inbox email address.
You can also tweak the subject and body of the email message to be as you like, i.e. task name will be taken from the subject, and you can change the tags to whatever is relevant for you.
I don't claim to be an expert at AppleScripting, in fact this is my first attempt, so there may be a better way of doing this.
Once you've set it up and successfully tested it, you can create an Automator action to run the script every day, then you will get your reminders a week in advance of someone's birthday, and you need never forget that important day again.
Here's the code:
copy from below the line:
--------------------------------------------
set m to month of (current date)
set d to day of (current date)
set w to weekday of (current date)
set y to year of (current date)
tell application "Address Book" to tell (people whose birth date is not missing value)
repeat with thisPerson in it
set thebirthday to thisPerson's birth date as date
set theyear to year of thebirthday
set year of thebirthday to (current date)'s year
set shortbirthday to date string of thebirthday
set ab_m to month of thebirthday
set ab_d to day of thebirthday
set ab_w to weekday of thebirthday
if ab_m = m and ab_d ≥ d and ab_d < d + 15 then
set theage to (y - theyear)
set thestreet to street of (addresses of thisPerson)
set thetown to city of (addresses of thisPerson)
set thecounty to state of (addresses of thisPerson)
set thezip to zip of (addresses of thisPerson)
set thecountry to country of (addresses of thisPerson)
set thename to thisPerson's name
set thefirstname to thisPerson's first name
set birthdayfile to ".birthdayemail.txt"
set theemail to "Subject: It's " & thename & "'s Birthday this " & ab_w & " (" & theage & " years old on " & shortbirthday & "), send a card
From: Me
To: Remember The Milk
P:1
D:today
L:Home
S:.birthdays,@errand
---
Address:
---
" & thestreet & "
" & thetown & "
" & thecounty & "
" & thezip & "
" & thecountry & "
---
"
set filelocation to my createemail(birthdayfile, theemail)
do shell script "sendmail -bm my+rtm+inbox@rmilk.com < " & filelocation
do shell script "rm " & filelocation
end if
end repeat
end tell
on createemail(the_filename, the_list)
set docFolder to (path to desktop as string)
set docName to docFolder & the_filename as list
set docunix to POSIX path of docFolder & the_filename
set docPointer to (open for access docName with write permission)
set eof of docPointer to 0
write the_list to docPointer as string
close access docPointer
return docunix
end createemail

(closed account) says:
Actually, instead of setting up something in automator, just save is as type application in the Script Editor, then copy the .app file you've created to ~/Library/StartupItems

(closed account) says:
...or create a cron job for it to run daily/weekly etc. using osascript

(closed account) says:
Slight mistake in the original code; remove the line between "Address:" and " & thestreet & " otherwise it will create two separate notes instead of the one.
So here's the fixed code:
---------------------------------
set m to month of (current date)
set d to day of (current date)
set w to weekday of (current date)
set y to year of (current date)
tell application "Address Book" to tell (people whose birth date is not missing value)
repeat with thisPerson in it
set thebirthday to thisPerson's birth date as date
set theyear to year of thebirthday
set year of thebirthday to (current date)'s year
set shortbirthday to date string of thebirthday
set ab_m to month of thebirthday
set ab_d to day of thebirthday
set ab_w to weekday of thebirthday
if ab_m = m and ab_d ≥ d and ab_d < d + 15 then
set theage to (y - theyear)
set thestreet to street of (addresses of thisPerson)
set thetown to city of (addresses of thisPerson)
set thecounty to state of (addresses of thisPerson)
set thezip to zip of (addresses of thisPerson)
set thecountry to country of (addresses of thisPerson)
set thename to thisPerson's name
set thefirstname to thisPerson's first name
set birthdayfile to ".birthdayemail.txt"
set theemail to "Subject: It's " & thename & "'s Birthday this " & ab_w & " (" & theage & " years old on " & shortbirthday & "), send a card
From: Me
To: Remember The Milk
P:1
D:today
L:Home
S:.birthdays,@errand
---
Address:
" & thestreet & "
" & thetown & "
" & thecounty & "
" & thezip & "
" & thecountry & "
---
"
set filelocation to my createemail(birthdayfile, theemail)
do shell script "sendmail -bm my+rtm+inbox@rmilk.com < " & filelocation
do shell script "rm " & filelocation
end if
end repeat
end tell
on createemail(the_filename, the_list)
set docFolder to (path to desktop as string)
set docName to docFolder & the_filename as list
set docunix to POSIX path of docFolder & the_filename
set docPointer to (open for access docName with write permission)
set eof of docPointer to 0
write the_list to docPointer as string
close access docPointer
return docunix
end createemail
So here's the fixed code:
---------------------------------
set m to month of (current date)
set d to day of (current date)
set w to weekday of (current date)
set y to year of (current date)
tell application "Address Book" to tell (people whose birth date is not missing value)
repeat with thisPerson in it
set thebirthday to thisPerson's birth date as date
set theyear to year of thebirthday
set year of thebirthday to (current date)'s year
set shortbirthday to date string of thebirthday
set ab_m to month of thebirthday
set ab_d to day of thebirthday
set ab_w to weekday of thebirthday
if ab_m = m and ab_d ≥ d and ab_d < d + 15 then
set theage to (y - theyear)
set thestreet to street of (addresses of thisPerson)
set thetown to city of (addresses of thisPerson)
set thecounty to state of (addresses of thisPerson)
set thezip to zip of (addresses of thisPerson)
set thecountry to country of (addresses of thisPerson)
set thename to thisPerson's name
set thefirstname to thisPerson's first name
set birthdayfile to ".birthdayemail.txt"
set theemail to "Subject: It's " & thename & "'s Birthday this " & ab_w & " (" & theage & " years old on " & shortbirthday & "), send a card
From: Me
To: Remember The Milk
P:1
D:today
L:Home
S:.birthdays,@errand
---
Address:
" & thestreet & "
" & thetown & "
" & thecounty & "
" & thezip & "
" & thecountry & "
---
"
set filelocation to my createemail(birthdayfile, theemail)
do shell script "sendmail -bm my+rtm+inbox@rmilk.com < " & filelocation
do shell script "rm " & filelocation
end if
end repeat
end tell
on createemail(the_filename, the_list)
set docFolder to (path to desktop as string)
set docName to docFolder & the_filename as list
set docunix to POSIX path of docFolder & the_filename
set docPointer to (open for access docName with write permission)
set eof of docPointer to 0
write the_list to docPointer as string
close access docPointer
return docunix
end createemail

(closed account) says:
Sorry, I left the timeframe as within the next 15 days. This needs to be 7 to make it birthdays within the next week.
So change line #14 from:
if ab_m = m and ab_d ≥ d and ab_d < d + 15 then
To this:
if ab_m = m and ab_d ≥ d and ab_d < d + 7 then
So change line #14 from:
if ab_m = m and ab_d ≥ d and ab_d < d + 15 then
To this:
if ab_m = m and ab_d ≥ d and ab_d < d + 7 then

(closed account) says:
OK, so the first problem I've come across is that a new task is going to get created every time I boot my laptop. Oops. :)
So the way around this is to schedule the script to run weekly (or however often you have configured the script timeframe as mentioned previously).
I do this by adding an event in iCal, and setting the alarm type to "Run Script". Then the script will run only once a week, and if my laptop is switched off at that time, iCal will still run it when I next boot up. So I've set the iCal alarm for every monday at 10am, when I'm most likely to have it switched on.
Anywhoo, this may all be a mute point as I'm not sure if people will really find this useful. :) hohum
So the way around this is to schedule the script to run weekly (or however often you have configured the script timeframe as mentioned previously).
I do this by adding an event in iCal, and setting the alarm type to "Run Script". Then the script will run only once a week, and if my laptop is switched off at that time, iCal will still run it when I next boot up. So I've set the iCal alarm for every monday at 10am, when I'm most likely to have it switched on.
Anywhoo, this may all be a mute point as I'm not sure if people will really find this useful. :) hohum
Log in
to post a reply.