English 中文(简体)
How to get the main window handle of a process using JScript?
原标题:

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?

问题回答

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.

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?

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.

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)





相关问题
What does it mean "to write a web service"?

I just asked a question about whether it was possible to write a web-page-checking code and run it from free web server, and one supporter answered and said that it was possible only if I run "a web ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Why does Scala create a ~/tmp directory when I run a script?

When I execute a Scala script from the command line, a directory named "tmp" is created in my home directory. It is always empty, so I simply deleted it without any apparent problem. Of course, when I ...

Ivy, ant and start scripts

I have a project that uses ant to build and ivy for dependencies. I would like to generate the start scripts for my project, with the classpath, based on the dependencies configured in Ivy, ...

热门标签