Re: Simple if .. else .. then statement
Re: Simple if .. else .. then statement
- Subject: Re: Simple if .. else .. then statement
- From: Paul Berkowitz <email@hidden>
- Date: Sat, 29 May 2004 09:29:18 -0700
On 5/29/04 8:11 AM, "Rick Davis" <email@hidden> wrote:
>
I think I need to step away from the computer. What should be a simple
>
thing is stumping me. How do I tell my script to look at a list of
>
items and perform a task if a condition is met and skip to the next
>
item in the list if the condition is not met?
What's the condition? Where you may be going wrong is in the syntax for
checking whether something is in a list. You need to provide some sample
code, not ask a question as general as that. The direct answer to your quest
is;
if [some condition is met} then
--do something
[else if some other condition is met then
do something else]
-- [more 'else ifs']
[else
do another thing]
end if
Or maybe you're talking about a _repeat loop_ through the list? Why didn't
you say so? ('look at a list' doesn't quite get that across, you know.) Some
people advise that you should always do this:
repeat with i from 1 to (count aList)
set anItem to item i of aList
if someCondition = thisValue -- or some other condition
-- do a ton of stuff, maybe call other handlers too, etc.
-- blah blah
end if
end repeat
If it doesn't meet the condition, it doesn't pass through the 'if' block, so
it goes straight on to the next iteration of the repeat loop.
There are some times when things get very complicated and you are dealing
with nested if blocks and other blocks, like this:
repeat with i from 1 to (count aList)
set anItem to item i of aList
if condition1 = "a" then
--do complicated stuff
if condition2 = "b" then
-- !! get me out of here! and on to next iteration
else
-- more stuff
end if
else if condition1 = "b" then
if condition 3 = "c" then
--other stuff
else
-- !! get me out of here! and on to next iteration
end if
end if
--yet more stuff to do under all conditions except "!get me out of here"
end repeat
If you hit any of those "!get me out of here", you don't want to do the 'yet
more' stuff at the end, right? Well, there are ways of constructing the
blocks better, especially if you use calls to handlers. You can just call
the handlers in under "good" conditions and omit the calls under "bad
conditions", instead of putting 'yet more stuff' at the end. Still, there
really are some times when you just need to get out of there.
Here's a great technique devised by Ray Robertson to make up for the fact
that AppleScript does not have a 'Next Repeat' syntax:
repeat with i from 1 to (count aList)
set anItem to item i of aList
repeat 1 times
if condition1 = "a" then
--do complicated stuff
if condition2 = "b" the
exit repeat -- 'repeat 1 times' block, on to the next item
else
-- more stuff
end if
else if condition1 = "b" then
if condition 3 = "c" then
--more stuff
else
exit repeat -- 'repeat 1 times' block, on to the next item
end if
end if
--yet more stuff to do under all conditions
end repeat -- 1 times
end repeat
(You can put 'repeat 1 times' on the line immediately below the first
'repeat' if you wish, makes no difference.)
Of course, if you also need a "real" exit repeat for the outer block, you
need to add:
set exitOuterRepeat to false
repeat with i from 1 to (count aList)
set anItem to item i of aList
repeat 1 times
--blah blah as above
if someCondition = "x" then
set exitOuterRepeat to true
exit repeat -- 1 times
end if
--etc, etc.
end repeat -- 1 times
if exitOuterRepeat then exit repeat -- outer
--yet more stuff to do otherwise
end repeat
--
Paul Berkowitz
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.