terminal-notifer Scripts Via the JSS

terminal-notifer Scripts Via the JSS

Scripts run as root when executed via the JSS.  This caused problems when trying to send notifications using terminal-notifier to users because root does not have a NotificationCenter instance, so any script using terminal-notifier would hang.  The trick to get around that is to run the command as the user.

I have been converting a lot of my shell scripts over to Python as it is more flexible and powerful.  This is script I wrote to send notifications while running as root.

import sys,pwd,os
import subprocess
from platform import mac_ver

v, _, _ = mac_ver()
v = float('.'.join(v.split('.')[:2]))

# Use Casper parameters 4-7
title = sys.argv[4]
subtitle = sys.argv[5]
message = sys.argv[6]
group = sys.argv[7]

#-------BEGIN SCRIPT-----------
if (v == 10.9) or (v == 10.10):
	u = os.getlogin()
	if ( u == '_atsserver') or ( u == 'root'):
		print u + " owns the loginwindow--no notification will be sent."
		print u + " owns the loginwindow--sending notification..."
		pw = pwd.getpwnam(u)
		os.execve('/Applications/Hopkins Tech Support.app/Contents/MacOS/notifier',["","-title",title,"-subtitle",subtitle,"-message",message,"-group",group],env);
	print "Notifications not supported version " + str(v)