Self Service - Get User Input with Script

dlondon
Valued Contributor

I've been banging my head against this issue of trying to get User Input when running a script in Self Service for machines with Mac OS 10.14.

The script below shows a working proof of concept. You do get prompted to allow Jamf to access Finder. That can be overcome with a pppc - links to more info in the script comments.

Feel free to comment/improve

#!/bin/bash
# GetUserInputFromSelfService.bash
# Proof of concept
# Applescript query inside bash script.  Used as a building block to get user info from a script run in JAMF Self Service
# David London - Shamelessly hacked from a much bigger script by James Toher
# On 10.14 onwards the user will either have to allow Jamf to access Finder or a PPPC Configuration Profile will need to be installed prior to running this
# A good pppc is:
# https://github.com/rtrouton/privacy_preferences_control_profiles/tree/master/Privacy%20Settings%20Whitelist%20-%20Jamf%20Notifications 
# you will need tccprofile.py to make it
# https://github.com/carlashley/tccprofile
# Richard Trouton as always is a font of knowledge https://derflounder.wordpress.com/2018/08/31/creating-privacy-preferences-policy-control-profiles-for-macos/
# 2019-10-23

user_entry=""

while  [ "$user_entry" = "" ] ; do

user_entry=$(osascript -e '
tell application "Finder"
    activate
    try
        display dialog "Please enter something" with title "Entry of something demo" default answer ""
        set user_entry to the (text returned of the result)
    on error number -128
        set user_entry to ""
    end try
    return user_entry
end tell')

done

# do something with $user_entry

exit 0
4 REPLIES 4

Mauricio
Contributor III

@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

dlondon
Valued Contributor

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.

Mauricio
Contributor III

@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.

dlondon
Valued Contributor

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