Send Custom Mac System Notifications Using Python | by Daniel Ellis | Apr, 2022

Picture by Brett Jordan on Unsplash

When coding, it’s common that we would wish to present the consumer with updates on progress or script termination.

That is significantly true for packages that run within the background and should have been forgotten about within the interim. I

n this text, we focus on three strategies for offering updates to the consumer:

  • Pop-up type system notifications / alerts
  • A system beep or sound
  • Voice dictation of a specified textual content string

Within the Terminal, we’re in a position to execute apple script instructions utilizing osascript -e flag. This enables us to generate a popup tied to an software coupled with a message. An instance the place we inform the Terminal emulator to show ‘script accomplished’ might be supplied with:

inform software "Terminal" to show alert "script accomplished!"

Placing these collectively implies that the complete command turns into:

osascript -e 'inform software "Terminal" to show alert "script accomplished!"'

Subsequent, we will play a system sound, in case the consumer is distracted (or away from their desktop). Obtainable sounds might be discovered with ls /System/Library/Sounds/ with a few of the frequent ones outlined under:

ls /System/Library/Sounds/Basso.aiff 
Frog.aiff
Hero.aiff
Pop.aiff
Submarine.aiff
Blow.aiff
Funk.aiff
Morse.aiff
Purr.aiff
Tink.aiff
Bottle.aiff
Glass.aiff
Ping.aiff
Sosumi.aiff

As soon as now we have chosen a sound (e.g. Submarine) we will play it utilizing afplay:

afplay /System/Library/Sounds/Submarine.aiff

Macs even have a quite simple voice dictation software which is accessible from the Terminal. Right here as an illustration, if we wished to announce “The script has been executed efficiently”, we will use the say command as follows:

say “The script has been executed efficiently”

Lastly, we construct a easy Python* script able to executing the above instructions as system calls.

To make sure we don’t interrupt the script, we both want to make use of non-blocking system calls or run the apple script and say instructions within the background utilizing the & image.

Equally, for comfort, we wrapped up all of the capabilities into an Alerts class with an ‘finish’ property permitting parenthesis free utilization for the ultimate name of this system (as this may almost definitely be the commonest utilization)

*Word, this may be accomplished via any programming language, however on the time of writing I most want it for Python

Utilization

An instance of learn how to use the category might be seen within the ‘usage_example’ operate. In essence, we save the code above and start by importing the Alerts class

from mac_notify import Alerts

Subsequent, we initialise the category, and use the capabilities are required:

information = Alerts()information.notify('This can be a notification')

We explored three strategies to tell a consumer of checkpoints in a program (ought to they be utilizing a mac). Following this, we then created a python script we will substitute into our scripts.

Within the case our customers are utilizing a Linux machine as a substitute then we could take a look at libnotify and notify-send for notifications and gTTS for voice dictation.

More Posts