How to display MacOS native notifications

Native system notifications on MacOS can be shown using the display notification AppleScript command. This takes the string to display and optional parameters like title, subtitle, and sound.

We can execute this command using osascript. This is an utility that allows us to run scripts written in AppleScripts.

We use GtSubprocessWithInMemoryOutput Object subclass: #GtSubprocessWithInMemoryOutput instanceVariableNames: 'builder shellCommand process errorBlock command envVariables semaphore stdoutStream stderrStream stdoutBuffer stderrBuffer outputPoll terminateOnShutdown stdinStream retryCount pollException' classVariableNames: '' poolDictionaries: 'LibCSignalSharedPool' package: 'GToolkit-Utility-System' to call osascript.

GtSubprocessWithInMemoryOutput
	waitOnCommand: '/usr/bin/osascript'
    arguments: { 
         '-e'.
         'display notification "Hello from Glamorous Toolkit"' }
  

We can also make a version where we customize the notification parameters.

First we set the command parameters.

message := 'Hello from Glamorous Toolkit'.
title := 'Notification'.
subtitle := 'Message shows'.
soundName := 'Submarine'.
  

Then we create the notification script.

scriptScript := 'display notification "{message}"  with title "{title}" subtitle "{subtitle}" sound name "{soundName}"'
	format: {
		#message ->message.
		#title ->title.
		#subtitle ->subtitle.
		#soundName ->soundName } asDictionary.
  

And finally we execute the script.

GtSubprocessWithInMemoryOutput
	waitOnCommand: '/usr/bin/osascript'
    arguments: { 
         '-e'. scriptScript }