' Just run the script. The first time you run it, it will 
' create a sample list of pop-ups to be closed. It will
' also display a one-time help screen (the screen only appears
' when there is no list of pop-up titles).
'
' This script will send an "Alt-F4" keystroke to any window
' that has a title you don't like. Typically, it is used to
' close those annoying browser "pop-up" windows. The list of
' bad titles is kept in a separate file named the same as
' this script (but with a TXT file extension), one title per 
' line. AOL users who need to send "Ctrl-F4" should edit the 
' Const line so it says "^{F4}" instead of the normal "%{F4}".
'
Const CLOSE_STRING = "%{F4}"
'
' The Windows Scripting Host "App.Activate" function is used
' to search titles. It will try to match the exact title. 
' Failing that, it will try to match the beginning or end of 
' the window title. For example, When I browse to 
' http://www.ericphelps.com (a subversive site you shouldn't look 
' at!), the title in Netscape is "Eric's Webspace - Netscape",
' and in IE it's "Eric's Webspace - Microsoft Internet Explorer".
' So if I added a prohibited title of "Eric's Webspace - Netscape"
' (without the quotes) it would close my Netscape browser every
' time I went to my web page -- but IE would work fine. On the
' other hand, if I make the title just "Eric's Webspace" (again
' without the quotes), then we would get a match on the first
' part of the actual window title for both browsers: They would 
' both shut down as soon as my web page opened. The title match
' is not case sensitive: "eric's webspace" would also work.
'
' Now you know how it works, here's how to use it. If you get a 
' pop-up while you are browsing, leave it up. Add the title to 
' the list of bad titles and save the list. The pop-up should 
' close immediately after you save the list. If it doesn't, 
' look again at the title. There may be a leading space or more
' than one space between words in the title. Modify the list
' again and save it. Once you get it right, that window will
' always be closed immediately after it opens in the future.
'
' Fun uses... Add the names of pictures, sounds, or documents
' you don't want anyone to open. Or add the name (the title)
' of programs you don't want anyone to run. 


Dim gstrDataFile 'As String -- Name of text file containing bad window titles
Dim strEngine 'As String -- Used to tell the user what program is running the wscript.
gstrDataFile = FileNameLikeMine("txt")
If Not CreateObject("Scripting.FileSystemObject").FileExists(gstrDataFile) Then 
	String2File "----- Title Data File -----" & vbCrLf & "Amazing XCam2" & vbCrLf & "about:blank" & vbCrLf, gstrDataFile
	strEngine = Wscript.FullName
	strEngine = Mid(strEngine, InstRrev(strEngine, "\") + 1)
	strEngine = Left(strEngine, Instr(strEngine, ".") - 1)
	strEngine = Ucase(Left(strEngine, 1)) & Lcase(Mid(strEngine, 2))
	MsgBox "	This script will run until system shutdown killing windows whose titles are found in the ""Title Data File"" at """ & gstrDataFile & """." & vbCrLf & vbCrLf & "	If you need to stop this process, kill the """ &  strEngine & """ program with the Windows Task Manager (Ctrl-Alt-Del). Alternatively, you can stop the program by deleting, renaming, or emptying the title data file." & vbCrLf & vbCrLf &  "	This dialog will only appear when there is no title data file."
End If
While True
	KillWindows
	Wscript.Sleep 500
Wend


Sub KillWindows
Dim wsh 'As WScript.Shell
Dim fs 'As Scripting.FileSystemObject
Dim ts 'As Scripting.TextStream
Dim strData 'As String -- Entire contents of gstrDataFile
Dim strTitle 'As String -- Just one bad window title
Const ForReading = 1
	Set fs = CreateObject("Scripting.FileSystemObject")
	Set wsh = CreateObject("WScript.Shell")
	On Error Resume Next
	Err.Clear
	Set ts = fs.OpenTextFile(gstrDataFile, ForReading, True)
	If Err.Number = 0 Then
		strData = ts.ReadAll
		If Err.Number = 0 Then
			'Read all title lines in data string
			Do Until (Instr(strData, vbCrLf) = 0)
				strTitle = Left(strData, Instr(strData, vbCrLf) - 1)
				strData = Mid(strData, Instr(strData, vbCrLf) + 2)
				If strTitle <> "" Then
					If wsh.AppActivate(strTitle) Then
						wsh.SendKeys CLOSE_STRING
						WriteLog Now & " " & strTitle
					End If
				End If
			Loop
			'Grab last bit in case there was no ending CrLf
			strTitle = strData
			If strTitle <> "" Then
				If wsh.AppActivate(strTitle) Then
					wsh.SendKeys CLOSE_STRING
					WriteLog Now & " " & strTitle
				End If
			End If
		Else
			ts.Close
			Wscript.Quit 1
		End If
	Else
		Wscript.Quit 1
	End If
	ts.Close
End Sub
	 
Sub WriteLog(strText)
'Write to screen if script is run with CSCRIPT. Otherwise, write to a log file.
Dim fs 'As Scripting.FileSystemObject
Dim ts 'As Scripting.TextStream
Const ForAppending = 8
	If Lcase(Right(Wscript.FullName, 11)) = "cscript.exe" Then
		Wscript.Echo strText
	Else
		Set fs = CreateObject("Scripting.FileSystemObject")
		Set ts = fs.OpenTextFile(Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & "log", ForAppending, True)
		ts.WriteLine strText
		ts.Close
	End If
End Sub

Function FileNameLikeMine(strFileExtension) 'As String
'Returns a file name the same as the script name except
'for the file extension. 
Dim fs 'As Object
Dim strExtension 'As String
	Set fs = CreateObject("Scripting.FileSystemObject")
	strExtension = strFileExtension
	If Len(strExtension) < 1 Then strExtension = "txt"
	If strExtension = "." Then strExtension = "txt"
	If Left(strExtension,1) = "." Then strExtension = Mid(strExtension, 2)
	FileNameLikeMine = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & strExtension
End Function

Sub String2File(strData, strFileName)
'Writes a string to a file
Dim fs 'As Scripting.FileSystemObject
Dim ts 'As Scripting.TextStream
Const ForWriting = 2
	Set fs = Wscript.CreateObject("Scripting.FileSystemObject")
	Set ts = fs.OpenTextFile(strFileName, ForWriting, True)
	ts.Write(strData)
	ts.Close
End Sub


