Hello.
This is something I will use in another context, so this only shows the concept.
And I didn’t figure it out by my self, I was in need, if not in dire, and looked it up in Matt Neuburgs “AppleScript The Definitive Guide” I guess this method is also described elsewhere. -It is nice to have when you need it!
The Example:
Say you have a handler that you wan’t to share with others, but you aren’t exactly aware of how the user wants your routine to perform in some part, say the error handler.
Then you can pass a handler like this, and the handler can also take parameters, You will even be given the return value of the script object through the result.
bigHandler(smallHandler, 5)
bigHandler(missing value, 5)
on bigHandler(theOther, andAParameter)
script suppliedHandler
property thehandler : theOther
property theParaMeter : andAParameter
thehandler(theParaMeter)
end script
if theOther is not missing value then
run suppliedHandler
set theRes to the result
display dialog "And the result was : " & theRes
else
display dialog "I miss the other with " & andAParameter
end if
end bigHandler
on smallHandler(theParaMeter)
display dialog "I'm the other And the parameter is " & theParaMeter
return false
end smallHandler
Afterthoughts: While this is the “clean” way to do it, the most flexible way is to use a global with some name
that is unlikely to clash with some variable name the user will create. -AppleScript usually doesn’t contain 100 of thousands of lines of code, where such clashes are much more likely to occur!
Here is an example of the more flexible solution to the same problem by using a global, but now we can actually send with one of the local variables of the bigHandler to the smallHandler.
bigHandler(smallHandler, 5)
bigHandler(missing value, 5)
on bigHandler(theOther, andAParameter)
global _bighHandlersHandler
local aLocal
set aLocal to "5 too!"
if theOther is not missing value then
set _bighHandlersHandler to theOther
_bighHandlersHandler(andAParameter, aLocal)
else
display dialog "I miss the other " & aLocal
end if
end bigHandler
on smallHandler(theParaMeter, otherParameter)
display dialog "I'm the other And the parameter is " & theParaMeter & " " & otherParameter
end smallHandler
Best Regards
McUsr