Script:
function getHandler handlerName,scriptText,returnOffsets
-- Extracts a handler from a script.
-- Given the name of a handler and a script, this handler gets
-- the text of the specified handler. Optionally, it will
-- return starting and ending character expressions of the handler.
-- check for function first because on is a substring of function
get offset("function " & handlerName,scriptText)
if it is zero then get offset("on " & handlerName,scriptText)
put it into startChar
put return & "end " & handlerName into footer
put offset(footer,scriptText) into endChar
if endChar is not zero then
add length(footer) to endChar
repeat until char endChar of scriptText is return ┬
or char endChar of scriptText is empty -- skip comment
add 1 to endChar
end repeat
repeat while char endChar + 1 of scriptText is return
add 1 to endChar -- take trailing returns
end repeat
end if
if returnOffsets is true
then return startChar & "," & endChar
else return char startChar to endChar of scriptText
end getHandler
This article is adapted from the Claris Tech Info database.