Re: Handlers in list
Re: Handlers in list
- Subject: Re: Handlers in list
- From: Paul Berkowitz <email@hidden>
- Date: Sat, 21 Apr 2001 11:06:14 -0700
On 4/21/01 10:14 AM, "Stephen Swift (aka Burnum)" <email@hidden> wrote:
>
Maybe handlers were never meant to go in a list, or maybe there is something
>
I just can't see.
>
>
My goal: There are four mini-scripts that my script could perform based on
>
my input in the main-script. To break up the script, I put each mini-script
>
in a handler. To choose the handler to run I used a repeat statement. The
>
repeat statement works fine when I use a list like {1,2,3,4} but when I have
>
a list of handlers, all the handlers activate. I suppose I could use if
>
then statements if all else fails. Any Ideas? Thanks.
>
>
Code:
>
set DlgResult to {null,null,null,null,null,null,false,true,false,false}
>
set someList to {handler1(), handler2(), handler3(), handler4()}
>
set Num to 1
>
repeat with i from 7 to 10
>
if item i of DlgResult = true then
>
set RunHand to item Num of someList
>
end if
>
set Num to Num + 1
>
end repeat
>
RunHand
The way to call a handler, if you're not in an application tell block, is
simply by
handler1()
You can even say
set x to handler1()
which will run the handler and set x to the 'return' result, if there is one
or will just run the handler anyway. if you then say
x
afterwards, it will run the handler a second time, since x was defined as
the handler. So, indeed, saying
set someList to {handler1(), handler2(), handler3(), handler4()}
will run the four handlers, one after the other. That's exactly what you're
telling it to do.
Then you set RunHand to (run) whichever handler corresponds to 'true' in a
particular position in the DigResult list. (In this particular case, that's
only once, but it could be 3 times, for example.) So those selected handlers
run again. And then, after the repeat loop is finished, you call the final
'RunHand' (whichever handler's position was the last to be evaluated to
'true') to run yet again.
So with one 'true', as above, you'll have got all four handlers running,
then the one you want runs two more times. If 2 of them evaluated to 'true',
you'd get all 4, then the two 'true' ones again, and then the last one yet
again!
I think what you want is this:
set DlgResult to {null, null, null, null, null, null, false, true, false,
false}
set Num to 1
repeat with i from 7 to 10
if item i of DlgResult = true then
RunHand(Num)
end if
set Num to Num + 1
end repeat
on RunHand(x)
if x = 1 then
handler1() -- or do whatever handler1 did
else if x = 2 then
handler2() -- or do whatever handler2 did
else if x = 3 then
handler3() -- or do whatever handler3 did
else if x = 4 then
handler4() -- or do whatever handler4 did
end if
end RunHand
-------------
Or, what comes to the same thing in fewer lines:
set DlgResult to {null, null, null, null, null, null, false, true, false,
false}
set Num to 1
repeat with i from 7 to 10
if item i of DlgResult = true then
if Num = 1 then
handler1() -- or do whatever handler1 did
else if Num = 2 then
handler2() -- or do whatever handler2 did
else if Num = 3 then
handler3() -- or do whatever handler3 did
else if Num = 4 then
handler4() -- or do whatever handler4 did
end if
end if
set Num to Num + 1
end repeat
------------------------
--
Paul Berkowitz
References: | |
| >Handlers in list (From: "Stephen Swift (aka Burnum)" <email@hidden>) |