Re: Calling handlers by reference
Re: Calling handlers by reference
- Subject: Re: Calling handlers by reference
- From: email@hidden
- Date: Mon, 21 May 2001 16:04:43 -0400
On 19 May 2001 17:33:29 -0400, Michael Ellis <email@hidden> asked,
>
have been scouring the AppleScript documentation for syntax that would
>
low me to call a handler by reference.
Its possible, but I think the method is a bit kludgy. I prefer to use script
objects, and I'll describe that approach later.
To do it with a handler, the reference you use has to have global scope. (That
is, it must be a global or a property of the script.)
>
I think an example would help
>
illustrate what I'd like to do:
>
>
BEGIN EXAMPLE --
>
--here, add
global helloWorldRef
>
on HelloWorld()
>
log "Hello world!"
>
end HelloWorld
>
>
set helloWorldRef to a reference to HelloWorld
-- just
set helloWorldRef to HelloWorld
>
(helloWorldRef as handler)()
And call it as simply,
helloWorldRef()
>
Basically I am trying to store references to handlers in variables so that
>
I can use them like callback pointers. I think AppleScript can do this,
>
because if you ask AppleScript:
>
>
log class of HelloWorld
>
>
it will report "handler". It seems like I ought to be able to coerce a
>
reference to a handler and then call it.
The problem is that for callbacks, you probably want to keep the reference to
the handler in a list, record, or local variable, and you can't get those to
execute as handlers. So you end up with a global working variable and you copy
the handler you want into the working global and call it from there.
I use script objects instead. They can be told to execute one of their handlers
no matter where they are stored:
script H1
to callback()
return "You called H1 back"
end callback
end script
script H2
to callback()
return "You called H2 back"
end callback
end script
set callbacks to {H1, H2}
tell some item of callbacks to callback()
--> "You called H[12] back"
--
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