Similar to the startup scripts we've seen previously, we also have client event shutdown scripts available to us. In this lesson, we'll demonstrate how we might perform some desired actions when a vision client is logged out of or otherwise terminated. On the client shutdown side, we actually have two different versions of the shutdown script. Let's see how each of these scripts work. As usual, these are accessed by going to the project menu in our designer and selecting the client events option, and there we see both shutdown options. The regular shutdown variant is run whenever a client is shut down, such as by logging out. Then there is also a shutdown intercept variant which can be used to interact with the user and potentially give an opportunity to intercept an abrupt shutdown. Let's first consider a very simple example for the regular shutdown script. In this example, we'll use the command system.security.getusername to get the username of the current user.
Then we'll embed that into a larger string, and we'll display it in a gui popup using system.gui.messagebox. Let's click apply to save this change without closing the dialogue, then we'll save our changes to the gateway at the upper left. Then to see the result of the script, we need to simply log out as any user. So I will bring up a minimized vision client from the toolbar. And right now, we can see that we're logged in as the admin. So I'll click log out up here at the upper right. And we see our popup displayed with the name of the current user admin. And when we click, okay, we see that we are in fact now logged out. A bit more interesting is the shutdown intercept script, the other option, which can be used to interact with a user and perhaps intercept a shutdown event.
For example, if the user clicks the X at the upper right to close the client window or whatever the OS close indicator is for this client, the script can jump in with a chance to intercept that action. So returning to our script window, when this script on the left runs, at the top here, we see there's already a variable and scope called event, similar to client event handlers. Part of it is a Boolean which the user can alter. If a user sets that cancel flag to one, the client won't be able to shut down. The script could ask the user a question or check some sort of condition. And if it decides that now is not a great time to shut the client down, it can simply set this cancel option to one on that event object. To see how this might work, let's, this time, consider our shutdown intercept script. This one's a bit more involved, but here's what it does. Let's assume that only an admin can kill the window off using the X, not operators or any other role.
Remember that we have all our roles and user accounts defined back over on our gateway. So in line two, we'll use system.security.getusername to return the current logged in username. Then on line three, we'll use that login, along with the default user source back on our gateway, to return a user object, which is this portion right here. And from that, we'll extract the user's role or roles into user roles. Now we can check whether an administrator is one of that user's roles or not. If they are an admin, they can kill off the window and it will notify the user as such, but any other roles cannot kill the window, as we see on this last else branch. Instead, what we'll do is we'll set event cancel equal to one which will intercept the client shutdown event, like so. In either case, we're going to display a gui popup telling the user what's happening, although in the end, it's up to you whether to do it with such a notification or just silently.
To see all this working, we need to go back to our vision client. And we need to log in again, first as a non-admin user to see the rejection. So we'll log in as an operator named Bob, and we see that we're logged in as Bob. We'll try to kill the window, and we cannot. We get the expected warning popup that only administrators can do that. So we'll click, okay. Now let's switch user to admin. And we see that we're logged in as the admin. So now, we'll try to kill the window again with the X, and this time we get the notification popup that we are terminating as the administrator, and the notification from the other popup if we still want that.
Once we click, okay, the whole client window disappears as we originally intended. So in this lesson, we've seen how to put two kinds of shutdown scripts to use, and even potentially intercept an otherwise abrupt client window termination.