How to find the window Title of Active(foreground) window using WindowScript Host

后端 未结 4 596
温柔的废话
温柔的废话 2020-11-30 11:56

I want to find the title of the window which is currently active(having focus) using Window Script Host(WSH) because I want my WSH script to Sendkeys only If the desired win

4条回答
  •  攒了一身酷
    2020-11-30 12:25

    The batch file has to run without errors.

    The first command makes the dll from the cls file. It will say Compilation Sucessfull. It expects the files to be in a folder called wso on your desktop.

    The second command registers it per machine. You must be an admin to do this. If you are not an admin then you must generate a reg file, and change all HKEY_CURRENT_ROOT to HKEY_CURRENT_USER\Software\Classes.

    To generate a 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.

    Here is the script running showing hwnd, PID, and window title (and error code). Note how when script starts there is no active window for about two seconds (windows is waiting for your program to create one for it to make active. It only waits 2 seconds). Usually at program starts, but also other times, for short periods there will be no active window. You must trap this. Here's 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 run with CScript in a command prompt.

    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's looks like you've been hit by a notepad bug when pasting from web pages from the funny spacing of the object name in the error message.

    If using notepad to write it copy and paste into wordpad to check line breaks. Notepad totally ignores and hides carriage returns but other programs don't. Notepad only looks for line feeds. If coping from browser based documentation such as web pages and help systems sometimes stray carriage returns get invisibly inserted in notepad.

提交回复
热议问题