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:
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.
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:
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:
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
Please type your age. Your age is a whole number, like 42. That age does not look right. true
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.
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 colorshows a colour picker, and puts the colour chosen init.answer printershows 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 inthe 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
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.