# Component Scripting for Template

**URL:** <https://forum.inductiveautomation.com/t/component-scripting-for-template/53344>\
**Category:** Ignition\
**Created:** [November 22, 2021, 3:03pm UTC](https://forum.inductiveautomation.com/t/component-scripting-for-template/53344 "2021-11-22T15:03:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aybars.Urut](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/aybars.urut/32/32003_2.png) [@Aybars.Urut](https://forum.inductiveautomation.com/u/Aybars.Urut)\
**Post date:** [November 22, 2021, 3:03pm UTC](https://forum.inductiveautomation.com/t/component-scripting-for-template/53344/1 "2021-11-22T15:03:16Z")

</div>

Hi there,  
I’m using Ignition 8.1. I want to write a simple code for mouseClicked inside a template that I move to the screen. Here is the image of my simple problem;  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/2/e2960dbab18a2ff6c0d7b125627cb4193a14ca3f.png)

I want to change the visibility of another image on the screen. I don’t get any error in this code but it doesn’t even work at all. I simply typed print “test” in it, but I can’t see it in the console either.

By the way, I can easily use this code in a button.

---

<div class="post-metadata">

**Author:** ![JoeF](https://avatars.discourse-cdn.com/v4/letter/j/4da419/32.png) [@JoeF](https://forum.inductiveautomation.com/u/JoeF)\
**Post date:** [November 22, 2021, 3:48pm UTC](https://forum.inductiveautomation.com/t/component-scripting-for-template/53344/2 "2021-11-22T15:48:13Z")

</div>

Looking at some old posts, adding the script to the template instance might not be available (?), but there are workarounds. Seems like you cannot get the event to fire at all.

One thing that I have used lately has been putting the script on the Template itself (not the template instance) that calls this potential GUI scenario and changes a component. This isn't a good code block, but it works:

```auto
currentWindow = 'Window_Path_That_Has_The_Image'
try:
	image = system.gui.getWindow(currentWindow).getRootContainer().getComponent('Image')
	image.visible = 0
except:
	pass

```

Previous posts:

> [@Mouse click on template canvas](https://forum.inductiveautomation.com/t/mouse-click-on-template-canvas/23017):
>
> Is there any work-around for lack of a mouse click event on the template canvas component?

> [@Template Event Handlers - Events in template and template instance](https://forum.inductiveautomation.com/t/template-event-handlers-events-in-template-and-template-instance/20831):
>
> I have a template created with a component that has mouseEntered and mouseExited scripts to produce a hover over effect. However I also want to be able to perform any action the developer wishes on the instance so I’ve left the template’s mouseClicked action empty. To test, I dragged on a template into a Window and wrote a simple script to write to the console into the mouseClicked event handler, but this event never fires. Is it possible to do this? Travis’ post below suggests perhaps not… b…

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 22, 2021, 5:22pm UTC](https://forum.inductiveautomation.com/t/component-scripting-for-template/53344/3 "2021-11-22T17:22:27Z")

</div>

Do you have any overlaying components, or a mouse event handler on the template _instance_ in your actual window?

Something might be intercepting the mouse event before it gets to your template’s inner handling code. Consider a `print "event fired"` to ensure it’s actually being executed.

---

<div class="post-metadata">

**Author:** ![Aybars.Urut](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/aybars.urut/32/32003_2.png) [@Aybars.Urut](https://forum.inductiveautomation.com/u/Aybars.Urut)\
**Post date:** [November 23, 2021, 5:54am UTC](https://forum.inductiveautomation.com/t/component-scripting-for-template/53344/4 "2021-11-23T05:54:55Z")

</div>

Surprisingly, it’s really sad that we can’t use mouseClicked directly. As mentioned in other posts, I think the best way is to make another component invisible and write on it. Thank you guys!

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [November 23, 2021, 7:11am UTC](https://forum.inductiveautomation.com/t/component-scripting-for-template/53344/5 "2021-11-23T07:11:55Z")

</div>

You can definitely use mouseClicked directly, Ive done it lots before. Where are you attaching your event script to?

If you have a template that has mouse events on components within it, then attach mouse events to the Template instance itself, mouse events won’t get passed through and your events won’t run that are defined in the template without code to pass it through

---

<div class="post-metadata">

**Author:** ![KEHT](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/keht/32/28736_2.png) [@KEHT](https://forum.inductiveautomation.com/u/KEHT)\
**Post date:** [May 10, 2022, 1:48pm UTC](https://forum.inductiveautomation.com/t/component-scripting-for-template/53344/6 "2022-05-10T13:48:01Z")

</div>

So, basically, you have to have all of your mouse event scripts in one place? Either on a template or the instance of the template. You can’t have a common “mouse over” effect done in a template, and have each instance navigate to a different page via _mouseClicked_?!

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 10, 2022, 6:18pm UTC](https://forum.inductiveautomation.com/t/component-scripting-for-template/53344/7 "2022-05-10T18:18:59Z")

</div>

> [@KEHT](#):
>
> You can’t have a common “mouse over” effect done in a template, and have each instance navigate to a different page via _mouseClicked_ ?!

If each template is doing the same thing, why not pass the navigation target as a template parameter?

The underlying behavior here (only one mouse event handler captures events at a time) is a Java Swing behavior, not an intentional decision we made. Vision has relatively few abstractions over Swing, so Swing's limitations _are_ Vision's limitations.

If you're _really_ motivated, you could use the [`EventDelegateDispatcher`](https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.16/com/inductiveautomation/factorypmi/application/components/util/EventDelegateDispatcher.html) class we use internally to automatically "forward" events from one component to another, but it's very situational and usually not worth messing with.
