Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Help with repeating in Applescript

Hi, I'm very new to Applescript(about 2 or 3 days). I've been learning the basic things for Applescript, so I'm trying to make a quiz-like script with 5 questions and answers using a display dialog. When the answer is correct, it goes to the next question. However, if it is wrong, I display a different dialog that says "Wrong!" and I give them two buttons to Press: "Close" or "Restart". I execute error number -128 when the user presses "Close".



But my problem is with the "Restart Button"


The way I want it is when the user presses Restart once they get the answer wrong on any question, I want the whole script to restart and back to the 1st question. I've tried using repeat/end repeat method, but it's not seeming to work. I try to set it to repeat variable Question_1 one time, but it's not working. I need help!


Below is the script for the first Question:


set Question_1 to display dialogQuestionOnedefault answer "" buttons {"OK"} default button 1

if text returned of Question_1 is answer_3 or text returned of Question_1 is answer_4 then


display dialog "Correct!" buttons {"Next"} default button 1

with transaction

end transaction

else


beep

set incorrect to display dialog "Wrong!" buttonsRestartOrClosedefault button 2

if button returned of incorrect is Closing then

error number -128

end if

if button returned of incorrect is Restarting then

repeat 1 times


end repeat

end if

end if

iPhone 4, iOS 5.1.1

Posted on Apr 22, 2013 4:00 PM

Reply
7 replies

Apr 22, 2013 5:29 PM in response to joseph0819

This should work:


set qs to {"Who is the President of United States?", "Who was the President of United States before Barack?", "Now, who then was the President of United States before Bush?"}

set asw to {"Barack Obama", "George Bush", "Bill Clinton"}


repeat with i from 1 to 3


display dialog (qs's itemi) default answer ""

set propAnswer to text returned of the result

if propAnswer is equal to asw's item i then

display dialog "Well done!"

else


display dialog (qs's itemi) default answer ""


end if

end repeat

Apr 22, 2013 5:50 PM in response to joseph0819

Sorry, I fixed it up more.


set qs to {"Who is the President of United States?", "Who was the President of United States before Barack?", "Now, who then was the President of United States before Bush?"}

set asw to {"Barack Obama", "George Bush", "Bill Clinton"}


repeat with i from 1 to 3


display dialog (qs's itemi) default answer ""

set propAnswer to text returned of the result

if propAnswer is equal to asw's item i then

display dialog "Well done!"

else

display dialog ("Wrong! Try again.. " & qs's item i) default answer ""

set propAnswer to text returned of the result

if propAnswer is equal to asw's item i then

display dialog "Well done!"

else

display dialog ("Too many tries the answer is… " & asw's item i) default answer ""

end if

end if

end repeat

Apr 22, 2013 6:30 PM in response to joseph0819

One way would be to have an outer repeat loop where variables are (re)set (sores, etc), and then an inner repeat loop that steps through the questions. To reset, you would just exit the inner repeat loop.


pjdube has the right idea to use a list (or multiple lists) to contain your questions and answers, otherwise doing them separately makes for a large, confusing script. For things like this I like to use records to keep things organized (it also allows for multiple answers).


The following script gives the option to continue or restart, and keeps track of the number of correct answers. Also note that a button named "Cancel", or a button name that you specify to be the cancel button, will generate your error number -128 (user canceled):

# QandA is a list of records in the form {question:"the question", answers:{"list", "of", "answers"}} property QandA : {¬      {question:"What is your favorite color?", answers:{"blue", "red"}}, ¬      {question:"What is your quest?", answers:{"to seek the holy grail"}}, ¬      {question:"What is the average airspeed velocity of an unladen sparrow?", answers:{"24mph", "24"}}} global correct on run      repeat -- forever (or at least until canceled)           set correct to 0 -- reset score for this round           repeat with anItem in QandA                if not doQuery(anItem) then exit repeat -- continue until doQuery returns false or items completed           end repeat           display dialog "" & correct & " correct out of " & (count QandA) with title "Score" buttons {"Quit", "OK"} cancel button "Quit"      end repeat end run to doQuery(theQuery) -- returns false to restart, true otherwise      set theAnswer to text returned of (display dialog theQuery's question default answer "" with title "Query" buttons {"Quit", "OK"} cancel button "Quit" default button 2)      if theQuery's answers contains theAnswer then           set correct to correct + 1           display dialog "Correct!" buttons {"OK"} default button 1 giving up after 1      else           display dialog "Sorry, but that is incorrect" buttons {"Quit", "Restart", "Continue"} cancel button "Quit"           if button returned of the result is "Restart" then return false      end if      return true end doQuery

Apr 22, 2013 7:54 PM in response to pjdube

That form of the repeat statement is similar to what you are doing with repeat with i from 1 to 3, the difference being that it steps through the items in a list instead. In the repeat statement you are using, the loop variable i is set to the next number in the range specified. Since you are just using i as an index into a list anyway, the repeat statement I'm using is a little bit handier in that the loop variable (anItem in my script) is set to the next item in the specified list.


For your first question, I'm pretty much self-taught - I don't use computers in my job (although those things are everywhere), so it is more or less a hobby. I started out with a couple of simple projects way back when and continued by watching scripting forums, playing with the various questions and answers. Forums such as these have been very educational - I still learn a lot from them, since there are quite a few participants who know their stuff.

Help with repeating in Applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.