Terminal Notifier Script Help

jhbush
Valued Contributor II

I'm trying to make a template in the JSS for notifications using Terminal Notifier. I made this script quick to do some sanity checks, but it still doesn't work like I expect. Notifications don't come through for some reason.```

!/bin/bash

HARDCODED VALUES ARE SET HERE

title="$7"
message="$8"
loginwindowuser=ps auxc | grep loginwindow | grep root|awk '{print $1}'
osvers=$(sw_vers -productVersion | awk -F. '{print $2}')
gem=gem list terminal-notifier -i

########################################################################################

# SCRIPT CONTENTS - DO NOT MODIFY BELOW THIS LINE

########################################################################################

if [[ ${osvers} -lt 8 ]]; then

echo "10.8 not installed"

exit 0;

else

if [[ ${gem} != true ]]; then

echo "Terminal Notifier not installed"

exit 0;

else

if [ "$loginwindowuser" == "root" ]; then

echo "At Login Window"

exit 0;

else

terminal-notifier -message "$8" -title "$7"

fi

fi

fi

exit 0
```

3 REPLIES 3

jagress
New Contributor III

This terminal-notifier thing is cool! Thanks for posting about it.

I've cleaned up your script a bit and it seems to work just fine:

#!/bin/bash

# HARDCODED VALUES ARE SET HERE
title="$1"
message="$2"
loginwindowuser=`ps auxc | grep loginwindow | grep root|awk '{print $1}'`
osvers=$(sw_vers -productVersion | awk -F. '{print $2}')
gem=`gem list terminal-notifier -i`
####################################################################################################
#
# SCRIPT CONTENTS - DO NOT MODIFY BELOW THIS LINE
#
####################################################################################################

if [[ ${osvers} -lt 8 ]]; then
        echo "10.8 not installed"
elif [[ ${gem} != true ]]; then
        echo "Terminal Notifier not installed"
elif [[ "${loginwindowuser}" == "root" ]]; then
        echo "At Login Window"
else
        terminal-notifier -message "$1" -title "$2"
fi

I used $1/$2 for command line args since I just tested it locally, not via Casper. Try it out and see if it works for you.

jhbush
Valued Contributor II

Updated a bit more since for some reason Terminal Notifier needs to run as the console/logged in user at least once to add itself to Notification Center

#!/bin/bash

# HARDCODED VALUES ARE SET HERE
user=`ls -la /dev/console | cut -d " " -f 4`
title="$7"
message="$8"
loginwindowuser=`ps auxc | grep loginwindow | grep root|awk '{print $1}'`
osvers=$(sw_vers -productVersion | awk -F. '{print $2}')
gem=`gem list terminal-notifier -i`
####################################################################################################
#
# SCRIPT CONTENTS - DO NOT MODIFY BELOW THIS LINE
#
####################################################################################################

if [[ ${osvers} -lt 8 ]]; then

        echo "10.8 not installed"

elif [[ ${gem} != true ]]; then

        echo "Terminal Notifier not installed"

elif [[ "${loginwindowuser}" == "root" ]]; then

        echo "At Login Window"

else

        su "${user}" -c 'terminal-notifier -message "$8" -title "$7"'

fi

jagress
New Contributor III

Now that I'm thinking about this, I would add a line to install terminal-notifier if it isn't already installed. That will ensure all 10.8 can run this script.

# Only try this if we're on 10.8+
if [[ ${osvers} -lt 8 ]]; then
        echo "10.8 not installed"
else

     # Install terminal-notifier if necessary
     if [[ ${gem} != true ]]; then
             echo "Terminal Notifier not installed. Installing..."
             gem install terminal-notifier
     fi

     # Only send notification if a user is logged in
     if [[ "${loginwindowuser}" == "root" ]]; then
             echo "At Login Window"
     else
             terminal-notifier -message "$8" -title "$7"
     fi

fi