Re: Run Handler-Name from Text?
Re: Run Handler-Name from Text?
- Subject: Re: Run Handler-Name from Text?
- From: Christopher Nebel <email@hidden>
- Date: Wed, 18 Apr 2012 21:56:26 -0700
While it's not possible to convert a string to handler (though you can invoke one using a string using "run script", as others have explained), it *is* possible to get references to handlers and then invoke them. Consider:
on handler_f()
return "f!"
end handler_f
on handler_g()
return "g!"
end handler_g
on handler_h()
return "h!"
end handler_h
on handler_for(x)
set lookup to {{"f", handler_f}, {"g", handler_g}, {"h", handler_h}} -- notice, no parentheses after the handler name.
repeat with i in lookup
if item 1 of i is x then
return item 2 of i
end if
end repeat
end handler_for
set fp to handler_for("g")
fp() --> "g!"
The syntactic rules for invoking a function this way are somewhat obscure. For instance, the "()" cannot be tied to an arbitrary expression, so 'handler_for("g")()' will not compile. I was also unable to combine the lookup and call parts into a single handler, because it insisted on interpreting "fp" as a handler name, not a variable -- I'm not sure why that is without digging a lot deeper. The technique may be of some use to you, though.
--Chris Nebel
AppleScript Engineering
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden