Using script to query information from Internet Explorer windows

Date:July 5, 2005 / year-entry #179
Tags:code
Orig Link:https://blogs.msdn.microsoft.com/oldnewthing/20050705-08/?p=35043
Comments:    14
Summary:Some time ago, we used C++ to query information from the ShellWindows object and found it straightforward but cumbersome. This is rather clumsy from C++ because the ShellWindows object was designed for use by a scripting language like JScript or Visual Basic. Let's use one of the languages the ShellWindows object was designed for to...

Some time ago, we used C++ to query information from the ShellWindows object and found it straightforward but cumbersome.

This is rather clumsy from C++ because the ShellWindows object was designed for use by a scripting language like JScript or Visual Basic.

Let's use one of the languages the ShellWindows object was designed for to enumerate all the open shell windows. Run it with the command line cscript sample.js.

var shellWindows = new ActiveXObject("Shell.Application").Windows();
for (var i = 0; i < shellWindows.Count; i++) {
  var w = shellWindows.Item(i);
  WScript.StdOut.WriteLine(w.LocationName + "=" + w.LocationURL);
}

Well that was quite a bit shorter, wasn't it!


Comments (14)
  1. AC says:

    Wouldn’t the C++ code be simpler if it would also use automation interfaces and not the less wrapped ones? A little more general code to handle automation has to be written once, but then real work would be more like scripts?

  2. Richie Hindle says:

    Even shorter in Python (with the pywin32 extensions):

    from win32com.client.gencache import EnsureDispatch

    for w in EnsureDispatch("Shell.Application").Windows():

    print w.LocationName + "=" + w.LocationURL

    (the last line should be indented; I don’t know whether the blog will keep the indentation – is there any documentation on how to format a comment anywhere?)

  3. Anonymous Coward says:

    Or using COM from Python:

    import win32com.client

    obj = win32com.client.Dispatch("Shell.Application")

    for w in obj.Windows():

    print w.LocationName + "=" + w.LocationURL

  4. Anonymous Coward says:

    Hah, I just read Richie’s closer and realized mine is just a slight variation on his theme…

  5. Anon says:

    I’m usually a big fan of "It would be shorter in Python", but this time it would actually be shorter in VBScript:

    for each w in CreateObject("Shell.Application").Windows()

    WScript.Echo w.locationName & "=" & w.LocationURL

    next

    There should be an equivalent "for (var in collection) {}" in javascript, but it doesn’t seem to like me…

  6. There should be an equivalent "for (var in collection) {}" in javascript, but it doesn’t seem to like me…

    Look at:

    http://www.quirksmode.org/js/associative.html

    "for (var i in object) is equivalent to Perl foreach $key (keys %hash)."

    As there are now real collections in Javascript that applies to Arrays only. Unfortunatly different browsers interpret this differently: Some return the Array´s indices, iterate over the values (if I remember right)

    Javascript (at least in modern browsers) supports try – catch as well, which is mighty as hell :-)

    I´m quite suprised that MS seems to favor VB / VB.NET so much over Jscript / JScript.NET (which I consider the better language).

    On Querying IE:

    From the Eclipse project their is a SWT component "Browser" which uses IE on Windows. Very easy to use.

  7. Benjamin Johnston says:

    This is exactly the clue that motivated me to fix a little problem that has been bothering me for a while: how to shutdown all instances of IE, but have IE remember the state of each window.

    Starting with the example above, it turns out to be only a short script to save the enumerated internet explorer windows to a file; and then write the converse script to read from a file and recreate all the windows.

    Probably a few minutes of coding if you know what you’re doing, or 30 if you (like me) don’t know the APIs too well.

  8. David Wilson says:

    I’m completely puzzled as to why double-clicking sample.js causes an error, whereas running it via cscript.exe does not.

    Can someone explain this to me? Thanks. :)

  9. memet says:

    Hey, I got an even shorter version, English:

    "Print all shell window names and URLs"

    (That’s my attempt at humour today).

  10. Python’s main virtue isn’t *short* code; it’s *readable* code, so I prefer:

    import win32com.client

    shell = win32com.client.gencache.EnsureDispatch("Shell.Application")

    for window in shell.Windows():

    &nbsp;&nbsp;&nbsp;&nbsp;print window.LocationName, ‘=’, window.LocationURL

    (Again, the last line should be indented. Perhaps it will be, perhaps not; there’s no preview thingamabob on this blog.)

  11. CN says:

    memet: Abort, Retry, Ignore?

    (an even sillier attempt…)

  12. sdk says:

    the default (most of the time) for .js is wscript.exe

    and wscript has no clue how to use WScript.StdOut

    If u wanna test this try a .js file with the single line:

    WScript.StdOut.WriteLine("hello blog");

  13. It used to mean something on 16-bit Windows.

Comments are closed.


*DISCLAIMER: I DO NOT OWN THIS CONTENT. If you are the owner and would like it removed, please contact me. The content herein is an archived reproduction of entries from Raymond Chen's "Old New Thing" Blog (most recent link is here). It may have slight formatting modifications for consistency and to improve readability.

WHY DID I DUPLICATE THIS CONTENT HERE? Let me first say this site has never had anything to sell and has never shown ads of any kind. I have nothing monetarily to gain by duplicating content here. Because I had made my own local copy of this content throughout the years, for ease of using tools like grep, I decided to put it online after I discovered some of the original content previously and publicly available, had disappeared approximately early to mid 2019. At the same time, I present the content in an easily accessible theme-agnostic way.

The information provided by Raymond's blog is, for all practical purposes, more authoritative on Windows Development than Microsoft's own MSDN documentation and should be considered supplemental reading to that documentation. The wealth of missing details provided by this blog that Microsoft could not or did not document about Windows over the years is vital enough, many would agree an online "backup" of these details is a necessary endeavor. Specifics include:

<-- Back to Old New Thing Archive Index