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

1

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 
Dairymaid answered 7/11, 2013 at 19:19 Comment(8)
Just that I understand this right: You want to find an application by (part of) its window title with VBScript on Windows?Journeywork
Not possible with gapps scriptPieter
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.Dairymaid
possible duplicate of Get title opening windows and close with specific title?Journeywork
@Journeywork I updated my post with my progress.Dairymaid
@Journeywork I updated my post again. I think I finally found an answer. Do you see any issues with using this solution?Dairymaid
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 - 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.Journeywork
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.Dairymaid
K
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..

Kattegat answered 4/5, 2014 at 23:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.