0

I'm a student currently learning AppleScript and I'm having difficulty looping a particular command, could someone help me out please? The code is as below, my aim is to loop only if the user enters an invalid value.

I appreciate any help given :)

Many thanks Andrew

    display dialog "Choose a number from 1-10" default answer "Only number less than 11" buttons {"Ok"} default button 1

try

    set theAnswer to (text returned of result) as number

on error

    display dialog "You didnt put in any numbers" buttons {"Ok"} default button 1
    return

end try
if theAnswer < 1 then
    set theTest to 0
else if theAnswer < 11 then
    set theTest to 1

else
    set theTest to 0
end if
if theTest = 0 then
    display dialog "Invalid Input" buttons {"Ok"} default button 1

else
    display dialog "You chose the number " & theAnswer buttons {"Ok"} default button 1
end if

1 Answer 1

1

Try:

repeat
    set theAnswer to text returned of (display dialog "Choose a number from 1-10" default answer "Only number less than 11" buttons {"Ok"} default button 1)
    try
        set theAnswer to theAnswer as number

        if theAnswer ≤ 10 and theAnswer ≥ 1 then
            display dialog "You chose the number " & theAnswer buttons {"Ok"} default button 1
            exit repeat
        else
            display dialog "Invalid Input" buttons {"Ok"} default button 1
        end if

    on error
        display dialog "You didnt put in any numbers" buttons {"Ok"} default button 1
    end try
end repeat

beep 3
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.