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
Send custom message
1 2 3 4 |
#!/bin/bash CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }') CurrentUserUID=$(id -u "$CurrentUser") launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" terminal-notifier -title "$1" -subtitle "$2" -sound default |
-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.
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.
1 2 3 4 |
#!/bin/bash CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }') CurrentUserUID=$(id -u "$CurrentUser") launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" terminal-notifier -title "$1" -subtitle "$2" -sound default -activate "bundle id of the app" |
For example, to display a notification which opens the mail app when the user clicks on it, deploy the following script.
1 2 3 4 |
#!/bin/bash CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }') CurrentUserUID=$(id -u "$CurrentUser") launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" terminal-notifier -title "$1" -subtitle "$2" -sound default -activate "com.apple.mail" |
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.
1 2 3 4 |
#!/bin/bash CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }') CurrentUserUID=$(id -u "$CurrentUser") launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" terminal-notifier -title "$1" -subtitle "$2" -sound default -open "URL of the website" |
For example, to display a notification which opens www.hexnode.com, deploy the following script.
1 2 3 4 |
#!/bin/bash CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }') CurrentUserUID=$(id -u "$CurrentUser") launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" terminal-notifier -title "$1" -subtitle "$2" -sound default -open "URL of the website" |
Remember to provide a header (Example: Alert) and body (Example: Click here to open hexnode.com) as arguments.