I’d like to automatically get drafts of emails to send to people wishing them a happy birthday.
I’m thinking that a script would run run each night and work like this:
Every morning I’d receive an email containing a mailto: URL for each Contact that: (a) has a birthdate on the next day; (b) does not have a value in a date field labeled “death”; and (c) has at least one email address labeled either home or work.
Tapping on a mailto: URL would create a draft email in my default mailer.
URLs would consist of: to, a string formatted as name<emailAddress> where (a) name is the Contact’s nickame (if present, otherwise its firstName) and (b) emailAddress is a home email address if the Contact contains one, otherwise, a work one; subject, a static string, e.g., “Happy Birthday ,” not customized based on the Contact; and body, a string starting with a salutation containing the person’s firstName or nickname, depending on availability as a described for to, but otherwise static and encoded in a way that facilitates multiple lines, blank lines, and emojis.
My Contacts are synced among Google, iOS, and macOS so this daily email generation script could run on any of these platforms. My Contacts are pretty clean and well structured. I have over 6K, but the vast majority lack a birthdate.
I can see 2 responders already asking for more detail. That’s how apple.stackexchange tends to be. You get deeper answers to tech-heavy issues but they will want greater specificity. Hope you get a useful solution.
I just added my two cents there. I’ll repeat it here, with a bit more detail (since StackExchange limits comment sizes and an answer is not appropriate if it isn’t actually proposing a solution.)
I think there are four tasks here you need to perform:
Run a query against your contacts list, looking for every contact that:
Has a birthday on-file
Has a valid e-mail address
Has a birthday that fits within a configured window (e.g. between the last time you ran the script and midnight tomorrow)
Has not already received a birthday e-mail this year
I don’t know how to do this, but I assume the Contacts app has AppleScript commands that you can use. Looking through its dictionary, I see that the address book application contains “people”, each of which seems to contain what you need. There is no “death date” field, but each person may contain “custom dates”, which I assume is what you are using for this field.
I am not an AppleScript developer, but I assume you can get an instance of the Contacts app (launching it, if necessary), and then iterate across the set of people, looking for those that meet your criteria.
Generate an e-mail message.
Again, this should be scriptable. Looking at the dictionary for Mail (assuming it’s what you will be using), there is an “outgoing message” object that you should be able to create. It has a “content” property containing the message body and can respond to “save” and “send” messages, depending on what you want to do with it.
Generate mailto: URLs with the required content. Fortunately, this is pretty easy. Here are a few links I ran across that you may find useful:
Note that if the subject and body of the message are non-trivial, you may need to perform some URL-encoding of things like newlines and other special characters. It is probably worth checking to see if there is a standard AppleScript function you can use to simplify this, since it may be tricky to write a function that will cover all possibilities.
If you’re only going to be sending static text, you may find it easier to just use a web site to generate the URL-encoded text, then copy/paste the results into your script. For example:
Many years ago, I wrote an Applescript somewhat along the lines of what you are looking for.
My script was triggered by Calender events. I added an event for each of my siblings birthdays. Since the number of people was restricted, I didn’t worry about things like more than one person with the same birthday.
(I don’t know if everyone is aware that a Calendar Alert can open a file; if the file is an Applescript compiled as an application, then “opening” the file runs the script. An Open File event is created in Calendar using the Custom… option while setting an event Alert.)
When my script opens, it doesn’t know whose birthday it is, so it asks Contacts for a list. It searches for a birthday that matches today. Then, if the recipient has a home email address, it composes and sends a message.
My message read,
Happy birthday, [first name]!
Hi, I'm Jeff's computer. Jeff probably didn't remember your birthday, but I did.
I should note that in recent years, as Apple has tightened sandboxing rules, I have had trouble keeping my script working. I figured the joke had pretty much run its course anyway, so I have pretty much stopped trying. It works fine run stand-alone, as long as the required permissions are granted. But those permissions don’t seem to carry over when the script is triggered by Calendar. Of course, you could run it manually. If it doesn’t find a birthday today, it just quits silently.
I can post or send my script to you. I don’t know how to include an attachment here, or if I can. I suppose I could paste the text of my script into a post, but it is almost 100 lines long. Does anyone have a better suggestion for posting it?
It’s an interesting little challenge. There’s a couple approaches that I thought of right away. But like mentioned, need a bit more info.
First, “death” isn’t a standard field in the macOS address book, but you can add other date fields and label them, so assuming that’s what you’ve done.
Also, rather than receiving an email with mailto: links, would you be willing for draft emails to be started for the people that met the criteria?
The most obvious to me AppleScript, but it’s probably the higher curve if you’re never used it before.
So I opened Automator, and sure enough you can select all contacts that have a birthday tomorrow with a preexisting filter. I could not see a way to filter that list based on the “Death” field, or any custom date field. There was then a Mail action that sent a message and image to the recipient, it appeared in my draft folder of Apple Mail. The limitations of Automator are clear here; while you can customize the message, there was no obvious way to choose between work/home emails. I don’t know what it would default to if both were present.
The last option I looked at was the macOS Calendar app feature of an “alert” that ran a script. For any individual event in the Calendar, you can set up an alert that runs a script or sends a mail message (which would meet your orig criteria), but the only open the Calendar app seems to have for the default alerts for the automatic Birthday calendar is how much time before the birthday to be notified; the other alert types (run app or send email) were not listed. (this open was found in the Calendar app preferences).
Applescript could exactly what you wanted. I was tempted to write one quickly for some fun, but if you are going to use it and need to possibly tweak it in the future, would be better to do it yourself if you go that route.
A final though as I’m typing this: The calendar app Fantastical might have something like this built in. I’m not a user, but it gets rave reviews and has lots of functionality.
Since this is such an uncommon occurrence, I’d suggest that you two trade email addresses in a direct message (click your avatar in the upper right, click the envelope icon twice, and create a new message) and then share it out-of-band.
Yes, correct. It is a Custom Label that I use on a Date field.
Yes, creating a draft email would be fine.
But, I don’t use a Mac application for email; I use (the web interface to) Gmail. (On iOS I use Spark.) So, ideally, I’d prefer the draft to be created in my Gmail Drafts folder. I’m guessing that doing so directly would require the script to run on Google’s server and that looks hard to do.
Since I’ve set Gmail as my default mailer on macOS, I’m wondering whether I can get an unattended AppleScript on macOS (triggered by a custom launch daemon, as suggested by @Shamino) to open a draft email in Gmail using a mailto: link. Any thoughts?
The first three (Stack Overflow, Ask Different and Super User) are all part of the Stack Exchange network. These are really meant for specific questions to solve specific problems (for software development, Macs and system administration, respectively). They are not really appropriate for tutorial type questions about how to get stating with programming in general.
The MacScripter site looks like it might be promising. They have a tutorial section of the forums.
Just getting caught up on this, so sorry if you’re already solved this.
Using gmail through a web browser might not work, but it would be pretty trivial to set up Apple Mail to link to the gmail account, or some other local client that supports AppleScript…you only need the most basic of functionality to create some draft messages. If it’s not your main mail client, you might even be able to have it not download mail automatically, so when the AppleScript opens the mail client, it just creates the draft messages, saves them to the server, and quits. That’s all supposition though.
Creating the draft messages would be very straight forward AppleScript though.
I’m making great progress on a solution that uses AppleScript to create a separate mailto: URL string for each draft email birthday (and anniversary) greeting. The advantage of this approach is that it uses your mailer of choice and, in my case, opens Gmail drafts in my browser.
Personally, I like LaunchControl better. It’s free and does a lot of error detection and some corrections for you (even finds errors in Apple launch items). Lots of help to explain various functional areas.