-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswiftDialog-timeout.sh
87 lines (71 loc) · 2.02 KB
/
swiftDialog-timeout.sh
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"
delay=$(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 [ $(id -u) -eq 0 ]; then
if [ ! -z $commandfile ]; then
touch $commandfile
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
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
my_pid=$(pgrep Dialog)
# sleep for the specified amount of time
sleep $delay
# activate the buttons
cat << EOF >> $commandfile
button1: enabled
button2: enabled
EOF
# wait here for the user to make a choice
wait $my_pid
# the user has made their choice so capture the exit code
my_exit=$?
# evaluate the exit code in a case statement and do something based on the choice
case $my_exit in
0)
echo "dialog return code $my_exit"
;;
2)
echo "dialog return code $my_exit"
;;
3)
echo "dialog return code $my_exit"
;;
*)
echo "dialog return code $my_exit"
;;
esac
# add the rest of your script here if needed
exit 0