# Obtaining Component Path on Mouse Click Event

**URL:** <https://forum.inductiveautomation.com/t/obtaining-component-path-on-mouse-click-event/88494>\
**Category:** Ignition\
**Tags:** ignition81, perspective\
**Created:** [April 8, 2024, 6:12am UTC](https://forum.inductiveautomation.com/t/obtaining-component-path-on-mouse-click-event/88494 "2024-04-08T06:12:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ujwal\_Wankhede](https://avatars.discourse-cdn.com/v4/letter/u/4af34b/32.png) [@Ujwal\_Wankhede](https://forum.inductiveautomation.com/u/Ujwal_Wankhede)\
**Post date:** [April 8, 2024, 6:12am UTC](https://forum.inductiveautomation.com/t/obtaining-component-path-on-mouse-click-event/88494/1 "2024-04-08T06:12:35Z")

</div>

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:

```auto
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)

```

 ![Component Path](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/4/3468841a6ea6a1179296bf7be57e0ddd47be613e.png)

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!

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [April 8, 2024, 8:45am UTC](https://forum.inductiveautomation.com/t/obtaining-component-path-on-mouse-click-event/88494/2 "2024-04-08T08:45:55Z")

</div>

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](https://docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-perspective/system-perspective-sendMessage).

---

<div class="post-metadata">

**Author:** ![Ujwal\_Wankhede](https://avatars.discourse-cdn.com/v4/letter/u/4af34b/32.png) [@Ujwal\_Wankhede](https://forum.inductiveautomation.com/u/Ujwal_Wankhede)\
**Post date:** [April 8, 2024, 9:28am UTC](https://forum.inductiveautomation.com/t/obtaining-component-path-on-mouse-click-event/88494/3 "2024-04-08T09:28:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 8, 2024, 12:21pm UTC](https://forum.inductiveautomation.com/t/obtaining-component-path-on-mouse-click-event/88494/4 "2024-04-08T12:21:18Z")

</div>

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

> [@Component/View Path Syntax](https://forum.inductiveautomation.com/t/component-view-path-syntax/80412/2):
>
> Okay, bear with me... Given a stacktrace path of Experimentation/Repl@D$0:2:0:0/root/FlexContainer/Button: You can discern there was an error raised by the Experimentation/Repl View, in the Designer. There's no indication of which View this View is embedded wihtin, but from whichever View it is embedded within, the instance of the View is located along this path: 0:2:0:0. Note that anytime there is still a number remaining in the path that you should be looking for a container. Start at the…

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [April 8, 2024, 12:30pm UTC](https://forum.inductiveautomation.com/t/obtaining-component-path-on-mouse-click-event/88494/5 "2024-04-08T12:30:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ujwal\_Wankhede](https://avatars.discourse-cdn.com/v4/letter/u/4af34b/32.png) [@Ujwal\_Wankhede](https://forum.inductiveautomation.com/u/Ujwal_Wankhede)\
**Post date:** [April 8, 2024, 12:54pm UTC](https://forum.inductiveautomation.com/t/obtaining-component-path-on-mouse-click-event/88494/6 "2024-04-08T12:54:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [April 8, 2024, 1:00pm UTC](https://forum.inductiveautomation.com/t/obtaining-component-path-on-mouse-click-event/88494/7 "2024-04-08T13:00:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ujwal\_Wankhede](https://avatars.discourse-cdn.com/v4/letter/u/4af34b/32.png) [@Ujwal\_Wankhede](https://forum.inductiveautomation.com/u/Ujwal_Wankhede)\
**Post date:** [April 8, 2024, 1:39pm UTC](https://forum.inductiveautomation.com/t/obtaining-component-path-on-mouse-click-event/88494/8 "2024-04-08T13:39:47Z")

</div>

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.
