@dlondon I've made a few changes to your proof of concept.
The display dialog comes from "System Events" no need for the Finder.
AppleScript has an internal timeout if there is no action and the sample below will deal with that, probably there are other ways to do this but it works for us.
The same "cancelled" is captured if the user cancel the prompt.
1#!/bin/bash
2
3user_entry=""
4
5validateResponce() {
6 case "$user_entry" in
7 "noinput" ) echo "empty input" & askInput ;;
8 "cancelled" ) echo "time out/cancelled" & exit 1 ;;
9 * ) echo "$user_entry" ;;
10 esac
11}
12
13askInput() {
14user_entry=$(osascript <<EOF
15use AppleScript version "2.4" -- Yosemite (10.10) or later
16use scripting additions
17set theTextReturned to "nil"
18tell application "System Events"
19activate
20try
21set theResponse to display dialog "Please enter something" with title "Entry of something demo" default answer ""
22set theTextReturned to the text returned of theResponse
23end try
24if theTextReturned is "nil" then
25return "cancelled"
26else if theTextReturned is "" then
27return "noinput"
28else
29return theTextReturned
30end if
31end tell
32EOF
33)
34validateResponce "$user_entry"
35}
36
37askInput
38
39exit 0
Now if you will run this from Jamf a few extra changes will be required.
Jamf runs as root and we need to get the logged user "attention"
so the amended version will be like this:
1#!/bin/bash
2
3
4userName=$(ls -la /dev/console | cut -d " " -f 4)
5
6user_entry=""
7
8validateResponce() {
9 case "$user_entry" in
10 "noinput" ) echo "empty input" & askInput ;;
11 "cancelled" ) echo "time out/cancelled" & exit 1 ;;
12 * ) echo "$user_entry" ;;
13 esac
14}
15
16askInput() {
17user_entry=$(sudo -u "$userName" osascript <<EOF
18use AppleScript version "2.4" -- Yosemite (10.10) or later
19use scripting additions
20set theTextReturned to "nil"
21tell application "System Events"
22activate
23try
24set theResponse to display dialog "Please enter something" with title "Entry of something demo" default answer ""
25set theTextReturned to the text returned of theResponse
26end try
27if theTextReturned is "nil" then
28return "cancelled"
29else if theTextReturned is "" then
30return "noinput"
31else
32return theTextReturned
33end if
34end tell
35EOF
36)
37validateResponce "$user_entry"
38}
39
40askInput "$userName"
41
42exit 0
PS: Keep the osascript part to the left of the function, we found out some odd results when playing with formation/indentation.
I hope this helps!
Regards
Thanks @Mauricio - I''l check it next week. Your script skills are better than mine.
There was a reason I had the while loop there - one use for this is to get the computer name when rebuilding the machine after wiping it from the support person rebuilding it. When I look through the inventory I see cases of machines named Joe's macbook etc so I want the name to be part of that process and in their face. I don't want them to be able to continue without naming it.
@dlondon The validateResponce function has the check and the loop. If the support person just press OK it will ask for the input again.
1"noinput" ) echo "empty input" & askInput
As AppleScript may time out or the support person press cancel we are "capturing" that and stopping the process.
1"cancelled" ) echo "time out/cancelled" & exit 1 ;;
You could also remove the second button (Cancel) and offer just the OK one.
Thanks again @Mauricio I re-read it and ran the script. Pretty slick. Thank you for the example - very much appreciated. No need for the While statement when done this way