Re: How to simulate C's function pointer in AppleScript?
Re: How to simulate C's function pointer in AppleScript?
- Subject: Re: How to simulate C's function pointer in AppleScript?
- From: has <email@hidden>
- Date: Thu, 23 Oct 2008 20:05:24 +0100
Philip Aker wrote:
On Oct 23, 2008, at 3:58 AM, André Berg wrote:
The question now is how can I correctly pass, from the top-level
script, myHandlerToTime() in the statement property of the Timer
script so that it gets used as the direct parameter to the run
script command inside the Timer script?
on doHandlerToTime()
display dialog "beep"
end doHandlerToTime
script Timer
property procedure : missing value
on timeit()
run script procedure
end timeit
end script
set Timer's procedure to doHandlerToTime
Timer's timeit()
This is a bad idea. AppleScript handlers aren't designed to be passed
around like this: it isn't documented and the language doesn't
properly support it. Moving handlers from one context to another will
screw up any static, non-local variable bindings that they have,
resulting in runtime bugs and errors. The fact that you can pass
handler objects around like this in the first place is only due to the
AppleScript interpreter's sloppy implementation (it really ought to
prevent get/set operations on script object slots containing handlers).
Anyway, to get back to the OP's question: you can't simulate C
function pointers in AppleScript. AppleScript uses Smalltalk-style
message passing semantics (also used by languages such as ObjC, Ruby
and F-script), where you send a message to the object and it's up to
the object to invoke the appropriate handler in response. Therefore,
you need to wrap your handler in a script object and pass that around
instead. Example:
script Timer
property actionObj : missing value
on timeit()
actionObj's doAction()
end timeit
end script
script MyAction
on doAction()
-- do something
end doAction
end script
set Timer's actionObj to MyAction
Timer's timeit()
HTH
has
--
Control AppleScriptable applications from Python, Ruby and ObjC:
http://appscript.sourceforge.net
_______________________________________________
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