5

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 
Community
  • 1
  • 1
user2116516
  • 139
  • 1
  • 2
  • 11
  • Just that I understand this right: You want to find an application by (part of) its window title with VBScript on Windows? – Tomalak Nov 07 '13 at 19:35
  • Not possible with gapps script – Zig Mandel Nov 07 '13 at 20:01
  • Yes Tamalak. The script would launch on startup and detect the window when opened and then close it. I edited the original post to include the script I referred to in the first paragraph. – user2116516 Nov 07 '13 at 20:42
  • possible duplicate of [Get title opening windows and close with specific title?](http://stackoverflow.com/questions/2935591/get-title-opening-windows-and-close-with-specific-title) – Tomalak Nov 08 '13 at 09:51
  • 1
    @Tomalak I updated my post with my progress. – user2116516 Nov 12 '13 at 12:37
  • @Tomalak I updated my post again. I think I finally found an answer. Do you see any issues with using this solution? – user2116516 Nov 12 '13 at 18:02
  • That last update is pretty good, you can use that. The only drawback is that it won't work in a situation where the app title is not exactly predictable. In such a case you would need to switch to WMI to search for an application by process name (look [over here](http://stackoverflow.com/questions/3300583/killing-processes-in-vbscript) - there are many other examples once you know what to search for). If your app title is fixed the above will work just fine. If you write that as an answer, I'll upvote & you can accept your own answer at some point. – Tomalak Nov 12 '13 at 20:57
  • Thanks for the WMI suggestion. With the above solution the kiosk is working well. Google forms goes to a thank you page after submission so I changed the title in the script to match. – user2116516 Nov 14 '13 at 02:19

1 Answers1

8
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..

Math
  • 3,334
  • 4
  • 36
  • 51
Chazmania
  • 81
  • 1
  • 2