3

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?

2

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
  • It's very close. The only difference is that I can press the "OK" button in your version; the dialog is refreshed if I press "OK". Is there no way to actually disable a button in AppleScript (like the "Ask for Text" action disables "OK" when the field is empty)? – rubik's sphere Jan 27 '17 at 12:06
  • It's not possible to disable a button in a dialog box created with AppleScript. You can accomplish that with Swift, though. – oa- Jan 27 '17 at 12:10
  • Thank you. You can display an additional message using "display alert" to let your user know that an answer is required. – oa- Jan 27 '17 at 12:32
  • 1
    Great idea. That way the user understands what was responsible for causing the refresh. – rubik's sphere Jan 27 '17 at 12:50
  • See my second above. – oa- Jan 27 '17 at 13:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .