Event when new main window is opened

Is there an event that is fired each time a new main window is opened?

I want to run a script each time a new main window is opened. How might I achieve this?

1 Like

Yes, the visionWindowOpened event script that exists on every window.

Click on a window in the Designer Project Browser and then press Ctrl-J to open the scripting window.

2 Likes

I am aware of the visionWindowOpened event. The problem with using this is that I then have to load each main page with the script, which is a maintenance nightmare.

I was hoping that I could have a client script or something that could detect when any new main window is opened. That way there is only one script in one location.

You could write a function in the Script Module Editor and then just call that function in each visionWindowOpened event script for each window.

This would give you one script in one location. It just needs to be called in each visionWindowOpened event script.

So each visionWindowOpened event script would have a one liner like this:

app.mymodule.myfunction(event)
1 Like

That’s how I’m doing it now. But I still have to add the function call to each window script. What if I want to stop using the script? Then I have to manually remove the function call from each window.

If you want to stop using the script you would just modify the script to exit early so it doesn’t do anything.

Example:

if event.source.name in ["My Window", "My Other Window"]:
	return

Things like this are not really an maintenance problem until they are.

nmudge - thanks for your input.

The solution you proposed is a suitable work around but it is still not ideal. I don’t want to add a function call to each main window. Period.

Each time I create a new main window I will have to remember to add the function call. And if I decide not to use it anymore then I have to edit each window to remove it. Or, as you suggested, adjust the script to exit early. But that just leaves scripts in my project that are doing nothing… it’s messy.

I am looking for a solution that does not involve using the window script.

I understand.

You would have to dig into Swing and the Ignition classes and add some kind of listener that notifies you when a window is opened. Sounds like a fun thing to do. I would implement a solution for you but I don’t have time.

Best,
Nick

I also don’t like leaving unused code laying around, and know of no other way to do what you need to do either. But in the interim, if you create the script in a window, as he suggested, that calls a Script Module script, then when you need a new window, just duplicate an existing one and the script will be in it already.

If you don’t want all the stuff that is in the duplicated window, you can create an unused “template” window with your common stuff that is used in every window, including the script, then just duplicate that window as a starting base.

I was able to find a clean solution to this thanks to some help from Luke in the support group.

There is a system client tag ([System]Client/User/CurrentWindow) that seems to hold the name of the current main window open on the client. When a new main window is opened the tag value will change and I can place my script function in a client tag change script.

2 Likes

That seems like a pretty good idea. Nice.