Re: Handlers in list
Re: Handlers in list
- Subject: Re: Handlers in list
- From: email@hidden
- Date: Mon, 23 Apr 2001 23:23:40 -0400
On Mon, 23 Apr 2001 11:38:10 +0200, Brennan Young <email@hidden> asked,
>
"Neal A. Crocker" <email@hidden> wrote
>
>
> Instead, do this:
>
>
I've been trying to understand this. It looks really useful but I don't
>
quite get it. I've boiled your example down to this
>
>
on dummyHandler()
>
display dialog "this never happens"
>
end dummyHandler
>
>
set myhandlers to {testA, testB, testC}
>
set r to random number from 1 to 2
>
set dummyHandler to item r of myhandlers -- ***
>
dummyHandler()
>
>
on testA()
>
display dialog "testA"
>
dummyHandler
I don't think you want the reference to dummyHandler above.
>
So the bit I'm a bit confused about is the line marked ***.
>
You are following very well. A handler without arguments is a thing that can be
returned as a value and assigned to a variable. But that variable can be called
as a handler with arguments only under certain circumstances (if its a global
variable, or if it was previously defined as a handler already, both of which
give it proper scope.)
>
Where is all this documented? I can't find anything about it in
>
Applescript Language Guide
It really seems like something that is just a fluke of the implementation, and
may go away the way "item 0 of someList" did something weird and then went away.
The behavior of handler names was determined by experiment, not by
documentation.
The safe way, I think, to do this, is to pass a script object and not a nekkid
handler. Script objects are legitimate, ASLG-approved things to assign to
variables, and calling their handlers is clear and straightforward.
script case1
on crevitate()
display dialog "Do whatever is needed"
end process
end script
script case2
on crevitate()
display dialog "Do something else needed"
end process
end script
script case3
on crevitate()
display dialog "Ditto."
end process
end script
set choices to {case1, case2, case3}
tell (some item of choices) to crevitate()
It keeps the conceptual distinction between assigning an object to a variable
and executing a handler clear. Objects are nouns, and its natural put them into
variables. Handlers are verbs, and its unnatural to see them as something to
store, but natural to see them as doing something when called. Its also natural
to see an object (a noun) as something that can be activated in some way,
causing it to do one of its actions (verbs, or handlers).
--
Scott Norton Phone: +1-703-299-1656
DTI Associates, Inc. Fax: +1-703-706-0476
2920 South Glebe Road Internet: email@hidden
Arlington, VA 22206-2768 or email@hidden