Accessing a Component
Now that we have an understanding of how the component hierarchy works, we can apply that knowledge to accessing a component from anywhere within the project. While moving up and down within the hierarchy remains the same, accessing our initial component can differ depending on where we start our script from.
From an Event Handler
Event Handlers get a special event object that has special properties depending on the type of event. Regardless of the event, all event objects have a source property, which gives the component that fired the event. When accessing a component from an event handler, we can first use event.source
to get a component on the window to start at. From there, we can use parent or getComponent() as needed to get to the component we need to access.
# This would access the text property of a Text Field component.
print event.source.parent.getComponent('Text Field').text
When accessing a component from an event on the window, there will be a different path to the component than normal. If you already have a window object, you can use the function getComponentForPath(). This allows you to enter in the path to the component as a string (similar to expression bindings), and will end up looking something like this:
system.gui.getParentWindow(event).getComponentForPath('Root Container.Text Field').text
You can also get the Root Container directly using the getRootContainer() function. This link of code works the same as the one above:
system.gui.getParentWindow(event).getRootContainer().getComponent('Text Field').text
From an Extension Function
Extension Functions get a special self object which is actually a direct reference to the component that the extension function is on. This provides a direct reference point from which to access other components within the component hierarchy.
# This would access the text property of the component running the extension function script.
print self.text
# This would access the text property of the component named 'Text Field' if it is in the same container.
print self.parent.getComponent('Text Field').text
From a Client Event Script
Client Event Scripts are special because they don't start with a direct reference to anything on a particular window. So, we have to use another means of finding a starting point on the window. The system.gui.getWindow() function allows us to get a reference to a window which we can use to navigate to the root container with .getRootContainer(),
and then to any component on that window. However, this will only work if the window is currently opened. If the window is closed, it will throw an error, which can be handled with normal exception handling.
# Start the try block in case the window is not open.
try:
# Grab the window reference and assign it to the variable window.
window = system.gui.getWindow("Other Window")
# Use the window reference to get the text property off of a text field.
print window.getRootContainer().getComponent("Text Field").text
# Handle the exception by opening an informative error.
except ValueError:
system.gui.errorBox("The window is not open!", "Error")
From a Project Script
Project Library are unique in that how they access components can vary depending on where the Script Module is being called from and what is being passed to it. If the script module is being called from an event handler or an extension function, it is possible to pass in the event or self objects and use them within the script module.
# This code would go in a project script. We are defining our function that takes an event object
# and uses it to find the value of the text property on the text field in the same container.
def func(event):
print event.source.parent.getComponent('Text Field').text
# On the action performed of a button on our window, we could then use this to call our function.
myTestScript.func(event)

However, this may not always be the case. In these instances, it is possible to instead use the same method that Client Event Scripts use and grab the window object instead.