LessonsIntermediate: Stacks, objects and dataLesson 29

Talking to the user

Tell the user something with answer, ask a question with ask, offer choices, and let them pick a file or a folder.

  • Intermediate
  • Lesson 29 of 83
  • About 8 minutes
  • 7 examples

Saying something: answer

answer shows a small dialog with a message and an OK button, and waits until the user clicks it. You met it in the very first lesson. Add with and a list of buttons, separated by or, and the button the user clicks comes back in it:

ScriptPut it in an object's script
on mouseUp
   answer "Save the changes to this stack?" with "Don't Save" or "Cancel" or "Save"
   switch it
      case "Save"
         save this stack
         break
      case "Don't Save"
         break
      case "Cancel"
         exit mouseUp
   end switch
   close this stack
end mouseUp

The last button is the default, the one the Return key presses. Put the safest choice last when a mistake would be costly.

Kinds of answer

A word after answer gives the dialog an icon that says what kind of message it is: information, question, warning or error. titled sets the dialog's title.

ScriptPut it in an object's script
on mouseUp
   answer information "The report is ready." titled "Report"
   answer warning "The disk is almost full."
   answer error "The file could not be opened."
end mouseUp

Asking a question: ask

ask shows a question with a box to type in. What the user typed comes back in it. If they click Cancel, it is empty and the result is Cancel, so always check before you use the answer:

ScriptPut it in an object's script
on mouseUp
   ask "What is your name?" with "Ada"
   if the result is "Cancel" then exit mouseUp
   put it into tName
   if tName is empty then
      answer "A name, please!"
   else
      answer "Hello," && tName & "!"
   end if
end mouseUp

The text after with is already in the box when the dialog opens. ask password is the same, but shows dots in place of what is typed.

Checking what was typed

People type all sorts of things. Keep the checking in a function of its own, which you can test without a dialog, and keep the handler that asks short:

ExampleRun it in the playground
function ageProblem pText
   if pText is empty then return "Please type your age."
   if pText is not an integer then return "Your age is a whole number, like 42."
   if pText < 0 or pText > 130 then return "That age does not look right."
   return empty
end ageProblem

put ageProblem("")
put ageProblem("forty")
put ageProblem("200")
put ageProblem("42") is empty
Output
Please type your age.
Your age is a whole number, like 42.
That age does not look right.
true
ScriptPut it in an object's script
on mouseUp
   repeat forever
      ask "How old are you?"
      if the result is "Cancel" then exit mouseUp
      put ageProblem(it) into tProblem
      if tProblem is empty then exit repeat
      answer tProblem
   end repeat
   answer "Thank you."
end mouseUp

Choosing a file or a folder

answer file shows the system's dialog for opening a file, and ask file the one for saving. Each puts the chosen file's full path in it, or sets the result to Cancel. answer folder chooses a folder.

ScriptPut it in an object's script
on mouseUp
   answer file "Choose a text file to open:" with type "Text files|txt|TEXT"
   if the result is "Cancel" then exit mouseUp
   put it into tPath
   put URL ("file:" & tPath) into field "Contents"

   ask file "Save a copy as:" with "copy.txt"
   if the result is "Cancel" then exit mouseUp
   put field "Contents" into URL ("file:" & it)
end mouseUp

The lesson Reading and writing files explains URL "file:".

More dialogs

  • answer color shows a colour picker, and puts the colour chosen in it.
  • answer printer shows the printer settings.
  • A stack of your own can be a dialog too: modal stack "Options" opens it and waits until it closes, and it can hand back a value in the dialogData.

Try it yourself

Write a button script that asks for two numbers, one after the other, and answers their sum. If either dialog is cancelled, stop quietly; if either answer is not a number, say so and stop.

Show a solution
ScriptPut it in an object's script
on mouseUp
   ask "First number:"
   if the result is "Cancel" then exit mouseUp
   put it into tFirst
   ask "Second number:"
   if the result is "Cancel" then exit mouseUp
   put it into tSecond
   if tFirst is not a number or tSecond is not a number then
      answer error "Those are not both numbers."
      exit mouseUp
   end if
   answer tFirst && "+" && tSecond && "=" && tFirst + tSecond
end mouseUp

Words in this lesson

Each one finds every lesson that uses it. The Dictionary has the whole story of each, with its syntax and more examples.