Iterating through components on a screen through scripting

I have a need to iterate through all of the components on a screen and grab certain properties from those components from a script. I have a function that works to get all of the components on the screen and return a list of those component objects. The issue that I run into is that this list includes only those components that are in the root container of the window. Any containers I have on the window will be returned as normal components in the componentList.

Is there any way to check to see if a component is a container? I’d like to recursively iterate through all of my containers and add any components within them to the componentList. Here’s the code snippet I’m using so far.

#-------------------------------------------------------------------------
# Grabs all components within the root container of a window
#-------------------------------------------------------------------------
def getComponentsOnWindow(displayPath):
    # Empty component list
    componentList = []
    window = system.gui.getWindow(displayPath)
    container = window.getRootContainer()
    
    # Create a list of all of the component names
    for component in container.components:
        componentList.append(component)

    return componentList

You could use Python’s type() to compare the type of each component against the Root Container:

def getComponentsOnWindow(displayPath):
    # Empty component list
	componentList = []
	window = system.gui.getWindow(displayPath)
	container = window.getRootContainer()

	for component in container.components:
		# If the type of the Root Container and Component match, 
		# we're currently iterating over a container
		if type(component) == type(container):
			print "A container: %s" % component.name
		# Otherwise, it's something else. 
		else:
			print "Not a container: %s" % component.name
1 Like

That’s exactly what I was after, thank you!