Obtaining Component Path on Mouse Click Event

I'm running a script triggered by a mouse click event and I'm seeking a more efficient way to obtain the component path within the script. Below is the script I'm using to retrieve the complete component path:

def runAction(self, event):
	def getPath(Component):
		path = Component.name
		if Component.parent is not None:
			path = '%s/%s'%(getPath(Component.parent),path)
		return path
	
	Component_Path = getPath(self)
	system.perspective.print(Component_Path)

While the provided script works fine, I'm wondering if there's a built-in or alternative function available to achieve the same result.
Any insights or suggestions would be greatly appreciated!

I don't know about making the code better, but why are you doing this?
It seems like you are trying to find relative paths between components when you might be using system.perspective.sendMessage | Ignition User Manual.

The objective is to record the component path in a database as part of exception handling process. While manually typing the path in each script is an option, it's both time-consuming and prone to errors.

1 Like

The component path is already encoded in logger keys when a logger is invoked within a component's scripts. See this discussion:

1 Like

To me this implies you are putting all your logic in the runAction extension functions. Is that true?

Yes, that's correct. Is there a particular concern you have regarding this approach?

It is definitely best practices to put all your business logic into project script libraries, and then call those functions from the appropriate spots. This will make error handling much simpler in my opinion.

The fact you need the component source from the call makes it feel like that you may be duplicating code to do the same thing across different components. Having it as a function in a project script library helps keep your code DRY (Don't Repeat Yourself). So you write it once, call it where you need, and do all your error handling in the function. Having the same code in multiple spots is a recipe for disaster when something needs to get changed - now it's on the designer to know all the spots it's called in and then change it in all those locations versus just changing a single function.

You can log the params that were fed in and the result and error via system.util.getLogger(), and if so desired include an argument for the source of the call - perhaps component.name or whatever makes sense for you.

Thank you for sharing your insights!
I appreciate your perspective on centralizing business logic and error handling. Just to clarify, the common functions utilized by the events are indeed placed in the project library or component custom methods.
The getPath function shown in the reference script is located within the project library. Its inclusion was primarily to offer a concise overview of the scripting process within a few screenshots.
Your feedback is valuable.