Category filter

Script to send notifications to a Mac using terminal-notifier

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.
  • Once Homebrew is installed, you can deploy the following script from the portal to install terminal-notifier.

Send custom message

-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.
    Specify arguments to send notifications to Mac

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.
Display custom notification on macOS devices

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.

For example, to display a notification which opens the mail app when the user clicks on it, deploy the following script.

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.

For example, to display a notification which opens www.hexnode.com, deploy the following script.

Remember to provide a header (Example: Alert) and body (Example: Click here to open hexnode.com) as arguments.

Notes:

  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.

  • Sample Script Repository