Skip to main content
Question

Self Service - Get User Input with Script


dlondon
Forum|alt.badge.img+14
  • Honored Contributor
  • 376 replies

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

1#!/bin/bash
2# GetUserInputFromSelfService.bash
3# Proof of concept
4# Applescript query inside bash script. Used as a building block to get user info from a script run in JAMF Self Service
5# David London - Shamelessly hacked from a much bigger script by James Toher
6# 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
7# A good pppc is:
8# https://github.com/rtrouton/privacy_preferences_control_profiles/tree/master/Privacy%20Settings%20Whitelist%20-%20Jamf%20Notifications
9# you will need tccprofile.py to make it
10# https://github.com/carlashley/tccprofile
11# Richard Trouton as always is a font of knowledge https://derflounder.wordpress.com/2018/08/31/creating-privacy-preferences-policy-control-profiles-for-macos/
12# 2019-10-23
13
14user_entry=""
15
16while [ "$user_entry" = "" ] ; do
17
18user_entry=$(osascript -e '
19tell application "Finder"
20 activate
21 try
22 display dialog "Please enter something" with title "Entry of something demo" default answer ""
23 set user_entry to the (text returned of the result)
24 on error number -128
25 set user_entry to ""
26 end try
27 return user_entry
28end tell')
29
30done
31
32# do something with $user_entry
33
34exit 0

4 replies

Mauricio11
Forum|alt.badge.img+11
  • Valued Contributor
  • 173 replies
  • October 24, 2019

@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


dlondon
Forum|alt.badge.img+14
  • Author
  • Honored Contributor
  • 376 replies
  • October 25, 2019

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.


Mauricio11
Forum|alt.badge.img+11
  • Valued Contributor
  • 173 replies
  • October 28, 2019

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


dlondon
Forum|alt.badge.img+14
  • Author
  • Honored Contributor
  • 376 replies
  • October 28, 2019

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


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings