How Can I Tell if Any Internet Explorer Windows Are Open to a Particular Web Site?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I determine if I have any Internet Explorer windows open to a particular Web site?

— N

SpacerHey, Scripting Guy! AnswerScript Center

Hey, N. Before we begin the Scripting Guys would like to state, for the record, that we have never tried to obtain each other’s phone records nor have we ever even considered listening in on each other’s phone conversations. This has become an issue in the tech world, and we thought it was important that everyone knows where we stand.

Well, OK, if you want to get technical, at one time we did try listening in on Peter Costantini’s phone calls home. However, seeing as how none of us speak Martian we soon gave up on that.

In fact, not only do we not listen to each other’s phone conversations (some of us barely listen to our own phone conversations) but – and, for once, this is actually relevant to the question you asked – we typically don’t try to determine if any of us have any browser windows open to a particular Web site. That’s not because we aren’t nosy; we are. That’s simply because this is a difficult task to perform against remote computers. In turn, that’s because the only way we know to do this is to use the Windows Shell object, an object that can only be created – and used – on the local machine.

So what does that mean? Well, that means we’ll show you a solution that will work, albeit only on your local computer. That’s fine, except for one thing: what if you want to get this information from a remote machine? In that case we can think of a couple possibilities. For one, you could use the WSH Controller object or WMI’s Win32_Process class to kick off the script on a remote machine. Either of those methods will work, although you will have to modify the code so that the results aren’t displayed in a message box but are, instead, saved to a text file or database or something.

Note. Why can’t you display a message box using these methods? Well, you can; it’s just that the message box will pop up on the remote machine.

Alternatively, you might configure this script to run as a scheduled task; that way you could have it run every x number of minutes, check to see if any browser windows are open to the specified Web site, and then report back the results. Here, too you’ll have to modify the code so that the results are saved somewhere rather than displayed in a message box. That’s not particularly complicated, just something you’ll have to decide for yourself.

But enough with the excuses. Let’s take a look at a script that can tell us whether or not a browser window is open to a specified Web site:

Set objShell = CreateObject(“Shell.Application”)
Set objShellWindows = objShell.Windows

If objShellWindows.Count = 0 Then Wscript.Echo “No browser windows are open to the Script Center.” Wscript.Quit End If

blnFound = False

For i = 0 to objShellWindows.Count – 1 Set objIE = objShellWindows.Item(i) strURL = objIE.LocationURL If InStr(strURL, “http://www.microsoft.com/technet/scriptcenter”)Then blnFound = True End If Next

If blnFound Then Wscript.Echo “At least one browser window is open to the Script Center.” Else Wscript.Echo “No browser windows are open to the Script Center.” End If

Just like we said we would, we start off by creating an instance of the Shell.Application object. With that object in tow we then use the following line of code to return a collection of all the Shell windows currently open:

Set objShellWindows = objShell.Windows

In case you’re wondering, Shell windows are, well, windows that belong to the Shell. If you have Windows Explorer or a My Computer window open then you have at least Shell window open. The same is true for Internet Explorer: a running instance of Internet Explorer will appear in the Shell’s Windows collection.

Which, needless to say, is why we grabbed this collection in the first place.

Our next step is to check the value of the collection’s Count property; this tells us how many Shell windows are currently open. Why do we bother doing that? Well, if the Count is 0 that means there aren’t any Shell windows open; if no Shell windows are open then it’s a pretty good bet that there aren’t any Shell windows open to a particular Web site. Therefore, if the Count is 0 we echo back the fact that no browser windows are open to the target Web site, then use the Quit method to terminate the script. That’s what this block of code is for:

If objShellWindows.Count = 0 Then
    Wscript.Echo “No browser windows are open to the Script Center.”
    Wscript.Quit
End If

Cool. But what if the Count isn’t 0? What then?

Well, after setting a variable named blnFound to False (more on that momentarily) we set up a For Next loop that runs from 0 to the collection Count minus 1:

For i = 0 to objShellWindows.Count – 1

Note. We knew you were going to ask that: why does the loop run from 0 to the Count minus 1? That’s because our collection operates just like an array: the first item in the collection has an index number (subscript) of 0, while the last item has an index number equal to the total number of items minus 1. If we have 9 items in the collection, the index number of the last item will be 8 (9 – 1). Thus we start at 0 (the first item) and continue through the Count minus 1 (the last item).

Inside the loop we use this line of code to bind to the current item in the collection:

Set objIE = objShellWindows.Item(i)

As you no doubt know, the first time through the loop the counter variable i is equal to 0; that means that this code will bind us to item 0, the first item in the collection.

After making the connection we store the value of the LocationURL property in the variable strURL. We then use VBScript’s InStr function to determine whether or not the window is open to the target Web site (which is what the LocationURL property tells us). For example, if our target Web site is the Script Center, we know that any page in the Script Center has to start with the string http://www.microsoft.com/technet/scriptcenter. Consequently, we use InStr to determine whether that string appears anywhere in the LocationURL:

If InStr(strURL, “http://www.microsoft.com/technet/scriptcenter”)Then
        blnFound = True

If the target string is found (meaning InStr returned a value other than 0) we then set the value of the variable blnFound to True; that way we’ll know that we found at least one window open to the Script Center. If the target string is not found then we leave blnFound alone. Either way, we then loop around and check the next item in the collection.

That brings us to our final block of code:

If blnFound Then
    Wscript.Echo “At least one browser window is open to the Script Center.”
Else
    Wscript.Echo “No browser windows are open to the Script Center.”
End If

Here we’re checking the value of blnFound. If blnFound is True, that means that, somewhere along the line, we stumbled upon a browser window open to the Script Center; in response, we echo back that fact. If blnFound is False, that can only mean one thing: we didn’t find any browser windows open to the Script Center. (Remember the value of blnFound starts out as False, and changes only if we find the target Web site.) In turn, we then echo back the fact that we didn’t find any browser windows open to the Script Center.

Like we said, N, this might not be the ultimate solution for you; however, it should be enough to get you started. (And it might be exactly what you need if you plan on running this script locally.) If this doesn’t quite answer your question just let us know. We’ll either try to come up with a better solution for you or we’ll just listen in on one of Dean’s phone conversations and see if he can come up with a better solution for you.

0 comments

Discussion is closed.

Feedback usabilla icon