osascript continously prompting

Eskobar
Contributor

 

Hello, my script continously asking for user password id blank is left empty ! any idea to fix it:
 

 

# Password validation loop.
TRY=1
until /usr/bin/dscl /Search -authonly "$user" "$USER_PASS" &>/dev/null; do
(( TRY++ ))
echo "Prompting $user for their Mac password (attempt $TRY)..."
USER_PASS=$(osascript -e 'tell application "System Events" to display dialog "Please, type your Password to start the operation:" default answer "" with title "test" with hidden answer buttons {"OK"} default button 1' -e 'text returned of result' 2>/dev/null)

if (( TRY >= 5 )); then
echo "[ERROR] Password prompt unsuccessful after 5 attempts. Exiting script..."
exit 1
fi
done
6 REPLIES 6

jamf-42
Valued Contributor II

where is $user coming from? don't you need switches for -u -P ? 

do you want to run dscl in a sub process? your doing a redirect to dev null after the ampersand? 

Confused..

mm2270
Legendary Contributor III

It works as long as it knows what $user is. Not sure if this is the whole script or just a snippet of it, but you need to make sure the $user variable is being populated correctly or the dscl command will always fail.

talkingmoose
Moderator
Moderator

Here’s a snippet I use. Instead of allowing five attempts, it’ll repeatedly prompt until either the response is not blank or the dialog is canceled. I’ve also added “with hidden answer” since you’re wanting to capture a password. This will obscure the response.

#!/bin/zsh

while [ "$theText" = "" ]
do
	pass=$( /usr/bin/osascript -e "display dialog \"Please, type your Password to start the operation:\" default answer \"\" buttons {\"Cancel\",\"OK\"} default button {\"OK\"} with title \"Test\" with icon POSIX file \"/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertNoteIcon.icns\" with hidden answer" 2>&1 )
	
	theText=$( echo "$pass" | /usr/bin/awk -F "text returned:" '{print $2}' )
	
	if [[ $pass == *"User canceled"* ]]; then
		exit 1
	fi
done

 

Hello @talkingmoose ,

This awesome!

However, if prompt remains blank am not forced to fulfill it. I can just drag the popup a way and continue working. Is there a way to force user to type his pwd instantly link in mac software installer ? or make the pop up full screen?

Thanks a lot

For that level of control, which you can’t get with osascript, you may want to investigate something like Nudge.

jamf-42
Valued Contributor II

you want to use swift dialog.. https://github.com/bartreardon/swiftDialog