views:

69

answers:

4

Is there any method in JScript to get the handle of the main window of a process by providing the process name? The Process.MainWindowHandle property works only in JScript .NET. Is anything similar available in classic JScript?

A: 

I am not sure if this works, just try to loop window.parent until its undefined.

something like -

var mainWindow = window;
while( mainWindow.parent ) {
    mainWindow = mainWindow.parent;
}

you also have something like window.top which always returns you the topmost window. But not sure if this is supported by all browsers.

Sachin Shanbhag
I use the same loop as Sachin to make sure I get my hands on the topmost window. It simply works.
Dick Lampard
Sachin, I do not have handle to any of the child window. I only have process name. I think the above loop will iterate up till the parent window returns undefined. Please correct me if my assumption is wrong.
satya
@rainbow365 - you do not need to have any child window handle to use the window object. It always returns you the current window in which you are currently working on. Check this - http://msdn.microsoft.com/en-us/library/ms535873(v=VS.85).aspx
Sachin Shanbhag
Thanks Sachin. But I have a different requirement. I do not need the main window handle of the 'current' window. I need the main window handle of a process that I pass or provide as input.
satya
@Sachin, @Dick: From the OP's posts, it doesn't look like s/he's using browser scripting. JScript is a Windows shell scripting language.
Helen
A: 

JScript and Windows Script Host don't have this functionality, and neither does WMI.

If PowerShell is an option for you, then you can use the Process.MainWindowHandle property you mentioned:

(Get-Process notepad).MainWindowHandle

Otherwise, you'll need to find or write an utility (COM object, command-line tool etc) that would provide this functionality, and call this tool from your script.


Edit: So you need to close the window — that's a UI automation task.

Windows Script Host provides very limited UI automation functionality. If you know the window title, you could try using the AppActivate to and SendKeys methods to activate that window and send the Alt+F4 shortcut to it. You can find an example this answer. (The code is in VBScript, but it should give you the idea.) However, this approach isn't reliable.

If you really really don't want to kill the process, the easiest solution is to use some third-party UI automation tool. For example, you could try the free AutoIt tool — I think it should be able to accomplish what you need.


Edit 2: Have you tried recording the closing of the window? You should get a script like this:

Sys.Process("notepad").Window("Notepad", "Untitled - Notepad").Close();

Isn't this what you need?

Helen
Thanks Helen for the reply. I want to close the main window of the process whose name is say, xyz.exe. I do not want to use terminate() method as this will kill the process rather than formally closing the application. It is fine for me if there are other ways to close the application rather than killing or terminating the process.
satya
sorry Helen, I forgot to mention. I am using Test complete tool for closing the window. so I can automate the closing process with the tool. Only thing I need is, to get the title of the window so that I will pass it to the automation tool to close it. As there can be morethan one process exists of same name (each have its main window. eg: iexplore.exe) i want to somehow extract the title of the window by sending the process name as input.
satya
@rainbow365 Note: You should always include details like this in your original questions. People aren't telepathists and without knowing the details won't be able to help you.
Helen
+1  A: 

For a native win32 application, there is no such thing as a "main window". A process can have no windows at all, or several top level "main" windows.

Anders
A: 

Well once i had to write a add-in for Outlook. My boss wants a splash-screen to appear when Outlook loads. But Outlook window goes over the splash. After a lot of search i found FindWindow http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k%28FINDWINDOW%29%3bk%28TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV4.0%22%29%3bk%28DevLang-CSHARP%29&rd=true this is help for it . This function finds window based on window caption and window class name. I p-invoked it and used it from C#. If you can use this function through JScript I think it could do the job for you. (I used Spy++ for finding lpClassName parameter)

ikirachen