Flipping the tree around can give a better understanding of why we can only move up and down through the tree structure, and how getting to a component can be very different depending on where you start. Let's say we want to go to the Text Field so that we can grab its value.
|Start From||Path to Text Field|
|Edit Table Container|
|Enter Data Button|
To move up or down within the hierarchy, there are two special commands we can use on component objects: parent and getComponent(). The parent property allows us to grab a reference to whatever is directly above the component in the hierarchy, which in most cases is the root container. We could then access any component on the root container by using getComponent("Component Name") and then placing the name of the component we want to access within the parenthesis.
Both parent and getComponent() can be used as many times as necessary to reach the desired component, drilling up or down through layers of containers or or grouped components. Once you have a component reference, you can then access any one of that component's attributes by using the name of the property, just like when accessing a property on the source component.
.parent to go up the hierarchy and using
.getComponent(name) to go down. The parent of a root container is not the window, and a reference to the window does not have a
.getComponent(name) function. To get a reference to a window, simply use system.gui.getParentWindow with any component's
event object as the parameter. Once you have a reference to a window, you can use its
.rootContainer property to get to the root of the component hierarchy, and from here you can follow the rules laid out above.
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.
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:
You can also get the Root Container directly using the getRootContainer() function. This link of code works the same as the one above:
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.
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.
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.
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.
Complex Property Types
Some component properties have more complex property values. For example, Font properties typically have a type of "Font".
In most cases these property types are simply using some built-in Java AWT types. These can be manipulated from scripting in Ignition by importing the appropriate library.
See the AWT javadocs for more information.