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.
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 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)
Comments are closed. |
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?
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?)
Or using COM from Python:
import win32com.client
obj = win32com.client.Dispatch("Shell.Application")
for w in obj.Windows():
print w.LocationName + "=" + w.LocationURL
Hah, I just read Richie’s closer and realized mine is just a slight variation on his theme…
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…
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.
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. :)
Hey, I got an even shorter version, English:
"Print all shell window names and URLs"
(That’s my attempt at humour today).
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():
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.)
memet: Abort, Retry, Ignore?
(an even sillier attempt…)
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");
It used to mean something on 16-bit Windows.
PingBack from http://eschew.org/blog/?p=156