Build A Birthday Reminder Application in Python on Mac

Did you forget someone’s birthday recently? Why don’t you write a birthday reminder script that pushes notification on your macbook or mac desktop? I have done that. Let me help you in writing a python script.

I did it my way: wrote a python script as birthday.py then packaged that into app for mac and I put that app into “Login Items” in System Preferences.

Sounds Easy? Do it. Feel Free to do it your way.

1. Prepare your birthday Data.

Try exporting your friends birthdays from facebook (you need write another python script to convert that data into JSON format) or write a JSON file manually.

If you are exporting data from Facebook, Export the data from events page, you will get a ics file, write a python script to get the “DTSTART” and “SUMMARY” from ics file into a JSON File.

[
  {
    "date": "19900517",
    "name": "Friend A"
  },
  {
    "date": "20180518",
    "name": "Friend B"
  }
]

2. Write a Python Script.

Now that you have prepared your data, next you need to write a python script. Here’s my script:

#import the required packages
import datetime as date
import json
import os

#invoke the osascript to fire your notification locally.
def notify(title, text):
    os.system("""
              osascript -e 'display notification "{}" with title "{}"'
              """.format(text, title))

#open your json file
#make sure your specify the full path
with open('/Users/paras/WS/temp/data.json') as data_file:
    data = json.load(data_file)

#array to add birtdays for today
birthdays = []
#today's date
today = date.datetime.today()

#loop overs your data and find the birthdays that are today
# and add them to your list
for x in data:
    bday = date.datetime.strptime(x['date'], "%Y%m%d").date()
    if bday.day==today.day and bday.month==today.month:
        birthdays.append(x['name'])

#create a string from list by join method
msg = "\n".join(birthdays)

#fire your notification :)
if msg != "":
    notify("Birthdays Today",msg)

3. Try Running your script.

Try with some dates for today and run as python yourscript.py. If you get a notification, your are good to go with next steps, else fix the errors before you move to next steps.

If you need “Birthdays this week” feature try scripting it as follows:

if bday.isocalendar()[1]==today.isocalendar()[1] and (bday.day>=today.day):
        birthdays_in_this_week.append(x['name'])

4. Package your python script into App with Automator App in Mac.

Open Automator App and select Application

Creating Application in Automator

After you created a new application, a window will open as shown below, from left side panel search for “run shell script” and add Run Shell Script to our service. Now you can your python command with the file path. Make sure you add the complete path as “/Users/yourusername/path/to/your/file.py”

Add Run Shell Script to your App

Once you have added shell script, click on Run button on the top right to test your application. And save as Application in some place.

Go to: System Preferences -> Users & Groups -> Select User -> Login Items

Click on + button to add your application. The Application will now run on startup and you will get a notification about a friend’s birthday as soon as you login everyday to your mac.

Further you could write many wonderful scripts and package them into mac app. I would be happy to know if you find and create wonderful possible use-cases.

Join India's Largest Code Learning Community on Uphub.io Get it on Google Play
Sharing options: