Testing the Answer from a dialog box

here is what i have so far… dont tell me to search the bbs besause i spent 2 days trying that.


display dialog "what did you do??" buttons {"Forgot homework", "Shaved the Dog", "Crashed the car"} default button 1
if button returned is 1 then display dialog "What did you forget?" default answer "Example: my Report, or the essay"

If you spent 2 days searching the board for this answer, you need to a) go outside & b) learn to use the search function.

When you display a dialog, it returns a record of the button pressed and the text returned (if any). From that record you can access either the “button returned” or “text returned”, each of which will be a string. In the case of the button, it will be the string you assigned the button that was pressed by the user. All of this is covered in the dictionary for Standard Additions, the Scripting Addition that provides the display dialog command. Go to the library or Barnes & Noble (or some other bookstore of choice), and spend a few minutes with any of the basic AppleScript books (see here for a list) or spend time with the MacScripter FAQ (terrific job, script masters!) – it will do you a world of good.

Until then:

set the_button to button returned of (display dialog "what did you do??" buttons {"Forgot homework", "Shaved the Dog", "Crashed the car"} default button 1)
if the_button = "Forgot homework" then
	display dialog "What did you forget?" default answer "Example: my Report, or the essay"
else if the_button = "Shaved the Dog" then
	display dialog "Why did you shave the dog?" default answer ""
else if the_button = "Crashed the car" then
	display dialog "Why did you crash the car?" default answer ""
end if

Jon

thank you very much. i searched for “custom buttons” in the search BBS thing.

It took you 2 days to search for “custom buttons”?

Jon

with breaks

i want to make a retry handler and i dont know how.


repeat
	set the_button to button returned of (display dialog "what did you do??" buttons {"Forgot homework", "Shaved the Dog", "Crashed the car"})
	if the_button = "Forgot homework" then
		display dialog "What did you forget?" default answer "must start with My"
		set ForgotenHomework to the text returned of the result as text
		display dialog "What is the teachers name?" default answer ""
		set TeacherName to the text returned of the result as text
		set Homework_excuse_list to {"I was waylaied by mutant ninja squirrels on my way to shool, all they wanted was my backpack to store nuts in, and sadly my homework was in it...", "The Simpsons was on and i wanted to build my brain so i watched it instead of doing " & ForgotenHomework & ".", "a Giand Squid escaped from the zoo on wendsday, did you hear about it? anyway it broke into our house and decided that " & ForgotenHomework & " looked tasty...he escaped before i could split him open with a knife and retrive it."}
		set Homework_excuse to item (random number from 1 to (length of Homework_excuse_list)) of Homework_excuse_list
		set openingLineList to {"I am sorry i didn't do", "I should have done"}
		set openingLine to item (random number from 1 to (length of openingLineList)) of openingLineList
		set Butlist to {"but you see", "I couldn't"}
		set But1 to item (random number from 1 to (length of Butlist)) of Butlist
		display dialog "" & openingLine & " " & ForgotenHomework & ", " & But1 & " " & TeacherName & ", it wasn't my fault, " & Homework_excuse & "." buttons {"Again", "Done"} default button 1
		
	else if the_button = "Shaved the Dog" then
		display dialog "Why did you shave the dog?" default answer ""
	else if the_button = "crash the car" then
		display dialog "why did you crash tha car?" default answer ""
	end if
end repeat

You put your code into a repeat loop with no conditional. Inside the loop, test something, and if it passes the test, then exit the loop:

set the_answer to "my "
set the_error to ""
set the_icon to 1
repeat
	if the_error is not "" then
		set the_error to the_error & return & return
		set the_icon to 2
	end if
	set the_answer to text returned of (display dialog (the_error & "What did you forget? (Must begin with 'My')") default answer the_answer buttons {"OK"} default button 1 with icon the_icon)
	if the_answer does not start with "my " then
		set the_error to "Your answer, " & the_answer & " does not start with 'my'. Please try again."
	else if the_answer = "my " then
		set the_error to "That was not an acceptible answer. Please try again."
	else
		exit repeat
	end if
end repeat
return the_answer

Jon

i used this before you posted
but what i did works find but i can change the name if the buttons or it downt work
and ok and cancel dont make any sence


start()

on start()
	set the_button to button returned of (display dialog "what did you do??" buttons {"Forgot homework", "Shaved the Dog", "Crashed the car"})
	if the_button = "Forgot homework" then
		display dialog "What did you forget?" default answer "must start with My"
		set ForgotenHomework to the text returned of the result as text
		display dialog "What is the teachers name?" default answer ""
		set TeacherName to the text returned of the result as text
		set Homework_excuse_list to {"I was waylaied by mutant ninja squirrels on my way to shool, all they wanted was my backpack to store nuts in, and sadly my homework was in it...", "The Simpsons was on and i wanted to build my brain so i watched it instead of doing " & ForgotenHomework & ".", "a Giand Squid escaped from the zoo on wendsday, did you hear about it? anyway it broke into our house and decided that " & ForgotenHomework & " looked tasty...he escaped before i could split him open with a knife and retrive it."}
		set Homework_excuse to item (random number from 1 to (length of Homework_excuse_list)) of Homework_excuse_list
		set openingLineList to {"I am sorry i didn't do", "I should have done"}
		set openingLine to item (random number from 1 to (length of openingLineList)) of openingLineList
		set Butlist to {"but you see", "I couldn't"}
		set But1 to item (random number from 1 to (length of Butlist)) of Butlist
		display dialog "" & openingLine & " " & ForgotenHomework & ", " & But1 & " " & TeacherName & ", it wasn't my fault, " & Homework_excuse & "." buttons {"that was great"} default button 1
		restarttest()
	else if the_button = "Shaved the Dog" then
		display dialog "what is your dogs name?" default answer ""
		set DogName to the text returned of the result as text
	else if the_button = "crash the car" then
		display dialog "why did you crash tha car?" default answer ""
	end if
end start
--home of the retry handler
on restarttest()
	display dialog "want to try again?" 
	start()
end restarttest

although what you said i tryed before posting and couldnt figure out…
oh yeah thanks for being almost the only person to ever answer my posts… :smiley: :smiley: :smiley:

i did the thing you said and now i have another problem the shaving dopg and car one dont work. i know its because the if then statments are indented too far but i cant get them out

repeat
	set the_button to button returned of (display dialog "what did you do??" buttons {"Forgot homework", "Shaved the Dog", "Crashed the car"})
	if the_button = "Forgot homework" then
		display dialog "What did you forget?" default answer "must start with My"
		set ForgotenHomework to the text returned of the result as text
		display dialog "What is the teachers name?" default answer ""
		set TeacherName to the text returned of the result as text
		set Homework_excuse_list to {"I was waylaied by mutant ninja squirrels on my way to shool, all they wanted was my backpack to store nuts in, and sadly my homework was in it...", "The Simpsons was on and i wanted to build my brain so i watched it instead of doing " & ForgotenHomework & ".", "a Giand Squid escaped from the zoo on wendsday, did you hear about it? anyway it broke into our house and decided that " & ForgotenHomework & " looked tasty...he escaped before i could split him open with a knife and retrive it."}
		set Homework_excuse to item (random number from 1 to (length of Homework_excuse_list)) of Homework_excuse_list
		set openingLineList to {"I am sorry i didn't do", "I should have done"}
		set openingLine to item (random number from 1 to (length of openingLineList)) of openingLineList
		set Butlist to {"but you see", "I couldn't"}
		set But1 to item (random number from 1 to (length of Butlist)) of Butlist
		set Button2 to button returned of (display dialog "" & openingLine & " " & ForgotenHomework & ", " & But1 & " " & TeacherName & ", it wasn't my fault, " & Homework_excuse & "." buttons {"Done", "Again"} default button 2)
		if Button2 = "Done" then
			exit repeat
			if Button2 = "Again" then
			else if the_button = "Shaved the Dog" then
				display dialog "what is your dogs name?" default answer ""
				set DogName to the text returned of the result as text
				set doggy_excuse to {"i had too shave " & DogName & ", he was chasing the neibors cat, it was the only way to stop him..."}
				set Button3 to button returned of (display dialog "" & doggy_excuse & "" buttons {"Done", "Again"} default button 2)
				if Button3 = "Done" then exit repeat
			else if Button3 = "Again" then
			else if the_button = "crash the car" then
				display dialog "why did you crash the car?" default answer ""
			end if
		end if
	end if
end repeat

First… Your “crash the car” doesn’t work because the text you check for and the button value do not match.

Second… You need to balance out your END IF statements correctly. After you check for the Values of “Done” and “Again” (and there are no other button2 or button3 values to check for, end the IFs.

That should get your indenting back working. You may need additional END IF statements if you don’t have the “Again” button checks actually do anything. You’ll see what I mean when you compile your script. Remember that all of your associated END, ELSE IF, ELSE and END IF statements that are associated should be at the same indent level.

it all works now thanks