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?

1 Answer 1

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 
5
  • 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)? Commented Jan 27, 2017 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. Commented Jan 27, 2017 at 12:10
  • Thank you. You can display an additional message using "display alert" to let your user know that an answer is required. Commented Jan 27, 2017 at 12:32
  • 1
    Great idea. That way the user understands what was responsible for causing the refresh. Commented Jan 27, 2017 at 12:50
  • See my second above. Commented Jan 27, 2017 at 13:43

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.