Hi,
I wonder if there is introspection/reflection in AppleScript. I have Googled around, but I haven’t found anything.
What I want to do is to call a handler from a string, such that if the string contains “doThis” the handler with that name would be called. In Java this is called “reflection”, in other object-oriented languages this is sometimes called “introspection”.
An extension of this would be to query a script object for all its handlers, so that you get a list of all handler names. And then to get hold of a specific script object by name.
Anyone?
/P.
There’s no built-in system for this (unlike some, AS doesn’t allow much creative poking around in its internals). Basically, you gotta fake it:
on foo()
beep 2
end foo
on bar()
display dialog "Hello world."
end bar
on do(handlerName, theScript)
run script "
script
on do(theScript)
tell theScript to " & handlerName & "()
end do
end script"
tell result to do(theScript)
end do
--
do("foo", me)
do("bar", me)
The penalty you have here is that ‘run script’ is slow as it has to load and unload the AS compiler each time it’s called. It’s faster if you run in an application that has a ‘do script’ function (e.g. Smile) and use that instead. Or you might reduce the overhead in ‘run script’ by using a single ‘run script’ to compile and return many such objects in one go.
HTH
has