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: André Berg <email@hidden>
- Date: Thu, 23 Oct 2008 21:55:50 +0200
Hi has,
Thank you for joining :)
You seem to have a lot of insight into the whole implementation details
of AppleScript.
If I may ask, is there any grounds to my observation earlier, that
passing an escaped string containing
AppleScript commands to "run script" takes significantly longer for
processing them than passing those
same AppleScript commands wrapped in a script object?
As you have noted the way you have proclaimed seems to be the best and
is actually
very very close to what I had in mind when I (mis)formulated my
question. It's working well.
Thanks.
André
--- Original Nachricht ---
Absender: has
Datum: 23.10.2008 21:05 Uhr
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
_______________________________________________
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