display dialog "Are you a noob?" default answer "" set noob_answer to text returned of result display dialog noob_answer
- The first line of the script displays the text Are you a noob? and lets you type in an answer. Without the default answer "" part of the code it will not let you type in an answer.
- In the second line noob_answer is our variable and we set our variable to the text returned of result. The result being what we put in the text and clicked ok and setting that result as text.
- The last line will display whatever the variable noob_answer is set to, in this case we set the result of the question to the variable so it will display whatever you type.
Script
Type in whatever you want and click ok
Result
The next option we have is to have a list of options for a user to choose from. To do this we use this script:
(choose from list {"Noob", "Average", "Expert"} ¬ with prompt "What is your programming skill?") set list_answer to result as text display dialog list_answer
- The first line of code tells Script Editor that it wants the user to choose from a list with options Noob Average and Expert. The symbol ¬ tells the Script Editor to read that line and the line below it as one line. It is useful to make your scripts look cleaner. To make the symbol use Option + l <- Lowercase L
- The second line has the with prompt part which displays the description of what the user is picking.
- The third line sets the variable list_answer to the result choosen from the list and sets it as text.
- The last line displays the variable list_answer, which is set to the result choosen from the list.
Script
Choose from the list
Result if Average choosen
The third option we are going to use to get information from a user is making a user choose between buttons. The limitation for buttons is that there can only be 3 buttons. The script for this is:
display dialog "What food do you like best?" ¬ buttons {"Waffles", "Pizza", "Ice Cream"} set best_food to button returned of result say best_food
- The first like tells us to display the text "What food do you like best?" again we use the ¬ symbol to tell the Script Editor that line 1 and line 2 should be read as one line. We don't need to use that symbol if we put them in one line like this:
display dialog "What food do you like best?" buttons {"Waffles", "Pizza", "Ice Cream"}but in the editor it looks better the with the ¬ symbol and is easier to edit.
Choose a button:
Result: Your computer says what button you choose! Now that you know three ways you can get information from a user we can now learn how to tell applications do to things.
How do you make the answer = something?
ReplyDeleteYou mean see if an answer is the same as some other value? Try this:
Deletedisplay dialog "How's it going?" buttons {"Great!", "Not that great..."}
set answer to button returned of result
if answer is equal to "Great!" then
display dialog "That's great!" buttons {"Done"}
else
display dialog "That's not great..." buttons {"Done"}
end if
You don't have to use "is equal to".
DeleteJust use:
if answer is "Great!" then
...
end if
Anyway to get a user to specify a path to a folder in finder?
ReplyDeleteI get this far
display dialog "Choose your folder " buttons {"Select"}
if result = {button returned:"Select"} then
tell application "Finder" to activate
but can't work out how to wait until a user actually selects a folder and chooses "OK" or something like that.. well I could just shove up another dialog box but that seems clumsy..
choose file with prompt "choose your folder."
Delete