# Client Timer Script for Rotating between different windows

**URL:** <https://forum.inductiveautomation.com/t/client-timer-script-for-rotating-between-different-windows/32731>\
**Category:** Ignition\
**Created:** [March 2, 2020, 1:56pm UTC](https://forum.inductiveautomation.com/t/client-timer-script-for-rotating-between-different-windows/32731 "2020-03-02T13:56:10Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [March 2, 2020, 1:56pm UTC](https://forum.inductiveautomation.com/t/client-timer-script-for-rotating-between-different-windows/32731/1 "2020-03-02T13:56:11Z")

</div>

7.9.5  
I am looking to write a Client Timer script to be able to swap between a list of windows. This is what I have so far.

```auto
swapWindows = {"Melt Overview":"Test","Test 2":"Test 3","Test 3":"Test 5"}
win = system.nav.getCurrentWindow()
nextWindow = swapWindows[win]
system.nav.swapTo(nextWindow)

```

When I run the script I get a error:

07:58:48.068 [designer-script-shared-timer-[Melt\_Dashboard]] ERROR com.inductiveautomation.ignition.common.script.TimerScriptTask - Error executing global timer script: Melt\_Dashboard/windowRotate @30,000ms . Repeat errors of this type will be logged as ‘debug’ messages.  
com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last):  
File “\<TimerScript:Melt\_Dashboard/windowRotate @30,000ms \>”, line 3, in   
KeyError

```
at org.python.core.Py.KeyError(Py.java:225)
at org.python.core.PyObject. __getitem__ (PyObject.java:655)
at org.python.pycode._pyx26.f$0(<TimerScript:Melt_Dashboard/windowRotate @30,000ms >:4)
at org.python.pycode._pyx26.call_function(<TimerScript:Melt_Dashboard/windowRotate @30,000ms >)
at org.python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1275)
at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:634)
at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:601)
at com.inductiveautomation.ignition.common.script.TimerScriptTask.run(TimerScriptTask.java:88)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)

```

Caused by: org.python.core.PyException: Traceback (most recent call last):  
File “\<TimerScript:Melt\_Dashboard/windowRotate @30,000ms \>”, line 3, in   
KeyError

```
... 12 common frames omitted
```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 2, 2020, 2:45pm UTC](https://forum.inductiveautomation.com/t/client-timer-script-for-rotating-between-different-windows/32731/2 "2020-03-02T14:45:47Z")

</div>

That means the result from system.nav.getCurrentWindow() wasn’t one of the windows in your dictionary (as a key). You have to handle that case. Either test with `if win in swapWindows:` or use a `try`-`except` block to catch the exception.

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [March 2, 2020, 3:29pm UTC](https://forum.inductiveautomation.com/t/client-timer-script-for-rotating-between-different-windows/32731/3 "2020-03-02T15:29:12Z")

</div>

> [@pturmel](#):
>
> if win in swapWindows:

I tried the if win example not for sure on the try-except one. I get a error with the if win example.

```auto
if win in swapWindows:
	swapWindows = {"Melt Overview":"Test","Test 2":"Test 3","Test 3":"Test 5"}
	win = system.nav.getCurrentWindow()
	nextWindow = swapWindows[win]
	system.nav.swapTo(nextWindow)

```

09:28:02.592 [designer-script-shared-timer-[Melt\_Dashboard]] ERROR com.inductiveautomation.ignition.common.script.TimerScriptTask - Error executing global timer script: Melt\_Dashboard/windowRotate @30,000ms . Repeat errors of this type will be logged as 'debug' messages.  
com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last):  
File "\<TimerScript:Melt\_Dashboard/windowRotate @30,000ms \>", line 1, in   
NameError: name 'win' is not defined

```
at org.python.core.Py.NameError(Py.java:260)
at org.python.core.PyFrame.getname(PyFrame.java:257)
at org.python.pycode._pyx73.f$0(<TimerScript:Melt_Dashboard/windowRotate @30,000ms >:5)
at org.python.pycode._pyx73.call_function(<TimerScript:Melt_Dashboard/windowRotate @30,000ms >)
at org.python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1275)
at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:634)
at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:601)
at com.inductiveautomation.ignition.common.script.TimerScriptTask.run(TimerScriptTask.java:88)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)

```

Caused by: org.python.core.PyException: Traceback (most recent call last):  
File "\<TimerScript:Melt\_Dashboard/windowRotate @30,000ms \>", line 1, in   
NameError: name 'win' is not defined

```
... 12 common frames omitted

```

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 2, 2020, 3:32pm UTC](https://forum.inductiveautomation.com/t/client-timer-script-for-rotating-between-different-windows/32731/4 "2020-03-02T15:32:30Z")

</div>

_win_ needs to be defined before you can do any testing with it. Try this:

```python
swapWindows = {"Melt Overview":"Test","Test 2":"Test 3","Test 3":"Test 5"}
win = system.nav.getCurrentWindow()
if win in swapWindows.keys():
	nextWindow = swapWindows[win]
	system.nav.swapTo(nextWindow)

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 2, 2020, 3:33pm UTC](https://forum.inductiveautomation.com/t/client-timer-script-for-rotating-between-different-windows/32731/5 "2020-03-02T15:33:35Z")

</div>

I wouldn’t expect you to be able to test `win` before you’ve assigned a value to it.

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [March 2, 2020, 4:14pm UTC](https://forum.inductiveautomation.com/t/client-timer-script-for-rotating-between-different-windows/32731/6 "2020-03-02T16:14:01Z")

</div>

in the client it switched to the second window but then it stopped

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 2, 2020, 4:32pm UTC](https://forum.inductiveautomation.com/t/client-timer-script-for-rotating-between-different-windows/32731/7 "2020-03-02T16:32:36Z")

</div>

Your second window is “Test”, but you have no key named “Test” to say what should follow.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [March 2, 2020, 4:34pm UTC](https://forum.inductiveautomation.com/t/client-timer-script-for-rotating-between-different-windows/32731/8 "2020-03-02T16:34:24Z")

</div>

Probably because your dictionary doesn’t include some of the windows you’re swapping to, as keys. For example the first key in your dict has a value of “test”. There is no key called “test” and so when the script changes the window to this and the script runs again, it’s not going to find it in the dictionary and will therefore not result in any more window changes. Your issue is semantic

Phil, beat me to it 😅

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 2, 2020, 4:38pm UTC](https://forum.inductiveautomation.com/t/client-timer-script-for-rotating-between-different-windows/32731/9 "2020-03-02T16:38:28Z")

</div>

Consider using a list of window names instead of a dictionary. Use the .index() method of the list to find the current window, then use `(idx+1) % len(thelist)` to pick the next window. Then there’s no need to explictly make keys and values for every transition.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 2, 2020, 4:41pm UTC](https://forum.inductiveautomation.com/t/client-timer-script-for-rotating-between-different-windows/32731/10 "2020-03-02T16:41:17Z")

</div>

> [@nminchin](#):
>
> Phil, beat me to it

Watch out for that Jordan dude--he'll steal your comment glory if I don't.... 🤩

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 2, 2020, 4:49pm UTC](https://forum.inductiveautomation.com/t/client-timer-script-for-rotating-between-different-windows/32731/11 "2020-03-02T16:49:32Z")

</div>

Along the same vein of questioning.

> [@Windows automatic swapping](http://forum.inductiveautomation.com/t/windows-automatic-swapping/19729/2):
>
> For rotating displays like this, I follow this procedure: set all of the windows to open on startup. use a client tag as a pointer to which window should be on top (I use one called ScreenPointer) a client timer script to set the pointer: pointer = system.tag.read('[Client]ScreenPointer').value pointer += 1 if pointer \> 5: #if max number of screens is 5 pointer = 1 system.tag.write('[Client]ScreenPointer') on each window (this is not a root container property, you have to hi…

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [March 2, 2020, 5:56pm UTC](https://forum.inductiveautomation.com/t/client-timer-script-for-rotating-between-different-windows/32731/12 "2020-03-02T17:56:33Z")

</div>

how can you accomplish this with out using a tag

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 2, 2020, 6:41pm UTC](https://forum.inductiveautomation.com/t/client-timer-script-for-rotating-between-different-windows/32731/13 "2020-03-02T18:41:57Z")

</div>

If you want to use Phil’s suggestion on using a list, it would look something like this:

```python
swapWindows =[
              "Melt Overview",
              "Test",
			  "Test 2",
			  "Test 3",
			  "Test 5"
			 ]

win = system.nav.getCurrentWindow()

if win in swapWindows:
	idx = swapWindows.index(win)
	nextIdx = (idx + 1) % len(swapWindows)
	system.nav.swapTo(swapWindows[nextIdx])

```

Note that you can put a list (or dictionary, for that matter) on separate lines to make it easier to read, and make sure the order of items is… um… in order.

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [March 3, 2020, 12:00am UTC](https://forum.inductiveautomation.com/t/client-timer-script-for-rotating-between-different-windows/32731/14 "2020-03-03T00:00:46Z")

</div>

Thanks for the update. I was able to get it working like this. Notice I did change the screen names prior to finishing the script.

```auto
swapWindows = {"Casting Overview": "Furnace Overview" , "Furnace Overview" :"Casting Overview"}
win = system.nav.getCurrentWindow()

if win in swapWindows.keys():
	nextWindow = swapWindows[win]
	system.nav.swapTo(nextWindow)

```

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [March 3, 2020, 5:23am UTC](https://forum.inductiveautomation.com/t/client-timer-script-for-rotating-between-different-windows/32731/15 "2020-03-03T05:23:47Z")

</div>

You’re still creating more work for yourself and for others by using a dictionary instead of a list

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [March 3, 2020, 7:45pm UTC](https://forum.inductiveautomation.com/t/client-timer-script-for-rotating-between-different-windows/32731/16 "2020-03-03T19:45:45Z")

</div>

I took your advice and changed it to JordanCClark’s example of a list.
