Finding Components in Project Script

I’m trying to find a way to have a function that can be called by any of the components in a window. Unless I am missing something, I think the correct way to do this would be to use a project script.

In the function I will need to access and modify components within the window. But I haven’t figured out how to find a component in the window from the project script. I have tried the syntax described in the video “Finding Components on Other Windows” but from the point of view of the project script there are no open windows.

Is it correct to use a project script here? And how do I access the components?

Thank you.

Yes, you should use a project script.

The components in a window have event handlers. Every event handler has a special variable called “event”. The “event” variable is important because it gives you access to all the components in the window. So you pass the “event” variable from an event handler script as an argument to your project script. Once you do that you have access to all the components in the window from your project script.

Here is an example of a project script function that receives the “event” variable from a component event handler script and uses it to access components in the window:

def accessComponents(event):
	#we get the root container of the window
	root = system.gui.getParentWindow(event).rootContainer
	#with the root container we can easily access all objects in the window
	table = root.getComponent("Table")
	label = root.getComponent("My Special Lable")
	#we can modify properties of components and do many other things with components
	label.text = "The accessComponents function was called."

Here is an example of an an event script that could be used in any component event handler, such as an actionPerformed event handler of a button or mouseClicked event handler of a label, etc. etc. Notice that all we need to do is call our project script module function, passing in the “event” variable as an argument:

project.mymodule.accessComponents(event)

Best,