Script to send notifications to a Mac using terminal-notifier
Jump To
Sending custom notifications to macOS devices allows admins to deliver personalized information to users and provide real-time updates conveniently. With Hexnode’s Execute Custom Script action, you can easily deploy scripts to send personalized notifications using the terminal-notifier tool to your devices.
Scripting language – Bash
File extension – .sh
Disclaimer:
The sample scripts provided below are adapted from third-party open-source sites.
Pre-requisites:
The script requires Homebrew and terminal-notifier tools to be installed on the devices.
You can install Homebrew by running the following command using either the Mac Live Terminal or the device’s built-in Terminal window.
-title: This parameter defines the header of the notification.
-subtitle: This parameter defines the body of the notification.
-sound: This parameter plays an alert sound when the notification appears. Use default to use the device’s default notification sound.
Mention the header and the body content as arguments separated by space in the Arguments field while deploying the script.
Notes:
While executing the scripts, you may specify any number of arguments (each separated by a space) in the Arguments field. You must use single quotes to enclose each argument that comprises two or more words.
For instance, specifying the following string, ‘Important Alert’‘Please restart your device’ in the Arguments field considers Important Alert as the first argument and Please restart your device as the second argument respectively.
For example, if you deploy a notification with the header Important Alert and body Please restart your device, the device receives a notification in the following manner.
Send clickable notifications
While sending custom messages, you can also configure clickable actions like opening an app or a URL when the user clicks on the notification received. This can be achieved by adding specific parameters to the script.
To open an app
By adding -activate parameter along with app bundle id to the terminal-notifier command, you can send a notification to the device that opens the app when the user clicks on it.
Script to send clickable notification for opening app to macOS devices
Remember to provide a header (Example: Alert) and body (Example: Click here to open the mail app) as arguments.
To open a URL
You can also send a notification that opens a URL when the user clicks on it by adding -open parameter along with the URL to the terminal-notifier command.
Script to send clickable notification for opening URL to macOS devices