Fork me on GitHub

Project Notes

#352 macOS Notifications with Ruby

Modern alternatives to the old growl notification system for ruby programming on macOS: osascript, terminal-notifier

Notes

Growl was a notification system for macOS developed in 2004. It was quite commonly used with ruby for desktop notifications, but has since become obsolete.

What are modern alternatives specifically for local/desktop messaging circa 2025? The two main ones that come to mind:

Using AppleScript/oascript

The osascript command can be used to execute AppleScript. Although poorly supported these days, some of the notification features still work.

For example, to always show a message in the foreground, can tell the “System Events” application to display dialog or display alert. Note that the display notification command no longer seems to work.

A dialog is displayed with both an OK and Cancel button:

osascript -e 'tell app "System Events" to display dialog "Hello World"'

dialog

An alert only has an OK button:

osascript -e 'tell app "System Events" to display alert "Hello World"'

alert

osascript -e 'tell app "System Events" to display notification "Hello World"'

From ruby, oascript can be invoked as a shell command. See oascript-example.rb:

#! /usr/bin/env ruby
app = "System Events"
message = "Ruby task finished"
system("osascript -e 'tell app \"#{app}\" to display alert \"#{message}\"'")

oascript-ruby

terminal-notifier gem

The terminal-notifier gem is used to send user notifications on macOS 10.10 or higher. It may be used from the command line or from ruby code.

Running from the command line:

terminal-notifier -title ProjectX -subtitle "new tag detected" -message "Finished"

On first use, the system will prompt for permissions:

terminal-notifier-perms

Subsequent messages will be displayed as expected:

terminal-notifier-cli-msg

Notifications may also be invoked directly from ruby using the gem’s API. See terminal-notifier-example.rb for an example:

#! /usr/bin/env ruby
require 'terminal-notifier'
TerminalNotifier.notify('Task finished successfully!', title: 'Ruby Script', subtitle: 'Done')

Execute the script:

ruby ./terminal-notifier-example.rb

The message is displayed as expected in the notification centre:

terminal-notifier-api-msg

Credits and References

About LCK#352 rubymacOS

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

Project Source on GitHub Return to the LittleCodingKata Catalog
About LittleCodingKata

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.

Follow the Blog follow projects and notes as they are published in your favourite feed reader