Skip to main content

I have a script that prompts a user for input using apple script for their name within my bash script. It seems to work great, but if they do not answer the prompt in about 1 min it prompts again because of a loop I have. Each time it prompts, another window appears on top of the other. So I either need to kill the dialog and I can't figure that out, or have my loop not keep trying every min and I'm not sure how do that either. Below is what my script looks like.



PromptDisplayName ()
{
DISPLAYNAME=$(/usr/bin/osascript << EOF
tell application "System Events"
activate
set DISPLAYNAME to display dialog "Enter Full Name: " default answer "John Doe" with icon note buttons {"OK"} default button 1 with title "Enter Full Name"
return text returned of DISPLAYNAME as string
end tell
EOF
)



Loop until Displayname is in the correct format



until [[ "$DISPLAYNAME" =~ ([a-zA-Z]){3,}[[:blank:]]([a-zA-Z]+){3,} ]]
do
PromptDisplayName
done



}



Call on function to prompt user



PromptDisplayName

Hi,



have a look at https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/DisplayDialogsandAlerts.html, in particular the 'giving up after X' part. This closes the dialog after X seconds. Choose X so its small enough for your repetition rate.



Hope this helps.


Thanks mschoder! That did it. The bash loop was on a 2 min timer so I just added this to the end of my my applescript line, giving up after 119. See line below.



tell application "System Events" activate set DISPLAYNAME to display dialog "Enter Full Name: " default answer "John Doe" giving up after 119