-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswiftDialog-timeout.sh
More file actions
87 lines (71 loc) · 2.2 KB
/
swiftDialog-timeout.sh
File metadata and controls
87 lines (71 loc) · 2.2 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/sh
##########
# with inspiration from
# https://stackoverflow.com/questions/1570262/get-exit-code-of-a-background-process
# https://github.com/bartreardon/swiftDialog
# https://github.com/bartreardon/swiftDialog/issues/231
##########
# set a few variable
dialogBin="/Library/Application Support/Dialog/Dialog.app/Contents/MacOS/Dialog"
commandFile="/var/tmp/dialog.log"
delaySec=$(/usr/bin/jot -r 1 5 15) # this will choose a random number between 5 and 15
# if we are root launch as the user in case we run from MDM like Jamf
if [ "$(/usr/bin/id -u)" -eq 0 ]; then
if [ -n "${commandFile}" ]; then
/usr/bin/touch "${commandFile}"
/bin/chmod 644 "${commandFile}"
fi
# get the loggedon username and user id
CurrentlyLoggedInUser=$(/usr/bin/stat -f %Su /dev/console)
CurrentlyLoggedInUserUID=$(/usr/bin/id -u "${CurrentlyLoggedInUser}")
# run dialog as the user
/bin/launchctl asuser "${CurrentlyLoggedInUserUID}" "${dialogBin}" \
--title Title \
--message Message \
--button1text OK \
--button2text Cancel \
--infobuttontext Help \
--button1disabled \
--button2disabled &
else
"${dialogBin}" \
--title Title \
--message Message \
--button1text OK \
--button2text Cancel \
--infobuttontext Help \
--button1disabled \
--button2disabled &
fi
# do a quick sleep to make sure the dialog is up and running and the commandFile is in place
sleep 0.5
# get the pid of the running dialog
myPID=$(/usr/bin/pgrep Dialog)
# sleep for the specified amount of time
sleep "${delaySec}"
# activate the buttons
/bin/cat << EOF >> "${commandFile}"
button1: enabled
button2: enabled
EOF
# wait here for the user to make a choice
wait "${myPID}"
# the user has made their choice so capture the exit code
myExit=$?
# evaluate the exit code in a case statement and do something based on the choice
case "${myExit}" in
0)
/bin/echo "OK clicked. dialog return code ${myExit}"
;;
2)
/bin/echo "Cancel clicked. dialog return code ${myExit}"
;;
3)
/bin/echo "Help clicked. dialog return code ${myExit}"
;;
*)
/bin/echo "Unknown error or dialog quit. dialog return code ${myExit}"
;;
esac
# add the rest of your script here if needed
exit 0