How to “require an answer” in a dialog in AppleScript

applescriptautomatordialogtext;

If one inserts an "Ask for Text" action into their workflow in Automator, they will be presented with the option to "Require an answer" of the user:

enter image description here

If the user fails to provide an answer, the user cannot press the "OK" button. The computer will beep at the user if the "OK" button is pressed while the answer field is empty.

Is there a way to replicate this exact behavior in AppleScript?

Best Answer

Yes, it can be accomplished like this:

repeat
  set myAnswer to the text returned of (display dialog "Your question goes here." default answer "")
  if myAnswer is "" then
    beep
  else
    exit repeat
  end if
end repeat

Please let me know if this works for you.


Second version that tells the user what the error is:

repeat
  set myAnswer to the text returned of (display dialog "Your question goes here." default answer "")
  if myAnswer is "" then
    beep
    display alert "Please enter your answer to continue."
  else
    exit repeat
  end if
end repeat