Finding an Active Window (Front End) Window Name Using Window Script Host

I want to find the title bar of a window that is currently active (with focus) using Window Script Host (WSH), because I want my WSH Script to be clicked only by the submit button. If the desired window is active.

Note * I am not able to use the alternative to activate the desired window before calling sendkeys.

Any help is appreciated.

+3
source share
4 answers

You can create a COM object using GetForegroundWindow and GetWindowText.

Put the following lines in wso.cls and save the wso folder on your desktop.

Imports System Imports System.Runtime.InteropServices Imports Microsoft.Win32 Namespace WindowScriptingObject <Guid("7448E08D-ED0F-4E23-B528-91937BB41756"), _ InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _ Public Interface _WindowScriptingObject <DispId(1)> Function ActiveWindow() As Integer <DispId(2)> Function WindowText(ByVal hWnd As Integer) As String End Interface <Guid("B146BF9E-78FC-4DB0-ABFE-9FF026B43E4D"), _ ClassInterface(ClassInterfaceType.None), _ ProgId("WindowScriptingObject")> Public Class WindowScriptingObject Implements _WindowScriptingObject Public WindowScriptingObject() Public Declare Auto Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow"() As Integer Public Declare Auto Function GetWindowText Lib "user32.dll" (ByVal hwnd As Int32, <Out()> ByVal lpString As System.Text.StringBuilder, ByVal cch As Int32) As Int32 Public Function ActiveWindow() As Integer Implements _WindowScriptingObject.ActiveWindow ActiveWindow=GetForegroundWindow() End Function Public Function WindowText(hwnd as Integer) As String Implements _WindowScriptingObject.WindowText on error resume next Dim b As New System.Text.StringBuilder(ChrW(0), 512) Dim ret = GetWindowText(hWnd, b, b.Capacity) WindowText = b.tostring End Function End Class End Namespace 

Then create the bat file in the same folder as wso.bat.

 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:library /out:"%userprofile%\desktop\wso\wso.dll" "%userprofile%\desktop\wso\wso.cls" /verbose "C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm" /codebase "%userprofile%\desktop\wso\wso.dll" /tlb:"%userprofile%\desktop\wso\wso.tlb" /v If /i "%cmdcmdline:~0,6%"=="cmd /c" pause 

Use in vbs after running bat file.

 Set wso=CreateObject("WindowScriptingObject") x = wso.ActiveWindow msgbox x, , "vbs" msgbox wso.windowtext(x), , "vbs" 

The GUIDs used here are specific to this project. Do not use them for other purposes.

Additional information on what we do.

http://social.msdn.microsoft.com/Forums/en-US/adcae113-4758-481a-a367-60d5d14d97d6/this-is-how-to-turn-vbs-and-js-files-into-exe- files-from-the-command-line-without-third-party-tools? forum = scripting

If you must complete the installation for each user, use regasm to create a regfile rather than registering it. Then change all HKCR links to HKCU\Software\Classes . Then merge with regedit /s regfile.reg .

To move a file, you need to run Regasm on it in a new location. See the command in the bat file.

It will be posted on the MS website, of course, for accurate historical purposes.

+3
source

Short answer: You cannot. At least not without writing a COM wrapper for the corresponding Windows API calls.

Can't you just use AppActivate and check the result?

 Set oShell = CreateObject("WScript.Shell") If oShell.AppActivate "Untitled - Notepad" Then oShell.SendKeys "Hello, world!" End If 


Long answer: To get the title of the active window, you need to call the GetForegroundWindow() function. VBScript and Windows Script The host does not support Windows API calls, so you need to write a COM wrapper around these functions, which you can then use in your script. Here are some examples:

Get current active window title in C

How to get the title of the current active window using C #?

+2
source

This is an updated version to use. The previous answer is the minimum required for work.

This also replaces the answer here ( appactivate between multiple instances of Internet explorer ), since it does not work for Windows 7 and later due to the fact that sendmail is a reserved name on these OSs.

 Imports System Imports System.Runtime.InteropServices Imports Microsoft.Win32 Namespace WindowScriptingObject <Guid("7448E08D-ED0F-4E23-B528-91937BB41756"), _ InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _ Public Interface _WindowScriptingObject <DispId(1)> Function ActiveWindow() As UInteger <DispId(2)> Function WindowText(ByVal hWnd As UInteger) As String <DispId(3)> Function WindowPID(ByVal hWnd As UInteger) As UInteger End Interface <Guid("B146BF9E-78FC-4DB0-ABFE-9FF026B43E4D"), _ ClassInterface(ClassInterfaceType.None), _ ProgId("WindowScriptingObject")> Public Class WindowScriptingObject Implements _WindowScriptingObject Public WindowScriptingObject() Public Declare Auto Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow"() As UInteger Public Declare Auto Function GetWindowText Lib "user32.dll" (ByVal hwnd As Int32, <Out()> ByVal lpString As System.Text.StringBuilder, ByVal cch As Int32) As Int32 Public Declare Auto Function GetWindowThreadProcessId Lib "user32" Alias "GetWindowThreadProcessId" (ByVal hwnd As UInteger, ByRef lpdwProcessId As UInteger) As UInteger Public Function ActiveWindow() As UInteger Implements _WindowScriptingObject.ActiveWindow ActiveWindow = GetForegroundWindow() If err.lastdllerror <> 0 then Dim tmp as uinteger = err.lastdllerror and &h80070000 err.raise(tmp, "WindowSystemObject.GetForegroundWindow", "Type net helpmsg " & err.lastdllerror & " in a command prompt for help") Exit Function End If End Function Public Function WindowText(hwnd as UInteger) As String Implements _WindowScriptingObject.WindowText Dim b As New System.Text.StringBuilder(ChrW(0), 512) Dim ret as uinteger = GetWindowText(hWnd, b, b.Capacity) If err.lastdllerror <> 0 then Dim tmp as uinteger = err.lastdllerror and &h80070000 WindowText = "" err.raise(tmp, "WindowSystemObject.GetWindowText", "Type net helpmsg " & err.lastdllerror & " in a command prompt for help") Exit Function End If WindowText = b.tostring End Function Public Function WindowPID(HWnd as UInteger) As UInteger Implements _WindowScriptingObject.WindowPID Dim X as UInteger Dim M as UInteger = 1 X=GetWindowThreadProcessID(HWnd,M) If err.lastdllerror <> 0 then Dim tmp as uinteger = err.lastdllerror and &h80070000 WindowPID = 0 err.raise(tmp, "WindowSystemObject.GetWindowThreadProcessID", "Type net helpmsg " & err.lastdllerror & " in a command prompt for help") Exit Function End If WindowPID = M End Function End Class End Namespace 
+1
source

The batch file should work without errors.

The first command makes the dll from the cls file. He will say "Compilation Sucessfull." He expects the files to be in the wso folder on the desktop.

The second team registers it on the car. You must be an administrator to do this. If you are not an administrator, you must create a reg file and change all HKEY_CURRENT_ROOT to HKEY_CURRENT_USER \ Software \ Classes.

To create regfile

 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm" /regfile:"%userprofile%\desktop\wso\wso.reg" "%userprofile%\desktop\wso\wso.dll" /v 

After editing wso.reg merge it with

 regedit /m "%userprofile%\desktop\wso\wso.reg" 

And you need to read the results of the commands.

Below is an example script showing hwnd, PID and window title (and error code). Please note that when the script starts, there is no active window for about two seconds (windows wait until your program creates one so that it activates, it only waits 2 seconds). Usually, when the program starts, but also in other cases, there will be no active window for short periods. You have to trap. Here is a script that does.

 On error resume next Set wso=CreateObject("WindowScriptingObject") Do x = wso.ActiveWindow wscript.echo x wscript.echo wso.windowtext(x) wscript.echo (err.number) err.clear wscript.echo wso.windowpid(x) wscript.echo (err.number) err.clear wscript.sleep 1000 Loop 

And this is what it looks like when launched from CScript on the command line.

 C:\Users\User>cscript "C:\Users\User\Desktop\ActiveWindow.vbs" Microsoft (R) Windows Script Host Version 5.7 Copyright (C) Microsoft Corporation. All rights reserved. -2147024809 -2147024809 3344366 Administrator: Command Prompt - cscript "C:\Users\User\Desktop\ActiveWin dow.vbs" 0 972 0 3344366 Administrator: Command Prompt - cscript "C:\Users\User\Desktop\ActiveWin dow.vbs" 0 972 0 3344366 1312854 vbscript - How to find the window Title of Active(foreground) window using Windo w Script Host - - Windows Internet Explorer 0 4724 0 1312854 vbscript - How to find the window Title of Active(foreground) window using Windo w Script Host - - Windows Internet Explorer 0 4724 0 3344366 Administrator: Command Prompt - cscript "C:\Users\User\Desktop\ActiveWin dow.vbs" 0 972 0 ^C C:\Users\User> 

---- EDIT ----

It looks like you got a notepad error when pasting from web pages from a ridiculous interval of the object name in the error message.

If you use notepad to copy and paste it into a text block to check line breaks. Notepad completely ignores and hides carriage returns, but other programs do not. Notepad is only looking for lines. If copying from browser-based documentation, such as web pages and help systems, sometimes wandering carriage returns become imperceptibly pasted into a notebook.

0
source

Source: https://habr.com/ru/post/1242305/


All Articles