Re: Coerce String to Handler Name?
Re: Coerce String to Handler Name?
- Subject: Re: Coerce String to Handler Name?
- From: "Arthur J Knapp" <email@hidden>
- Date: Mon, 16 Apr 2001 10:59:27 -0400
>
Subject: Re: Coerce String to Handler Name?
>
Date: Sun, 15 Apr 2001 05:14:38 -0600
>
From: Ehsan Saffari <email@hidden>
>
simple open AE is sent to the applet from another application, then the
>
applet reads the name of an object in the calling application and chooses
>
which handler to invoke and what variables to read from the calling app
>
based on this object name, which is a string.
>
It would be most elegant if this string could be used to invoke the
>
required handler without first defining a huge nested if clause that goes
>
like : If x="a", my a(), else if x="b", my b(), ad nausem. This would
>
also make the calling app simple since calls to all handlers can be made
>
with ONE simple calling routine, not to mention maintenance of both sides
>
of this exchange.
I think that having a "huge nested if clause" scares a lot of scripters
because it seems to be a lot of code, but it actually evaluates very quickly
and is often the best flow-control structure to use.
However, there are alternatives, especially if you don't mind all of your
handler names having the same length:
on Aaa()
display dialog "a"
end Aaa
on Bbb()
display dialog "b"
end Bbb
on Ccc()
display dialog "c"
end Ccc
on Ddd()
display dialog "d"
end Ddd
on Eee()
display dialog "e"
end Eee
property handlerList : {Aaa, Bbb, Ccc, Ddd, Eee}
property handlerParse : ".....Aaa.Bbb.Ccc.Ddd.Eee"
on HandlerCall(str)
set indx to offset of "." & str in handlerParse
if (indx = 0) then
-- handler not found
else
set indx to (indx div 4)
end if
script Temp
property hand : item indx of handlerList
end script
hand() of Temp
end HandlerCall
HandlerCall("Bbb")
P.S. Note how cool it is to define a script object inside of a handler,
this allows one to pass handlers as executable bits of code:
on ConsiderCase(a_handler, the_parameters)
script Temp
property call_handler : a_handler
end script
considering case
call_handler(the_parameters) of Temp
end considering
end ConsiderCase
-- Just as a pointless example:
--
on IsEqual({a, b, isSensitive})
if (isSensitive) then
ConsiderCase(IsEqual, {a, b, false})
else
return (a = b)
end if
end IsEqual
IsEqual({"Aaa", "aaa", false}) --> true
IsEqual({"Aaa", "aaa", true}) --> false
Arthur J. Knapp
http://www.stellarvisions.com
mailto:email@hidden
Hey, check out:
http://www.AppleScriptSourceBook.com