title: vbscript to auto detect open window and close it OR Google Apps Script to control form submit button
body: <p>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. </p>
  
  <p>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.</p>
  
  <p>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. </p>
  
  <pre><code>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 " &amp; chr(34) &amp; "c:\scripts\Remote Desktop.rdp" &amp; chr(34)
  WScript.Sleep 5000
  Else
  WScript.Sleep 3000
  End If
  Loop
  </code></pre>
  
  <p>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</p>
  
  <pre><code>Set oShell = CreateObject("WScript.Shell") 
  If oShell.AppActivate("Untitled - Notepad") Then
     WScript.Sleep 500
     oShell.SendKeys "%{F4}"
  End If
  </code></pre>
  
  <p>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. </p>
  
  <p>I think I've found a good solution. I found <a href="https://stackoverflow.com/questions/2261157/vbscript-to-detect-an-open-messagebox-and-close-it?rq=1">this post</a> and modified the answer. Does anyone see any issues with this?</p>
  
  <pre><code>' 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 
  </code></pre>