Close a script’s window on rerun

Hi! I’m working on a script that opens a window, and I’m tired of closing the previous window by hand on each change and re-run. I figured I can do this to close the previous window, but not sure if it properly terminates it? Feels hacky, so I wonder if there’s a proper way to do so?

for window in NSApp.windows():
	if window.title() == 'My Window Name':
		window.close()

Why not try to keep track of the original window. You should be able to store a reference in a global variable. Then you can either close the previous window or re-use it.

1 Like