# Iterating through components on a screen through scripting

**URL:** <https://forum.inductiveautomation.com/t/iterating-through-components-on-a-screen-through-scripting/16335>\
**Category:** Ignition\
**Created:** [December 15, 2017, 3:44pm UTC](https://forum.inductiveautomation.com/t/iterating-through-components-on-a-screen-through-scripting/16335 "2017-12-15T15:44:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![brian.mcenulty](https://avatars.discourse-cdn.com/v4/letter/b/b77776/32.png) [@brian.mcenulty](https://forum.inductiveautomation.com/u/brian.mcenulty)\
**Post date:** [December 15, 2017, 3:44pm UTC](https://forum.inductiveautomation.com/t/iterating-through-components-on-a-screen-through-scripting/16335/1 "2017-12-15T15:44:43Z")

</div>

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.

```auto
#-------------------------------------------------------------------------
# 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

```

---

<div class="post-metadata">

**Author:** ![Paul.Scott](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul.scott/32/4578_2.png) [@Paul.Scott](https://forum.inductiveautomation.com/u/Paul.Scott)\
**Post date:** [December 15, 2017, 4:23pm UTC](https://forum.inductiveautomation.com/t/iterating-through-components-on-a-screen-through-scripting/16335/2 "2017-12-15T16:23:46Z")

</div>

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

```auto
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

```

---

<div class="post-metadata">

**Author:** ![brian.mcenulty](https://avatars.discourse-cdn.com/v4/letter/b/b77776/32.png) [@brian.mcenulty](https://forum.inductiveautomation.com/u/brian.mcenulty)\
**Post date:** [December 15, 2017, 4:33pm UTC](https://forum.inductiveautomation.com/t/iterating-through-components-on-a-screen-through-scripting/16335/3 "2017-12-15T16:33:07Z")

</div>

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