Beginner: Repeat with while loop and errors
Okay, so not sure if apple.stackexchange is a better place for this, but I need some help with this code:
Objective: Ask user to开发者_如何学运维 enter amount of times the loop should be repeated. Send feedback if they type it in the wrong format
Problem If I type in an decimal, it just takes it as an integer and still works, how can I prevent this, or check it another way?
set correctEntry to false --initially assume false
repeat while correctEntry is false
--Let user put how many times it loops
set textToDisplay to "How often should this repeat?"
display dialog textToDisplay default answer "2.4"
set reps to text returned of the result
--Detailed check/feedback if they input wrong
try
--Begins as string, try making it an integer
set reps to reps as integer --coercion will should only work with integer
set correctEntry to true --remember won't get here if first statement fails
on error
try
--See if string can at least be converted to a number
set reps to reps as number
display dialog "Only integers"
on error
display dialog "That wasn't a number"
end try
-- still false here
end try
end repeat
--Only gets here if correctEntry becomes true
repeat reps times
say "You entered right"
end repeat
Also, to check if a negative number was entered, I would just use
if reps > 0
set reps to reps as integer
set correctEntry to true
else
display dialog "Must be positive"
Is there a better way? Even a built in "positive" call?
Finally, I am using http://www.fischer-bayern.de/as/as4as/AS4AS_e.pdf as tutorial, however I'm not sure if it is good enough. I was thinking of going to Mac OS X Developer Library, but would like some input on a good way to learn. I have just a bit of Perl experience, so it doesn't have to be for a complete beginner.
Update: Found this site: http://www.macosxautomation.com/training/applescript/intro.html
Thanks a lot for any help you can provide.
Update2: The code works if I use the same check for an integer in the second try block, what's wrong with the first. Why does it count everything as an integer? More importantly, why does using the if statement suggested not work if put in my script?
Try this...
repeat
set textToDisplay to "How often should this repeat?"
set reps to text returned of (display dialog textToDisplay default answer "2.4")
try
set reps to reps as number -- this makes sure no letters were entered
if (class of reps) is integer and reps is greater than 0 then
exit repeat
else
error "Only positive integers"
end if
on error theError number errorNumber
display dialog "There was an error: " & (errorNumber as text) & return & return & theError buttons {"OK"} default button 1 with icon stop
end try
end repeat
repeat reps times
say "You entered right"
end repeat
For learning here's some tutorials. When I learned I did the ones in the "Tutorials for Beginning Scripters" section. There's more advanced tutorials as you get better too. Last, you have to also study the "Applescript Language Guide" which is under the Help menu in AppleScript Editor.
精彩评论