# Trying to loop open windows and close all except two

**URL:** <https://forum.inductiveautomation.com/t/trying-to-loop-open-windows-and-close-all-except-two/58722>\
**Category:** Ignition\
**Tags:** vision, scripting, ignition81\
**Created:** [April 21, 2022, 9:31pm UTC](https://forum.inductiveautomation.com/t/trying-to-loop-open-windows-and-close-all-except-two/58722 "2022-04-21T21:31:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrews](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@andrews](https://forum.inductiveautomation.com/u/andrews)\
**Post date:** [April 21, 2022, 9:31pm UTC](https://forum.inductiveautomation.com/t/trying-to-loop-open-windows-and-close-all-except-two/58722/1 "2022-04-21T21:31:39Z")

</div>

Hello all,

So I have a home page. And I have a logo(label atop), which should redirect you to the home page. I need to make sure when that happens I close anything open but my nav and home page.

My current code closes ALL windows. Not sure what I did wrong, I just know its something simple, i’m overthinkingor did wrong. lol

Code is below:

```auto
system.nav.swapTo('Home')
windows = system.gui.getOpenedWindowNames()
for path in windows:
	if path != 'Home' or path != 'Navigation Menu':
		system.nav.closeWindow(path)

```

---

<div class="post-metadata">

**Author:** ![josborn](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@josborn](https://forum.inductiveautomation.com/u/josborn)\
**Post date:** [April 21, 2022, 9:44pm UTC](https://forum.inductiveautomation.com/t/trying-to-loop-open-windows-and-close-all-except-two/58722/2 "2022-04-21T21:44:43Z")

</div>

Swap that `or` for an `and`

---

<div class="post-metadata">

**Author:** ![andrews](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@andrews](https://forum.inductiveautomation.com/u/andrews)\
**Post date:** [April 21, 2022, 9:48pm UTC](https://forum.inductiveautomation.com/t/trying-to-loop-open-windows-and-close-all-except-two/58722/3 "2022-04-21T21:48:05Z")

</div>

@josborn thank you! I just facepalmed so hard right now.

---

<div class="post-metadata">

**Author:** ![josborn](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@josborn](https://forum.inductiveautomation.com/u/josborn)\
**Post date:** [April 21, 2022, 9:48pm UTC](https://forum.inductiveautomation.com/t/trying-to-loop-open-windows-and-close-all-except-two/58722/4 "2022-04-21T21:48:47Z")

</div>

heh, no worries. I cant tell you how many times missed this type of thing.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [April 21, 2022, 10:04pm UTC](https://forum.inductiveautomation.com/t/trying-to-loop-open-windows-and-close-all-except-two/58722/5 "2022-04-21T22:04:46Z")

</div>

> [@andrews](#):
>
> `if path != 'Home' or path != 'Navigation Menu':`

Could be:  
`if path not in {"Home", "Navigation Menu"}:`  
"Reads" a little bit cleaner, no need to think about the boolean condition, and easily extensible if you want to allow a third window.

I might even go so far as to make an `windowsToKeep = {}` or `allowedWindows` or something variable, just to further lower cognitive load the next time you need to scan this in a year or five.

---

<div class="post-metadata">

**Author:** ![andrews](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@andrews](https://forum.inductiveautomation.com/u/andrews)\
**Post date:** [April 21, 2022, 10:19pm UTC](https://forum.inductiveautomation.com/t/trying-to-loop-open-windows-and-close-all-except-two/58722/6 "2022-04-21T22:19:30Z")

</div>

Even better! Can’t say no to clean code. I will freshen it up tomorrow.

Thank you
