Posted on 01-13-2020 09:48 AM
username=$(/bin/launchctl asuser "$LoggedInUID" sudo -iu "$LoggedInUser" /usr/bin/osascript -e 'Tell application "System Events" to display dialog "Please enter username name and click Submit:" default answer "" buttons {"Submit"} with icon caution' -e 'text returned of result' 2>/dev/null)
echo $username
Posted on 01-13-2020 12:06 PM
Try just this, it works for me:
username=$(/usr/bin/osascript -e 'Tell application "System Events" to display dialog "Please enter username name and click Submit:" default answer "" buttons {"Submit"} with icon caution' -e 'text returned of result' 2>/dev/null)
Posted on 01-13-2020 02:04 PM
@alexjdale Thanks for responding, I'll test this and report the results.
Posted on 01-15-2020 12:00 PM
@alexjdale Hi Alex,
Two things:
1. This does not execute the script as the logged-in user which then leads to the users getting a security prompt.
2. However, this led me down the path of setting up PPPC, which worked as a solution for using osascripts to call System Events.
If you are interested, here is a youtube video explaining it:
https://www.youtube.com/watch?v=Po_h3KdgYmw&feature=youtu.be&t=1436s
you can get the AppleEvents config profile here to upload https://github.com/jamf/JamfPrivacyPreferencePolicyControlProfiles
and you can download the PPPC utility here
https://github.com/jamf/PPPC-Utility/releases
Thanks for your help!
Posted on 01-16-2020 04:10 PM
In addition to @gduncan.mbo's response, and a reply to the syntax in the original post:
In this instance, with a Jamf policy, you are running a command under a different user session (root), which launches launchctl
, which then tries to adopt the user's UID bootstrap/kernel namespace, then it tries running sudo -iu
as an adopted launchctl process - and because all of this is running outside the authorized sandbox, in which /usr/bin/osascript
is allowed, the command fails in the background (I got errors with securityd
, trustd
, and osascript
).
So the question is, what are you exactly trying to do with your users? Are you trying to run a policy to ask them a question? Why, in the context of your script, are you trying to run a command as a user, and ask the logged-in user for "the username", and store it in another variable username
?
Is your goal to return the value of the name of the current logged in user? Try this:
#!/bin/bash
currentUser="$(ls -l /dev/console | awk '{ print $3 }')"
userRealName="$(dscl . read /Users/${currentUser} RealName | awk '{ print $NF}')"
echo "{userRealName}"