Birthday Emails

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:

  1. 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.
  2. Tapping on a mailto: URL would create a draft email in my default mailer.
  3. 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 :balloon:,” 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.

Does anyone have an idea of how to approach this?

Thank you for taking the time to read this post.

1 Like

Try posting this to the Apple section at StackExchange. They dive into more technical, power user issues.

Thank you for your suggestion. Done:

https://apple.stackexchange.com/questions/440252/automatically-prepare-drafts-of-happy-birthday-emails

UPDATE: June 5, 2022 4:37 PM
I deleted my post because apparently my inquiries were too open ended and I wasn’t observing the forum’s Question/Answer protocol.

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.

1 Like

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:

  1. Run a query against your contacts list, looking for every contact that:

    • Has a birthday on-file
    • Is alive
    • 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.

  2. 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.

  3. 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:

  4. Schedule your script to run every day.

    There are many different ways this can be done. The most Mac-like way is to create a custom launch daemon file that will run your script at the schedule you specify. See also:

And now you’ve got the hard/fun part - actually writing the code to do all this. Enjoy.

1 Like

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.

1 Like

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.

Thanks. That is what we have done.

1 Like

That’s exactly what we did, but @jajvj1 beat me to posting it. :wink:

1 Like

@jajvj1 @angusC @Shamino
You’ve convinced me to sally forth to give AppleScript a try. Other than installing Script Debugger, do you have any other suggestions for getting started?

I found these forums for AppleScript:

Do you recommend any one of these over another? Or, something else that I haven’t found?

Thank you.

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?

I looked at Flexibits’ help page and the macOS version of Fantastical appears to support AppleScript, but I didn’t see any indication that AppleScript can run a date query that returns a list of matches. Its iOS FAQ says that Fantastical supports an x-callback-url but the macOS FAQ makes no mention of such support. (Moreover, the list of applications supporting x-callback-url points to the iOS app alone, not the macOS version.)

So, the bottom line is that Fantastical running on macOS doesn’t look promising, as far as I can see. I’m not aware of anyway to execute an AppleScript on iOS (though apparently an iOS Shortcut can can invoke an AppleScript on macOS and return data as a JSON string). So, I don’t know how I could make use of support for x-callback-url on the iOS verson of Fantastical.

Of course, I suppose that I could use an iOS automation Shortcut to trigger Scriptable to (1) query iOS Fantastical using an x-callback-url, and (2) uses uses each resulting match to create a iOS Spark draft via a mailto: URL scheme. (Sounds rather like a Rube Goldberg Machine to me; surely there is a more straightforward way to accomplish what I want.)

As an aside, I can NOT find a x-callback-url for Apple’s iOS Calendar app.

Please let me know if you see something that I overlooked.

Thank you.

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.

A web search found this Apple forum post: Starting out with AppleScript - Apple Community

It contains references and links to various books and resources you may find useful.

1 Like

@jajvj1 @angusC @Shamino
It turns out that this script is simplified tremendously by using the appropriate SmartGroup.

1 Like

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.

That’s great, wasn’t sure that would work, but thought there was a chance.

Loved the smart groups in contacts too.

@Shamino @jajvj1 @angusC @macguyver
I posted a working AppleScript.

Any idea why open location doesn’t encode the emoji correctly?

If I need to use a shell script, would someone please provide a snippet or link to some examples?

Thank you.

UPDATE: June 5, 2022 4:35 PM
I changed the link to my script.

Apparently, there are at least two applications for doing this:

Do you recommend either of them or should I just create the launchd plist file with a text editor manually?

UPDATE: June 5, 2022 4:24 PM
Corrected link to LaunchControl

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.

1 Like