@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.
#!/bin/bash
user_entry=""
validateResponce() {
case "$user_entry" in
"noinput" ) echo "empty input" & askInput ;;
"cancelled" ) echo "time out/cancelled" & exit 1 ;;
* ) echo "$user_entry" ;;
esac
}
askInput() {
user_entry=$(osascript <<EOF
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set theTextReturned to "nil"
tell application "System Events"
activate
try
set theResponse to display dialog "Please enter something" with title "Entry of something demo" default answer ""
set theTextReturned to the text returned of theResponse
end try
if theTextReturned is "nil" then
return "cancelled"
else if theTextReturned is "" then
return "noinput"
else
return theTextReturned
end if
end tell
EOF
)
validateResponce "$user_entry"
}
askInput
exit 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:
#!/bin/bash
userName=$(ls -la /dev/console | cut -d " " -f 4)
user_entry=""
validateResponce() {
case "$user_entry" in
"noinput" ) echo "empty input" & askInput ;;
"cancelled" ) echo "time out/cancelled" & exit 1 ;;
* ) echo "$user_entry" ;;
esac
}
askInput() {
user_entry=$(sudo -u "$userName" osascript <<EOF
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set theTextReturned to "nil"
tell application "System Events"
activate
try
set theResponse to display dialog "Please enter something" with title "Entry of something demo" default answer ""
set theTextReturned to the text returned of theResponse
end try
if theTextReturned is "nil" then
return "cancelled"
else if theTextReturned is "" then
return "noinput"
else
return theTextReturned
end if
end tell
EOF
)
validateResponce "$user_entry"
}
askInput "$userName"
exit 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.
"noinput" ) echo "empty input" & askInput
As AppleScript may time out or the support person press cancel we are "capturing" that and stopping the process.
"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