# Navigating windows using relative paths

**URL:** <https://forum.inductiveautomation.com/t/navigating-windows-using-relative-paths/18066>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [May 16, 2018, 8:45pm UTC](https://forum.inductiveautomation.com/t/navigating-windows-using-relative-paths/18066 "2018-05-16T20:45:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cesrom](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@cesrom](https://forum.inductiveautomation.com/u/cesrom)\
**Post date:** [May 16, 2018, 8:45pm UTC](https://forum.inductiveautomation.com/t/navigating-windows-using-relative-paths/18066/1 "2018-05-16T20:45:53Z")

</div>

Having searched this forum, and the ideas site, I couldn’t find something about my “situation”.

Consider I have the following window hierarchy:

```auto
Windows
˫ Folder 1
  ˫ Subfolder 1
    ˫ Window 1
    ˫ Window 2
  ˪ Subfolder 2
    ...
˪ Folder 2

```

Whenever I want to navigate from one window to another I usually perform a swap, say:

```auto
system.nav.swapWindow(
    'Folder 1/Subfolder 1/Window 1', # or system.nav.getCurrentWindow()
    'Folder 1/Subfolder 1/Window 2',
    params
)

```

What would be extremely awesome would be the following:

```auto
# From: 'Folder 1/Subfolder 1/Window 1'
# To: 'Folder 1/Subfolder 1/Window 2'
system.nav.swapWindow(
    system.nav.getCurrentWindow(),
    './Window 2',
    params
)

# Or
system.nav.swapTo(
    './Window 2',
    params
)

```

Thoughts?

Should I just build it myself?

---

<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:** [May 16, 2018, 9:06pm UTC](https://forum.inductiveautomation.com/t/navigating-windows-using-relative-paths/18066/2 "2018-05-16T21:06:27Z")

</div>

Which window is the current window? There can be any number open at any given time. Including none open at all.

---

<div class="post-metadata">

**Author:** ![cesrom](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@cesrom](https://forum.inductiveautomation.com/u/cesrom)\
**Post date:** [May 16, 2018, 9:09pm UTC](https://forum.inductiveautomation.com/t/navigating-windows-using-relative-paths/18066/3 "2018-05-16T21:09:56Z")

</div>

In my example, the current window is `'Folder 1/Subfolder 1/Window 1'`.

Modified code for clarity.

```auto
# From: 'Folder 1/Subfolder 1/Window 1'
# To: 'Folder 1/Subfolder 1/Window 2'
system.nav.swapWindow(
    'Folder 1/Subfolder 1/Window 1',
    './Window 2', # Full path: 'Folder 1/Subfolder 1/Window 2'
    params
)

```

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [May 16, 2018, 10:54pm UTC](https://forum.inductiveautomation.com/t/navigating-windows-using-relative-paths/18066/4 "2018-05-16T22:54:03Z")

</div>

This type of stuff is fun to build. Enclosed is how I would do it. Place the following script inside of a shared script, like shared.nav

```auto
from com.inductiveautomation.ignition.common.model import ApplicationScope

PATH_SEPARATOR = "/"
DIR_UP_CHAR = ".."
PARENT_DIR_CHAR = "."

SCOPE = ApplicationScope.toCode(ApplicationScope.getGlobalScope())
def swapWindow(window1,window2,params = {}):
	
	if SCOPE in ("C","D"):
		pathParts = []
		wp1 = window1.split("/")
		wp2 = window2.split("/")
		if wp2[0] not in (DIR_UP_CHAR, PARENT_DIR_CHAR):
			pathPaths = wp2
		elif wp2[0] == DIR_UP_CHAR:
			pathParts = wp1[:-2] + wp2[1:]
		elif wp2[0] == PARENT_DIR_CHAR:
			pathParts = wp1[:-1] + wp2[1:]
			
		window = []
		for part in pathParts:
			if part == DIR_UP_CHAR and len(window):
				window = window[:-1]
			elif part == PARENT_DIR_CHAR:
				pass
			else:
				window.append(part)
		windowPath = "/".join(window)

		system.nav.swapWindow(window1,windowPath,params)	

```

Then, instead of using system.nav.swapWindow, use shared.nav.swapWindow

````auto
swapWindow("Hello/This/Is/My/Path","../OtherWindow",{"someId":"alf"})```
````

---

<div class="post-metadata">

**Author:** ![cesrom](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@cesrom](https://forum.inductiveautomation.com/u/cesrom)\
**Post date:** [May 17, 2018, 1:02am UTC](https://forum.inductiveautomation.com/t/navigating-windows-using-relative-paths/18066/5 "2018-05-17T01:02:33Z")

</div>

Thanks, @Kyle_Chase! 👌

This is exactly what I had in mind.

Do you mind if I borrow this piece of code and incorporate it into a library I’ve shared on [GitHub](https://github.com/thecesrom/Ignition)? (Shameless plug)

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [May 17, 2018, 3:08am UTC](https://forum.inductiveautomation.com/t/navigating-windows-using-relative-paths/18066/7 "2018-05-17T03:08:06Z")

</div>

I’ll make a pull request tomorrow and you can pull it in that way?

---

<div class="post-metadata">

**Author:** ![cesrom](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@cesrom](https://forum.inductiveautomation.com/u/cesrom)\
**Post date:** [May 17, 2018, 3:12am UTC](https://forum.inductiveautomation.com/t/navigating-windows-using-relative-paths/18066/8 "2018-05-17T03:12:41Z")

</div>

Sure thing, Kyle!!! (Additional exclamation points added because posts must be at least 20 characters long.)

---

<div class="post-metadata">

**Author:** ![cesrom](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@cesrom](https://forum.inductiveautomation.com/u/cesrom)\
**Post date:** [May 17, 2018, 7:05am UTC](https://forum.inductiveautomation.com/t/navigating-windows-using-relative-paths/18066/9 "2018-05-17T07:05:43Z")

</div>

Sorry, Kyle, but I couldn’t stop myself; I jumped the gun, and incorporated your function into _incendium_,

Thanks!

See here: [incendium.nav](https://github.com/thecesrom/Ignition/blob/master/python/incendium/nav.py)
