vbscript to auto detect open window and close it OR Google Apps Script to control form submit button

user2116516 picture user2116516 · Nov 7, 2013 · Viewed 31.3k times · Source

In the past I've used the script below to detect if an active window with the title "Remote Desktop" is or isn't active. If it isn't active the script will auto launch or make it active again.

Question: Is there a way to detect if a window is active and auto close it? I'm setting up a kiosk in chrome kiosk mode on a Windows 7 machine for our office. The main page launches a selected Google form in a pop-up window. The form confirmation page has the title "Thank You!" in the title bar. Is there a way for the script to auto detect this window and close it? This would be nice because the user would see that their response was submitted (for a second or two) but if they did not close the window it would not still be open when the next user goes to use the kiosk.

Another option might be if there is a way to use a Google script on the form to program the submit button to close the window. I'm not sure if that's possible.

Option Explicit
'On Error Resume Next

Dim objShell

Set objShell = CreateObject("WScript.Shell")

Do 
If (objShell.AppActivate("Window Title Here") = False) Then
objShell.Run "mstsc.exe " & chr(34) & "c:\scripts\Remote Desktop.rdp" & chr(34)
WScript.Sleep 5000
Else
WScript.Sleep 3000
End If
Loop

If the window I need closed is active and then the following script is ran the window will close. It's almost like I need to piece the top and bottom code here together to achieve what I need, but I'm not sure how. Dim oShell

Set oShell = CreateObject("WScript.Shell") 
If oShell.AppActivate("Untitled - Notepad") Then
   WScript.Sleep 500
   oShell.SendKeys "%{F4}"
End If

I'm trying to find a script that will run on startup and wait for a window with a specific title to open and then close it once it is detected. It would be even better if I could control how long the window remains open once detected, but if I could just get it to close that would suffice.

I think I've found a good solution. I found this post and modified the answer. Does anyone see any issues with this?

' Will loop forever checking for the message every half a second  
' When it finds the message it will close the window 

Set wshShell = CreateObject("WScript.Shell") 

Do 
    ret = wshShell.AppActivate("Untitled - Notepad") 
    If ret = True Then 
        wshShell.SendKeys "%{F4}" 'ALT F4
    End If 
    WScript.Sleep 500 
Loop 

Answer

Chazmania picture Chazmania · May 5, 2014
Set wshShell = CreateObject("WScript.Shell") 

Do 
ret = wshShell.AppActivate("Untitled - Notepad") 
If ret = True Then 
    wshShell.SendKeys "%{F4}" 'ALT F4
End If 
WScript.Sleep 500 
Loop

The only trouble i see is the Alt+F4 will sequentially close windows until it will want to shut down windows itself. kinda makes me nervous even though this script will only detect the name you give it.

I tried the script and it works fine but what about using the escape key for certain windows?

The other thing i do not like is this is always running and taking resources. I slowed down the loop so it will at least be paused in the background most of the time..