How to show notification?
Version 1: use SwiftDialog
Systemrequirements
macOS 11.x.x Big Sur SwiftDialog until version 2.2 from macOS 12.x.x Monterey latest version
#!/bin/zsh
dialogBinary="/usr/local/bin/dialog"
title="Jamf Enrollment"
message="Your Mac needs to be enrolled with Jamf! Please contact the Servicedesk."
${dialogBinary} \
--notification \
--title "${title}" \
--message "${message}"
returncode=$?
If you want to change default dialog app icon to yours, you need to put your icon into folder /Library/Application Support/Dialog
as Dialog.png and after this install Dialog.app and restart your Mac.
Version 2: use AppleScript with osascript
it is the easiest way, it works on older macs too, but it is not possible to customize the icon in notification:
#!/bin/sh
# check JAMF folder exists
# show notification
JAMFFolder="/Library/Application Support/JAMF"
if [ -d "$JAMFFolder" ]; then
echo "$JAMFFolder is a directory."
osascript -e 'display notification "Your Mac is already enrolled with Jamf!" with title "Jamf Enrollment" subtitle "Well done!"'
else
osascript -e 'display notification "Your Mac needs to be enrolled with Jamf!" with title "Jamf Enrollment" subtitle "Please contact the Servicedesk:"'
fi
Version 3: use AppleScript and create an App
use ScriptEditor with this script and build an app.
set JAMFFolder to POSIX path of "/Library/Application Support/JAMF"
tell application "System Events" to set theFolderExists to exists folder JAMFFolder
if theFolderExists then
display notification "Your Mac is already enrolled with Jamf!" with title "Jamf Enrollment" subtitle "Well done!"
else
display notification "Your Mac needs to be enrolled with Jamf!" with title "Jamf Enrollment" subtitle "Please contact the Servicedesk:"
end if
Change icon in the app, after this you can start app over LaunchAgent to special time:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>[package-identifier]</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/open</string>
<string>/Library/Application Support/[path to the app]</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>12</integer>
<key>Minute</key>
<integer>30</integer>
</dict>
<key>RunAtLoad</key>
<false/>
</dict>
</plist>
or start the app hourly with:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>[package-identifier]</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/open</string>
<string>/Library/Application Support/[path to the app]</string>
</array>
<key>StartInterval</key>
<integer>3600</integer>
<key>RunAtLoad</key>
<false/>
</dict>
</plist>
It is important that you create a LaunchAgent in user context if you want to activate agent without restart a Mac. You can try to do this with this script as postinstall script in your install package:
#!/bin/sh
# activate launch agent for each user
# Run postinstall actions for root.
echo "Executing postinstall"
# Add commands to execute in system context here.
# Run postinstall actions for all logged in users.
for pid_uid in $(ps -axo pid,uid,args | grep -i "[l]oginwindow.app" | awk '{print $1 "," $2}'); do
pid=$(echo $pid_uid | cut -d, -f1)
uid=$(echo $pid_uid | cut -d, -f2)
# Replace echo with e.g. launchctl load.
launchctl bsexec "$pid" chroot -u "$uid" / launchctl bootstrap gui/"$uid" /Library/LaunchAgents/<package-identifier>.plist
done
exit 0