Skip to main content
Solved

Having an issue with notification with bash and apple script

  • June 18, 2020
  • 2 replies
  • 53 views

Forum|alt.badge.img+3

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

Best answer by mschroder

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.

2 replies

Forum|alt.badge.img+12
  • Valued Contributor
  • Answer
  • June 18, 2020

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.


Forum|alt.badge.img+3
  • Author
  • New Contributor
  • June 18, 2020

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