Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the active window identifier?

Status
Not open for further replies.

WilliamCalm

Programmer
Feb 6, 2004
9
CN
Hi friends:

I was actually doing Wscript programming but I couldn't find a forum for Wscript, so I'm here. I need my Wscript program be able to tell what the active window is, to be specific, I need it to judge if the active window is Internet Explorer with a particular web address, and then the program shall respond accordingly. I searched all over but couldn't find out how, so please help if any knows how to do it. Thanks in advance.
 
Hello WilliamCalm,

I think we have problem in getting to the active window in general within vbscript pure.

Now, it depends on what you have in mind. If you can get-by by probing all top-level windows and know if an instance of webbrowser with the url being your signature window, I can make you get to the iwebbrowser2 object itself, active window or not. Once you get hold of the object's handle, you can decide what you want. You can kill it or you can make it window getting the focus (active).

You see, this scheme is not dependent on the prior determination which top-level window get the focus (active). But still, my above explanation means to tell that you can eventually get your target window killed or get focus.

If you want to make progress under the idea sketched above, you could first explain what you have in mind about the action.

regards - tsuji
 
Hi, tsuji

Thank you very much for your response, what I have in mind is that I want my Wscript to detect if the active window is the webbrower with a particular URL, if not, the Wscript shall do nothing, and if yes, it will just send a key press (a space) to the computer (by the command WshShell.SendKeys " "). This program shall not try to kill a window or bring an inactive window to active status. Would you help me with that? And if you require any further clarification, I will be most glad to elaborate. :)
 
Perhaps you may take a look at the AppActivate method ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hello again,

Try testing out this enumeration of top-level windows with just enough information. You'll see, if you discover instances of iwebbrowser2, you can in fact query and know the exact page being loaded.
Code:
const surl="[URL unfurl="true"]http://www.microsoft.com"[/URL]
'const surl="[URL unfurl="true"]http://www.tek-tips.com"[/URL]

on error resume next
set shapp=createobject("shell.application")
if err<>0 then wscript.echo err.number & vbcrlf & err.description : wscript.quit(2)
if cwins is nothing then wscript.echo "No active desktop? Quit." : wscript.quit(1)
on error goto 0
set cwins = shapp.Windows
for each owin in cwins
    on error resume next
    bFound=(strcomp(typename(owin), "iwebbrowser2",1)=0) _
            and (strcomp(typename(owin.document),"htmldocument",1)=0) _
            and (err.number=0)
    err.clear
    on error goto 0
	if bFound then
		'wscript.echo owin.locationURL
		if instr(1,owin.locationURL,surl,1)<>0 then
			set oie_id=owin
			exit for
		end if
	end if
next
set cwins=nothing
set shapp=nothing
if isobject(oie_id) then
	wscript.echo oie_id.document.location.href
	wscript.echo oie_id.locationurl
	wscript.echo oie_id.locationame
end if
set oie_id=nothing
The above crudely identified an instance oie_id. You can refine the signature surl either less restrictive like: "microsoft.com" or more restrictive including specific page involved.

Now, the above script only displaying some property related to oie_id so identified for further development. It says nothing about whether it being on-focus or out-of-focus. That is a perpetual problem.

But, the point to make also is that you can absolutely make it active (on focus) if you want to. Only, the method I use is pretty ugly. This is to continue with the oie_id above.
Code:
if isobject(oie_id) then
    surl_id=oie_id.locationurl
    oie_id.quit
    set wshshell=createobject("wscript.shell")
    wshshell.run "iexplore " & surl_id
    set wshshell=nothing
end if
A fresh run instance of it would make it active.

regards - tsuji
 
Hi, tsuji and PHV

Sorry for not following up this week as I was out for one week's outdoor business, I shall look into the code that you have provided and then see how it goes. Thanks to you guys again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top