Skip to content

Instantly share code, notes, and snippets.

@sgmills
Created August 15, 2024 13:40
Show Gist options
  • Save sgmills/c7146947786e840e6a14f4ebf70cfcbd to your computer and use it in GitHub Desktop.
Save sgmills/c7146947786e840e6a14f4ebf70cfcbd to your computer and use it in GitHub Desktop.
#!/bin/bash
####################################################################################################
#
# Description
# This script triggers an alert or banner in notification center
#
# Use Jamf Pro script parameters to configure options
#
####################################################################################################
# VARIABLES
type="${4}" # Notification agent type. Options include: alert (persistent) or banner (temporary)
title="${5}" # Title for notification
subtitle="${6}" # Subtitle for notification
btn_label="${7}" # Text displayed on the button
btn_type="${8}" # Button type can be link or none
btn_payload="${9}" # Button payload if link type
timeout="${10}" # Button timeout in seconds
# Path to notification agent
NA_PATH="/Applications/IBM Notifier.app/Contents/MacOS/IBM Notifier"
####################################################################################################
# FUNCTIONS
# Function to check for IBM Notifier and install it if needed
agent_check () {
# Check if IBM Notifier is installed and working
agent_help=$("${NA_PATH}" --help > /dev/null 2>&1; echo $?)
tries=0
while [[ "$agent_help" != 200 ]]; do
if [[ $tries -gt 2 ]]; then
echo "Agent not detected. Exiting..."
exit 1
fi
echo "Agent not installed after $tries attempt(s), trying install..."
/usr/local/bin/jamf policy -event install-ibm-notifier
sleep 1
agent_help=$("${NA_PATH}" --help > /dev/null 2>&1; echo $?)
tries=$((++tries))
done
}
# Function to get alert exit code
wait_for_alert () {
# Pass a PID as argument 1 to this function and it will spit out the exit code once that process completes.
# Also works if the process was already closed before this funciton runs.
# The script pauses when this function is called and will not continue until a button is pressed or a timeout occurs.
# Capture the alert PID
waitPID=$1
# Wait a beat to ensure the alert has run
/bin/sleep 1
# Check for child process
childPID=$( pgrep -P "$waitPID" )
# Wait for process to exit or timeout
for i in $(seq 1 "$timeout"); do
if [[ "$childPID" ]]; then
/bin/sleep 1
# Re-check for child process
childPID=$( pgrep -P "$waitPID" )
else
break
fi
done
# Action taken or timeout reached
# If the alert is still active, kill it
if [[ "$childPID" ]]; then
kill -9 "$childPID" 2&> /dev/null
echo "Process timed out"
exit 0
fi
# Attempt to capture and return the alert exit code
wait "$waitPID"
alertSelection=$?
return "$alertSelection"
}
####################################################################################################
# DO THE THING
# Use function to ensure the agent is installed
agent_check
# Notify the user
"${NA_PATH}" \
-type "$type" \
-title "$title" \
-subtitle "$subtitle" \
-main_button_label "$btn_label" \
-main_button_cta_type "$btn_type" \
-main_button_cta_payload "$btn_payload" &
# Capture the PID of the alert that we sent to the background with the & above
alertPID=$!
# Use function to report the exit code or force timeout
wait_for_alert $alertPID
# Use a case statement to take different actions depending on what the user clicked
case "$alertSelection" in
0)
echo "User clicked button"
;;
239)
echo "User cancelled"
;;
*)
echo "Alert exited with unexpected code: $alertSelection"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment