Skip to content

Instantly share code, notes, and snippets.

@MarcAlx
Last active March 15, 2023 12:54
Show Gist options
  • Save MarcAlx/f7a51a245c7c0450dbd4f4203b9916e0 to your computer and use it in GitHub Desktop.
Save MarcAlx/f7a51a245c7c0450dbd4f4203b9916e0 to your computer and use it in GitHub Desktop.
Send macOS notifications from python 3
#!/usr/local/bin/python3.4
# -*- coding: utf-8 -*-
# Crée par MarcAlx
import os
def displayNotification(message,title=None,subtitle=None,soundname=None):
"""
Display an OSX notification with message title an subtitle
sounds are located in /System/Library/Sounds or ~/Library/Sounds
"""
titlePart = ''
if(not title is None):
titlePart = 'with title "{0}"'.format(title)
subtitlePart = ''
if(not subtitle is None):
subtitlePart = 'subtitle "{0}"'.format(subtitle)
soundnamePart = ''
if(not soundname is None):
soundnamePart = 'sound name "{0}"'.format(soundname)
appleScriptNotification = 'display notification "{0}" {1} {2} {3}'.format(message,titlePart,subtitlePart,soundnamePart)
os.system("osascript -e '{0}'".format(appleScriptNotification))
if __name__ == '__main__':
#displayNotification("message","title","subtitle","Pop")
#displayNotification("message","title","subtitle")
displayNotification("message","title")
#displayNotification("message")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment