Notifications to End Users

thoule
Valued Contributor II

I like notification center for non-obtrusive messages so typically use Yo, Terminal-Notifier, or sometimes AppleScript. Sadly JAMFHelper takes focus so any message interrupts what the user is doing (typing an email..?) and get unhappy.

Looks like that's not possible in 10.11 anymore... This works 10.10 and under. What are people doing for notifications in scripts?

#!/bin/sh                                                                          

currentUser=`ls -l /dev/console | awk '{print $3}'`
notify(){
    message=$1
    lwpid=`ps auxw |grep loginwindow |grep -v grep | awk '{print $2}'`
    uid=`id -u $currentUser`
    gid=`id $currentUser|awk -Fgid= '{print $2}'|awk -F( '{print $1}'`
    /bin/launchctl bsexec $lwpid chroot -u $uid -g $gid / osascript -e "display notification "$message" with title "Updates""
}

notify "I love JAMF."
1 REPLY 1

brock_walters
Contributor

Hi Todd -

I looked at this today & had it on my list from long ago. Better late than never? Plus, these things hopefully always benefit someone...

This is actually a) something you can still do & b) is much easier to do now since OS X 10.9 -

sudo /usr/bin/osascript -e 'display notification "We <3 you too." with title "Message"'

That's it. :)

You can also use the Management Action.app installed as part of the JAMF Management Framework when a computer is enrolled which makes use of OS X Notification Center:

/Library/Application Support/JAMF/bin/Management Action.app/Contents/MacOS/Management Action -title 'MONEY' -message 'Check your Apple stock!'

There are some known issues around this at the moment. For more info see:

https://jamfnation.jamfsoftware.com/discussion.html?id=17245

Now, about those variables that you no longer need (ahem.)

You could use pgrep to get loginwindow process. It does the hard work for you:

/usr/bin/pgrep loginwindow

Because running launchctl bsexec requires sudo you were probably getting the root user's UID & GID when you executed the script with sudo. To get the UID & GID of the current logged in user you could do something like this instead:

uid=$(sudo -u $(/usr/bin/who | /usr/bin/awk '/console/{print $1}') id -u)
gid=$(sudo -u $(/usr/bin/who | /usr/bin/awk '/console/{print $1}') id -g)

Thanks!