How to Build an Extended Script Watchdog for External Applications

This time we are going to have a second look at a script watchdog for an external application. The first time we have learned how to monitor an external application and start it again, if it has been closed. Due to some customer requests based on that script watchdog, we are now going to enhance the script by also monitoring if the application has been minimized.

Before you start writing your own script for a SiteKiosk application watchdog please note that you can have SiteKiosk autostart an application and restart it after logout or screensaver activity by just configuring this behaviour in the SiteKiosk configuration. Go to Applications, click Add and select that SiteKiosk starts the application automatically.

The script we are about to create comes in handy, if you want the application to run permanently and you also want it to stay maximized. Whether or not an application can be closed or minimized is beyond what SiteKiosk can directly control, its based on what the original code of the application itself allows. If you have control of that code you should alter it, to prevent the user from doing these things if you do not want that. The script watchdog can take action if you cannot control the code of the application.

The script uses the SiteKiosk Object Model. As it is going to be a an external script and not part of the code of a website it will run with both IE- and Chrome-based versions of SiteKiosk.

We will add the monitoring for minimization of the application to the existing script that already monitors if the application is running. Please refer to the original post to learn more about that part of the code.

First we will add the OnInsert event and a global variable for the object required to handle our application. If OnInsert fires and the title of the application window matches the application to be monitored, we will assign the window object we receive through the event to the global object variable gk_skwin.

...
SiteKiosk.WindowList.OnInsert = OnInsert; //fires if a windows is inserted

var gk_skwin; //global variable for the window object to monitor the application

...
 
function OnInsert(skwin){
	//check if our application has been started
	if(skwin.ItemText === "WindowTitleOfApplicationToWatch"){
        gk_skwin = skwin; //application has been started, assign returned window object to global variable
	}
}
...

The next step is to add a periodic event that will check if our application has been minimized. The check runs every 5000 milliseconds. You can change the time to your liking.

SiteKiosk.Scheduler.AddPeriodicEvent(5000, CheckAppIsMinimized);

The CheckAppIsMinimized function called by the periodic event uses the IsMinimized method to check the application by using the handle from the global object that represents the monitored application. If the monitoried application is minimized, the Maximize method is called to maximize it again.

function CheckAppIsMinimized(){
   //use try/catch in case our application is not started and can therefore not be monitored
   try{
		//check if application is minimized
		if(SiteKiosk.WindowList.IsMinimized(gk_skwin.Handle)){
			//maximize the minimzed application
			SiteKiosk.WindowList.Maximize(gk_skwin.Handle);
		}
   }catch(e){}
}

If we put the code together we will get the follwing script. It uses Notepad to demonstrate its usability. Please note that the script could be a lot more elegant in handling the task at hand but has been kept as straight forward as possible for the purpose of this demonstration

SiteKiosk.WindowList.OnRemove = OnRemove; //fires if a window has been closed
SiteKiosk.WindowList.OnInsert = OnInsert; //fires if a windows is inserted

var gk_skwin; //global variable for the window object to monitor the application

SiteKiosk.Scheduler.AddDelayedEvent(5000, StartMyApp); //starts the desired application the first time after 5000 ms
SiteKiosk.Scheduler.AddPeriodicEvent(5000, CheckAppIsMinimized); //monitors whether application is minimzed every 5000 ms
 
function StartMyApp(){
    SiteKiosk.ExternalApps.Run("c:\\windows\\notepad.exe", true);
}

function OnInsert(skwin){
	//check if our application has been started
	if(skwin.ItemText === "Untitled - Editor"){
        gk_skwin = skwin; //application has been started, assign returned window object to global variable
	}
}
 
function OnRemove(skwin){
    //checks if the application that should run has been closed
    if(skwin.ItemText === "Untitled - Editor"){
        //the application has been closed, restart it again
        SiteKiosk.Scheduler.AddDelayedEvent(500, StartMyApp); //starts the desired application the next time after 500 ms
    }
}

function CheckAppIsMinimized(){
   //use try/catch in case our application is not started and can therefore not be monitored
   try{
		//check if application is minimized
		if(SiteKiosk.WindowList.IsMinimized(gk_skwin.Handle)){
			//maximize the minimzed application
			SiteKiosk.WindowList.Maximize(gk_skwin.Handle);
		}
   }catch(e){}
}

Copy and paste the script into an editor and save it as a javascript file (.js). It is recommended to store it in the ..\SiteKiosk\html folder, to make sure the SiteKiosk user can access it in Autostart mode.

To run the watchdog script with SiteKiosk, open the configuration, go to Start Page & Browser, select your browser engine and click on Advanced. Add the script to be executed on startup of SiteKiosk.