Communicating with the User
You will often want to be able to communicate with your user, to ask them questions, allow them to select a folder or file or give them options or information.
LiveCode provides a number of built in dialogs to allow this type of communication, these dialogs are displayed natively on all platforms.
The Answer Dialog
The answer dialog allows you to display information on screen, and optionally allows the user to make a choice from a list of up to seven choices. If no choices are specified then an "OK" button is shown.
The answer command allows you to specify the text and button choices and returns the button that was clicked. You can also specify the window title, as well as an icon to be displayed.
The font, object positions, button order and icon automatically change to reflect the operating system.
Example:
answer question "Do you want to continue?" with "Yes" or "No" or "I don't know"
The Ask Dialog
The ask dialog allows you to ask the user a question where they can type their answer.
The ask command allows you to specify the question, window title and icon. As with the answer dialog the font, object positions, button order and icon automatically change to reflect the operating system.
The ask dialog is shown with "OK" and "Cancel" buttons. If the user clicks "OK" the content of the text box is placed in the it variable and the result function returns empty. If the user cancels the dialog the it variable is set to empty and the result function returns cancel.
Example:
ask "What is your name?"
Note: The it variable is a special local variable that is used to get the result of certain commands such as get, read from file, convert, ask, and answer. The it variable can be used like any other local variable: you can put a value into it, or put it into another container.
File Selector Dialogs
The file selector dialogs allow you to display the system standard dialogs. These dialogs allow the user to select a file or a set of files, select a directory, or specify a name and location to save a file.
The Answer File Dialog
The answer file command displays a standard file dialog for the user to select a file. You can specify a prompt and an optional defaultPath.
The file path to the file selected by the user is returned in the it variable. If the user cancelled the dialog, the it variable will be empty and "cancel" will be returned by the result function.
Example:
answer file "Select the files you wish to process:"
The Ask File Dialog
The ask file command displays a standard Save dialog for the user to enter a file name and specify a location. You can specify a prompt and an optional defaultPath.
The file path to the file selected by the user is returned in the it variable. If the user cancelled the dialog, the it variable will be empty and "cancel" will be returned by the result function.
Example:
ask file "Please name the file:"
The Answer Folder Dialog
The answer folder command displays a standard file dialog for the user to choose a folder. You can specify a prompt and an optional defaultPath.
The file path to the file selected by the user is returned in the it variable. If the user cancelled the dialog, the it variable will be empty and "cancel" will be returned by the result function.
Example:
answer folder "Please choose a folder:"
The Answer Color Dialog
The answer color command displays the operating system's standard color-selection dialog box, allowing the user to choose a color. You can specify an optional startingColor. If you specify a startingColor, the dialog box displays that color by default.
The color the user chooses is placed in the it variable. If the user cancels the dialog, the it variable is set to empty, and the result function returns "Cancel".
The color is returned in the form of three comma-separated integers between zero and 255, specifying the level of each of red, green, and blue. This format can be used directly to set any color property.
Example:
answer color with "blue"
Liz
If I want to program to do different things depending on which buttons are pushed for an "answer question" script, how do I go about doing that?
My script for a "next" button looks something like this:
on mouseUp
answer question "Are you sure you want to continue?" with "Yes" or "No"
if it is "Yes"
then go to the next card
end if
end mouseUp
I get an error at the "end if" line, saying "compilation error at line 5 (Handler: end doesn't match handler name) near "if", char 4."
If there's another lesson/ tutorial somewhere that can teach me how to do this, all I need is the link to it. If not, instructions would be greatly appreciated.
Hanson Schmidt-Cornelius
Hi Liz,
LiveCode processes one line at a time and uses new lines to separate a new logical instruction. The syntax for if...then...end if statements requires in your example that the "then" statement should be on the same line as the "if" statement.
All you have to do is move "then" to the line above it.
Kind Regards,
Hanson
Liz
Hi Hanson,
I've tried what you suggested, plus a few variations:
Putting the if, then, and end if all on the same line ("compilation error at line 3 (Handler: end doesn't match handler name) near "if", char 44")
Putting the end if below the if-then (compilation error at line 4 (Handler: end doesn't match handler name) near "if", char 4)
Using "end" instead of "end if" and putting it all on the same line ("compilation error at line 3 (Handler: missing handler name after end), char 39")
Using "end" instead of "end if" and putting it below the if-then ("compilation error at line 4 (Handler: missing handler name after end), char 1")
Do you have any other suggestions of how to resolve the errors?
Hanson Schmidt-Cornelius
Hi Liz,
here is an updated version of the initial code you posted with a small change. This should work for you.
on mouseUp
answer question "Are you sure you want to continue?" with "Yes" or "No"
if it is "Yes" then
go to the next card
end if
end mouseUp
Kind Regards,
Hanson
Bruce
I liked this exercise and I wanted to take the points learned in this exercise to expand the previous "Buttons" one. So I took the previous exercise and I changed one of the buttons to be named "custom". Then if the user pushes the custom button it will ask him or her to select a color with a default color of "yellow" already selected.
Here is my code for the main stack (no button code).
command colorText pColor
if pColor is "custom" then
answer color with yellow
put it into tColor
set the textColor of field "text" to tColor
else
set the textColor of field "text" to pColor
end if
end colorText
on mouseUp
put the short name of the target into tColor
colorText tColor
end mouseUp
Thomas Bodetti
I have a situation where it would be beneficial to exit to top or to go to the next line, answer question "This will clear all Tab Icons so you can start over" with "Yes" or "No"
if it is yes then go to the next line else if it is no then exit to top (can I call a function on exit) sorry if this is not very clear,
Hanson Schmidt-Cornelius
Hi Thomas,
you can examine the "it" variable to see what the user selected. Based on the result you can then take the action you wish. This could also be a call to a function.
Kind Regards,
Hanson
Brian
Ok, hopefully this is simple,
I have a basic answer dialog with Yes or No options for the buttons.
I click Yes, and then use an If to check it. Whether I click Yes or No, the it variable is always returned as No.
Here's the code.
answer question "We could not find any databases at " & tDBFile & ". Do you want to create one?" with "Yes" or "No"
put it into QAnswer
answer question "it is " & QAnswer
If QAnswer is "Yes" Then
toplevel stack "SetupSaveSourceMain"
# from SetupSaveSourceMain I have to then connect to, or create the Database
End If
Elanor Buchanan
Hi
I can't see anything wrong with your code here, I tested it by creating a button and setting the script to
on mouseUp
answer question "We could not find any databases at " & tDBFile & ". Do you want to create one?" with "Yes" or "No"
put it into QAnswer
answer question "it is " & QAnswer
end mouseUp
When I click the button I get the first dialog, if I click "Yes" I get the second dialog displaying "it is Yes", if I click "No" I get it displaying "it is No".
You could try the code in a separate button, perhaps something else earlier in the code is affecting the variable.
If you need more assistance you could post your question to Stack Overflow or the LiveCode forum.
http://stackoverflow.com/questions/tagged/livecode
http://forums.livecode.com/
Kind regards
Elanor
Sean Cole
The image for the ask file dialog is incorrect
Elanor Buchanan
Thanks Sean, we have updated the image.
Javier trolle
En la variable it se guarda el patch seleccionado, pero se puede guardar el Tag y otras propiedades?
Elanor Buchanan
Hi Javier
You can't get the tags directly from the ask file dialog but you can get them using a shell command e.g.
put shell("mdls -raw -name kMDItemUserTags test.txt")
This will return something like
(
Red
)
where "Red" is a tag. You can then parse the result to get the tags.
I hope that helps.
Kind regards
Elanor
------------------------------
Hola javier
No puede obtener las etiquetas directamente desde el cuadro de diálogo Preguntar archivo, pero puede obtenerlas utilizando un comando de shell, por ejemplo.
put shell ("mdls -raw -name kMDItemUserTags test.txt")
Esto devolverá algo como
(
Red
)
donde "rojo" es una etiqueta A continuación, puede analizar el resultado para obtener las etiquetas.
Espero que eso ayude.
Saludos cordiales
Elanor
Chrissi
Hi. I have a return separate list which I want to use to generates options for an Answer dialog, i.e.
answer "Which subject?" with line 1 of tSubjects or line 2 of tSubjects
but the return separated list is a variable length. Any ideas?
Elanor Buchanan
Hi Chrissi
You should be able to construct the answer command in a variable and then use the 'do' command to execute it. Something like this should work
put "answer" && quote & "Choose an option" & quote && "with" into tCommand
repeat for each line tOption in tOptions
put space & tOption & space & "and" after tCommand
end repeat
delete the last word of tCommand
do tCommand
I hope that helps.
Kind regards
Elanor