How to get the main window handle of a process using JScript? - windows

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)

Related

Command-line to show a file's properties?

I want to write an app that displays the default Windows Properties sheet, I can't find much info on doing it the normal programmatic way, so I'm thinking maybe shell out to a command-line call; does anyone know the Windows command-line to call up a file's property/details window? Not having any luck searching for it.
This window here:
In a general sense, there are two methods for going about this. How you specifically accomplish it (i.e. PowerShell, C#, etc) is up to you.
Call the ShellExecute function on the file with the PROPERTIES verb.
Call the SHObjectProperties function on the file.
Examples of both can be found here in AutoIt: http://www.autoitscript.com/forum/topic/118673-open-a-files-properties-window/
It appears that there IS no direct command-line call that can do this, so I whipped up a quick little VBScript to do it, since it appeared to be the quickest and easiest way to achieve the end result (at least for my specific need).
The VBScript simply sends the Windows Shortcut Alt + Enter; so, when a file is highlighted it'll automatically use that file as the one to send the shortcut to.
For anyone else who may want to do the same, just paste the following into a new text file and save it as a .vbs file:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "%{ENTER}"
For anyone interested in an even better solution, I ended up writing a small command-line app in VB.NET to do this, here's a link to it's full info + the source code: https://geekdrop.com/x/props

Applications hook

I would like to make some hook in windows programs. For instance, Vodafone application have some MSN like popup that can't be hiddent. So, in that way, I would like to create my option to hide or not this popups. Can anybody tell me some tutorials (basic first) how to do that?
Thanks in advice.
You have to create system wide windows hook with SetWindowsHookEx with first param WH_CBT. You can hook on HCBT_CREATEWND event and return non zero value and window will be destroyed, but first you have to somehow recognize that this is right window, maybe according to window title or something inside that title.
Another thing is that you can't use managed code and C# for system wide hooks, since you have to make unmanaged dll in witch hook will reside. So you have to use C++ or Delphi, and if you are not experienced in win32 programming that would probably be very complicated task.

How to handle drag-and-drop to a Win32 application icon?

I have to update a Win32 application in order to handle the drag-and-drop of files over the icon of the executable.
I am not sure about how to proceed. A few researches led me to considering the "WM_DROPFILES" message, but MSDN syas it is "Sent when the user drops a file on the window", while I don't want to open a window.
Think of a command line tool "MyProgram.exe" : if I drag "MyFile.file" on the windows icon "MyProgram" in the desktop, I would like it to execute the same way as it would do when typing ">MyProgram MyFile.file" in the command prompt.
Any idea how to achieve this result ?
While it is true that apps get this for free by parsing the command line, there is a shell interface called IDropTarget you can implement if you need more control. See MSDN and this blog entry for more details.
Windows does this for you automatically. Any program foo.exe accepts drags of any file.
Martyn

Command line in Visual Studio - how to close popup window?

I have situation like below.
I'm running some command and then I get a popup with Y/N answer. Is there a possibility to force answer Yes and automatically close the window through command line?
If you want to click a button in an external window, you'll need to hook the window with the button exposed. You can accomplish this by grabbing the window handle via FindWindow, finding the child button, and sending a BM_CLICK with an API call via SendMessage.
you mean a command window within VS ? I don't think there's anything generic, you could run all sorts of things, depends on what your command is - and how much control over it you have - maybe some example of what you're trying to do could help. 'picking' the window will work but depends again on what you're doing, how custom it is etc.

How to have SlickEdit control another window?

I want to have SlickEdit control another window.
I have an idea of how this could be done using some window’s apis but I am not sure how to implement this in SlickEdit. I am assuming Slick-C (SlickEdit's macro language) would be used. I have done some limited coding in Slick-C but I am not sure if window apis can be run.
Here is what I want done using windows API.
BringWindowToTop (This will bring the other window to the top)
SetForegroundWindow
Simulate pressing the F7 in the other window by using SendKey.
SendKey is a method in WScript.Shell .
It sounds pretty straight forward, all I need to know is how to do it in SlickEdit.
Update:
I pretty much used the concept jussij outlined but in a language I am familar with called PL/B. I already had most of these APIs working for another process, so it was pretty easy to create this new program and had SlickEdit shell out to run the program.
Here is what was needed inside of SlickEdit:
_command BenShellSAV1P198() name_info(','VSARG2_MACRO|VSARG2_MARK|VSARG2_REQUIRES_MDI_EDITORCTL|VSARG2_READ_ONLY)
{
save_all();
shell("T:\\Sunbelt\\CODE\\plbwin.exe -h -i PlbBenTDSm.INI SAV1P198.PLC","N");
}
Then I bound that macro to a hot key and it all works just fine.
SendKey is a method in WScript.Shell.
I am pretty sure everthing you describe can be done at the WScript level.
So you could write a script that does all the work and once you have it working, just add it as a tool to SlickEdit by running the script using the cscript.exe executable.

Resources