-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathMac-Task-Quit-Prohibited-App.sh
More file actions
55 lines (41 loc) · 1.4 KB
/
Mac-Task-Quit-Prohibited-App.sh
File metadata and controls
55 lines (41 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/bash
# This script is provided AS IS without warranty of any kind.
# https://github.com/Mac-Nerd/Mac-scripts
# -----------------------------------------------------------
# This script will attempt to gracefully quit a running app. If unsuccessful after a
# set timeout (default 60 seconds) the application will be more forcefully quit.
#
# Note: If the user has unsaved changes or open documents in the prohibited app, this will
# cause them to potentially be corrupted or lost. Use with caution.
AppToKill=$1
KillTimeout=$2
if [ -n "$AppToKill" ]
then
echo "[ERROR] This script requires the name of an app or process to quit."
exit 1002
fi
if [ -n "$KillTimeout" ]
then
KillTimeout=60 # default 60s timeout
fi
currentUser=$(stat -f "%S"u /dev/console)
currentUID=$(/usr/bin/id -u "$currentUser")
if [ "$currentUser" == "LoginWindow" ]
then
echo "No logged in user."
exit 1001
else
# is the application or process running?
if [ $(pgrep -ilf "$AppToKill") ] || [ $(pgrep -ilf "$AppToKill".app) ]
then
echo "Found $AppToKill. Quitting it."
/bin/launchctl asuser "$currentUID" /usr/bin/osascript -e "tell application \"${AppToKill}\" to quit"
fi
sleep "$KillTimeout"
# is the application or process *still* running?
if [ $(pgrep -ilf "$AppToKill") ] || [ $(pgrep -ilf "$AppToKill".app) ]
then
echo "$AppToKill still running. Force quitting it."
/usr/bin/pkill -9 -ilf "$AppToKill"
fi
fi