# 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](https://www.hexnode.com/mobile-device-management/help/how-to-run-scripts-on-mac-using-hexnode-mdm/) 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](https://www.hexnode.com/mobile-device-management/help/how-to-enable-live-terminal-on-your-macos-devices/) or the device’s built-in Terminal window. Command to install Homebrew on macOS devices
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" 
    
       1
    
    
    
      /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Once Homebrew is installed, you can deploy the following script from the portal to install **terminal-notifier**. Script to install terminal notifier on macOS devices
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    \#!/bin/bash CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }') CurrentUserUID=$(id -u "$CurrentUser") launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" brew install terminal-notifier 
    
       1
    
    2
    
    3
    
    4
    
    
    
      \#!/bin/bash 
    
    CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')
    
    CurrentUserUID=$(id -u "$CurrentUser")
    
    launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" brew install terminal-notifier

 

### Send custom message

Script to send custom notification on macOS devices

\#!/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

   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.

 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](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2023/06/Provide-notification-content-as-arguments.png "Provide notification content as arguments")](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2023/06/Provide-notification-content-as-arguments.png)

 

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](https:2023/06/Send-custom-notifications-using-terminal-notifier-to-macOS-devices.png "Send custom notification using terminal notifier to macOS devices")](https:2023/06/Send-custom-notifications-using-terminal-notifier-to-macOS-devices.png)

### 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](https://www.hexnode.com/mobile-device-management/help/how-to-find-the-bundle-id-of-an-application-on-mac/) 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

\#!/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"

   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.

Script to send clickable notification for opening mail app to macOS devices

\#!/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"

   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.

Script to send clickable notification for opening URL to macOS devices

\#!/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"

   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](https://www.hexnode.com/), deploy the following script.

Script to send clickable notification for opening hexnode website to macOS devices

\#!/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"

   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.

 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.