How to Create a Python Desktop Notification on Mac?

Estimated read time 1 min read

You can create a desktop notification on Mac using the osascript command in Python. Here’s an example:

import os

def send_notification(title, message):
    os.system(f"osascript -e 'display notification \"{message}\" with title \"{title}\"'")

send_notification("Hello World", "This is a notification.")

In this example, we define a function send_notification() that takes two arguments: a title string and a message string. The function uses the os.system() function to run an AppleScript command that displays a notification with the specified title and message.

To use this function, simply call send_notification() with the desired title and message. For example, send_notification("Hello World", "This is a notification.") would display a notification with the title “Hello World” and the message “This is a notification.”

Note that in order for this to work, you need to have “System Events” enabled in the Accessibility settings. You can find this in System Preferences > Security & Privacy > Privacy > Accessibility, and add your terminal or IDE to the list of apps allowed to control your computer.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply