Tag change scripts allow tag data changes in your application to trigger the execution of specified script actions. In this lesson, we'll learn how to use tag change scripts with a simple demo example. So, let's set the stage for this with the following. In our Ignition Designer, we've got these three elements: a main window, tag change main, which has a checkbox. And this checkbox writes to this predefined memory tag named open the window, which is a type Boolean, and a popup window named tag change popup with a couple of simple display labels. As you might've guessed already, the idea here will be to check the check box, which has a binding to the tag and updates its value, which we can demonstrate by going into preview mode and checking the check box, and seeing that the tag updates and updating the tag, and seeing that the checkbox updates also.
So going back to design mode. And then finally, we want the update of this tag to initiate a popup of this popup window. So how do we access tag chain scripts? They're found in the same place as all other client scripts, in the designer project menu, in the client events option, and then here in the tag change option of the client events scripts popup. Mine just happens to be here already since this was the option I was working in when I last saved my project. Just click on tag change here if that's not the case for you. Let's take a closer look at this client tag change scripts dialogue. The first thing we wanna do is create a name script. So we'll click on this plus icon down here at the bottom and give it a name. How about window opener. And note that we can have many such name scripts defined. Of course, we'll leave this one enabled here at the right.
Next, let's consider which attributes of tag changes we want to trigger our script. We can trigger our script to run if any of these tag attributes change, the tag quality, or the timestamp also, but for now, we'll just use a value change. And we'll unselect these first two and focus on value changes. Next, let's specify which tags will trigger our script to run. In this tag paths pane, we can add multiple tags which will trigger the script, one per line. And when any of these tags change, it will trigger the script to run. So for our example, we'll click on the tag icon at the right, navigate to our tags folder, select our open the window tag, click, okay, and we see the tag of interest in our tag path pane. So whenever there's any change to the value of the open the window tag, the window opener tag change script will be executed.
Next, let's write the script actions which will take place. When the script runs, it will be given three variables, and we'll print each one of them out. The first one we'll print is called event, and it's a tag change event object. The most important part of it is that it has a tag path property. So in the case where your tag chain script can be triggered by multiple tags, you can check which tag has changed by looking at its event.tag path property, like so. The second one we'll print is the initial change flag, which is zero or one, based on whether or not the script was triggered by subscribing to the tag or not. Whenever you first start up a system and it subscribes to all the tags, it gets an initial change event which says, hey, the tag didn't really change right now, but since you just subscribed, we figured we might wanna let you know what the initial value is.
Sometimes that's really useful for a tag change script, and sometimes it's not. If you want to consume that initial value, you don't have to worry about this initial change. But if you do want to ignore the initial startup value, you may not wanna run your script if the initial change flag is set to true. It gives you the option to decide for yourself. And finally, we also have the new value variable. New value is itself a qualified variable, which means it contains not only a value, but also a quality and a timestamp. So this variable will tell us what value the tag changed to, and will print it out like so. In our case, new value is the one we really care about because we'll only display our popup window if it's value has become true, since it's bound to our Boolean tag.
So to check that, we can do something like this. If newvalue.value, and that's all we need, since it itself is a Boolean. So if that's true, we'll open the popup window, like so. system.nav.openwindow, and in parentheses and double quotes, we need the actual full path to the popup window, which we can get by right-clicking on the popup window in the project browser, selecting copy path, and returning to our scripts window, and copy pasting it like so, and we'll terminate the entry. And for bonus points, what we'll do is we'll copy that statement, like so, and we'll change the open window to center window to center it on the screen.
So let's see our script in action now. We'll first click okay here at the bottom to save our changes. And then at the upper left, we'll save our changes to the gateway. And we'll also need to open a vision client to see our script in action. So I'll open up one I have minimized in my toolbar. And I'll log in as a user. Let's say admin, like so. You'll notice that our window appears on start-up. This is because we had it set that way in the project browser to open on start-up. And we see that our tag currently has a false unchecked value in the tag browser. So we'll click the check box, and we see that the tag's value updated, and we see the popup appear.
Ta da! But at this point, you might be saying, hey, wait, what about that data we also printed out? To see it, let's open up our console window by selecting help, diagnostics and the console tab. If we scroll up, or down rather, in our console just a little bit, we'll see our data. As a reminder of what all this data means, the first one is the tag which triggered the script to run, in this case, open the window. The second one is the initial value. And this first one means that it's being triggered by the initial subscribe. And the third one is the value, quality and timestamp of the tag. Note that the initial values are one and false. If we click the check box a few more times after closing that popup, we'll see that the value of the tag toggles between false and true, and back to false, as we expect.
And we see that the initial value remains at zero since it's past the initial subscribe. So with this example, we've seen how to write and use a simple tag change script that gets triggered whenever a specified tag's value changes.