name of me returns the name of the script file.
But I don’t see a way to get the handler’s name.
Any ideas?
set scriptFileName to name of me
--> "My Test Script"
### HOW DO I GET THE NAME OF THE EXECUTING HANDLER? ###
on myHandler()
--- THIS DOESN'T WORK ---
set handlerName to name of handler
end myHandler
Handler names cannot be retrieved at run time like in many programming languages. However some programming languages have compiler macro’s like FUNCTION which will be replaced by a string at compile time. Also programming languages who are text interpreted can get the current handler/function name. But with byte code interpreters, like AppleScript, the handler name doesn’t need to be the same at runtime, and most of the time it isn’t.
Yes. The nearest you can get is to write code into the handler itself which provides the name (more properly the ‘label’) in text form:
on myHandler()
set handlerName to "myHandler"
log handlerName
try
century of (current date)
on error errMsg number errNUm
showError(handlerName, errMsg, errNUm)
end try
end myHandler
on showError(handlerName, errMsg, errNUm)
display alert "Error in '" & handlerName & "' handler:" message errMsg
end showError
myHandler()