Created
April 4, 2017 16:08
-
-
Save haircut/acb72b795e9427691716b487d4dd2592 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Script: Safely Close Application | |
# | |
# Description: | |
# Safely closes and application after alerting user and prompting to save | |
# any unsaved documents (if applicable). | |
# | |
# Parameters: | |
# - app_name: Name of the application | |
# - app_process: Process name if different than app name | |
# - app_icon: Path to icns of application icon | |
# | |
# Author: | |
# Matthew Warren <[email protected]> | |
# | |
# Created: 2016-01-23 | |
# Modified: 2017-02-08 | |
#################### | |
# Read parameters | |
app_name=${4:-"Application"} | |
app_process=$5 | |
app_icon=$6 | |
# Location of CocoaDialog binary | |
ccd="/path/to/cocoaDialog.app/Contents/MacOS/cocoaDialog" | |
# Make sure CocoaDialog is installed; if not, attempt to fix it via policy | |
if [[ ! -a "${ccd}" ]]; then | |
echo "Attempting to install CocoaDialog via policy" | |
/usr/local/jamf/bin/jamf policy -forceNoRecon -event InstallCocoaDialog | |
if [[ -z "${ccd}" ]]; then | |
echo "Unable to install CocoaDialog, so we need to quit" | |
exit 1 | |
else | |
echo "CocoaDialog is now installed" | |
fi | |
fi | |
# Check if the application is running and store its process id | |
if [[ ! -z $app_process ]]; then | |
app_pid=$(pgrep "${app_process}") | |
else | |
app_pid=$(pgrep "${app_name}") | |
fi | |
# If application is running alert user it must close | |
if [[ $app_pid ]]; then | |
echo "${app_name} is running; alerting user it must close" | |
# Look for the app's icon | |
if [[ -z $app_icon ]]; then | |
icon_candidate_1="/Applications/${app_name}.app/Contents/Resources/AppIcon.icns" | |
icon_candidate_2="/Applications/${app_name}.app/Contents/Resources/${app_name}.icns" | |
if [[ -f "${icon_candidate_1}" ]]; then | |
app_icon_path=$icon_candidate_1 | |
elif [[ -f "${icon_candidate_2}" ]]; then | |
app_icon_path=$icon_candidate_2 | |
else | |
app_icon_path="/Applications/Self Service.app/Contents/Resources/Self Service.icns" | |
fi | |
else | |
app_icon_path=$app_icon | |
fi | |
# Alert user | |
"${ccd}" msgbox --title "${app_name} Update" --text "${app_name} needs to be closed." --informative-text "To safely install updates, ${app_name} must be closed. You will be prompted to save any unsaved documents before the application shuts down." --button1 "Close ${app_name} and Update" --no-cancel --icon-file "${app_icon_path}" --icon-size 64 | |
# Close app gracefully with AppleScript | |
osascript -e "tell application \"${app_name}\" to activate" -e"tell application \"${app_name}\" to quit" | |
echo "Closed ${app_name}" | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment