Can I Get the Name of the Running Handler?

How can I get the name of the running handler?

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

Handlers are variables for an action and have no native properties.

Can’t you just set handlerName to 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.

@Marc and @DJ, thanks for confirming that you can’t get the name of the handler in AppleScript.

Is the negative answer on this question still true?

Hi @lagr.

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()
1 Like

Is there anyway to use the

PRETTY_FUNCTION
Variable that is available in Objective-C?
I use it all the time in my ObJ-C logging