Jump to content

Access to Command Window Contents


Recommended Posts

I am accessing the contents of the command window by scripting the "select all" and "copy" functionality. My method works **BUT** I want to elimate the mouse click altogether. I understand there is a menu selection function but I do not know how I would use it in this context. Is there a way to access the menu in the command window without using a mouse click. Also is there a way to copy the contents from the window outside of what I am doing?

CAVEAT... I **cannot** use a method to pipe or communicate such as command the StdoutRead and StdinWrite. The application I am driving as a subprocess does not support it, which is why I am interacting with the command window. Here is code I have written that works.

both "copyCmdWindowWithKeyboard" and "copyCmdWindowWithMouse" work but I really need to eliminate the use of MouseClick. I could use a better method if you have one for getting the text from the command window.

Anything ideas?

Thank you,

W.

#include <Clipboard.au3>

Func setScriptPrefs()

Opt('WinWaitDelay',500)

Opt('WinDetectHiddenText',1)

Opt('MouseCoordMode',0)

Opt("WinTitleMatchMode", 2) ; ABSOLUTELY NECESSARY FOR WINDOW TITLE MATCHING

Opt("SendKeyDelay",1) ; Alters the length of the brief pause in between sent keystrokes

EndFunc

Func activateWind( $title )

If ( BitAND( WinGetState( $title ), 8 ) ) Then ; window is active

Return( True )

Else

WinActivate($title) ;if the window (by title) is not active then make it the active window

return( WinWaitActive($title,"", 5) ) ; return the window handle (non-zero) if the window exists and is active

EndIf

EndFunc

Func copyCmdWindowWithMouse( $title )

Local $mouseBtn = "primary" ; "left"... the primary mouse button may be swapped so use "primary" instead of left

;BlockInput(1) ; disable user keyboard and mouse input

activateWind( $title )

MouseClick($mouseBtn,13,14,1,0)

MouseClick($mouseBtn,67,154,1,0)

MouseClick($mouseBtn,201,201,1,0)

sendToWindow($title, "{ENTER}")

;BlockInput(0) ;re-enable keyboard and mouse input

EndFunc

Func copyCmdWindowWithKeyboard( $title )

Local $mouseBtn = "primary" ; "left"... the primary mouse button may be swapped so use "primary" instead of left

;BlockInput(1) ; disable user keyboard and mouse input

activateWind( $title )

MouseClick($mouseBtn,10,12,1, 0)

sendToWindow($title, "{DOWN 7}{RIGHT}{DOWN 3}{ENTER 2}") ;"{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{RIGHT}{DOWN}{DOWN}{DOWN}{ENTER}{ENTER}"

;BlockInput(0) ;re-enable keyboard and mouse input

EndFunc

Func sendToWindow( $title, $sendStr )

If ( activateWind( $title ) ) Then

Send( $sendStr )

Sleep( 500 ) ; Let some time to pass (allowing for keystrokes to make it to the screen) before returning

Return( True ) ; we sent string to the title window

Else

Return( False ) ; we were not able to send because the title window was not active

EndIf

EndFunc

setScriptPrefs()

$title = "Capture CMD Info"

$sendStr = "This is a test of the emergency broadcast system. This is only a test."

Run(@ComSpec & " /k TITLE "& $title, "c:\", @SW_MAXIMIZE)

sendToWindow( $title, $sendStr )

copyCmdWindowWithKeyboard( $title )

MsgBox(0, "Captured", _ClipBoard_GetData() & @LF)

Link to comment
Share on other sites

Hi,

Welcome to the autoit forum :)

Have you tried the Control* functions to see if the cmd wnd can be manipulated ? (use the AutoIt3Info tool).

Edit: oh and please post your code within [autoit][/autoit] tags ;)

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

AFAIK you won't be able to copy the text without using the Mouse* functions.

This is the best I can do for the Windows' cmd prompt, adapt it for yours :

Opt("WinTitleMatchMode", 2)

Local Const $sCmdWndTitle = "Capture CMD Info"

Local Const $hWnd = WinGetHandle($sCmdWndTitle)
If $hWnd = 0 Then Exit(1)

If WinActive($hWnd) = 0 Then
    WinActivate($hWnd)
    WinWaitActive($hWnd)
EndIf

Local Const $aMgp = MouseGetPos(), $sTitle = WinGetTitle($hWnd), $aWgp = WinGetPos($hWnd)

MouseClick("right", $aWgp[0] + 10, $aWgp[1] + 35, 1, 0)
MouseMove($aMgp[0], $aMgp[1], 0)

Send("{DOWN 4}{ENTER}")

While WinGetTitle($hWnd) = $sTitle
WEnd

Send("{ENTER}")

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

This is expanding on the help file example for "stdoutread".

ConsoleWrite("Note: @ComSpec = " & @ComSpec & @CRLF & @CRLF)
ConsoleWrite("-----------------------------------------------------------" & @LF)

ConsoleWrite("Help on CMD command:-" & @CRLF & @CRLF & _Cmd("cmd.exe /?") & @LF)
ConsoleWrite("-----------------------------------------------------------" & @LF)

ConsoleWrite("List of all DOS commands:-" & @CRLF & @CRLF & _Cmd("Cmd /c help") & @LF)
ConsoleWrite("-----------------------------------------------------------" & @LF)

ConsoleWrite("List of specific DOS command:-" & @CRLF & @CRLF & _Cmd("Cmd /c help findstr") & @LF)
ConsoleWrite("-----------------------------------------------------------" & @LF)


Func _Cmd($sCmd, $sDir = "")
    Local $text = '', $Pid = Run(@ComSpec & " /c " & $sCmd, $sDir, @SW_HIDE, 2 + 4)
    While 1
        $text &= StdoutRead($Pid, False, False)
        If @error Then ExitLoop
        Sleep(10)
    WEnd
    While 1
        $text &= StderrRead($Pid)
        If @error Then ExitLoop
        ConsoleWrite(@LF & "!#------------------------# Error #---------------------------# " & @LF)
    WEnd
    Return $text
EndFunc   ;==>_Cmd

; PowerShell - The updated, new, improved version of command prompt, if present on your computer.
; Replace the "_Cmd" with "_PowerShell" in the above examples for _PowerShell function examples. Plus the following one:-
; ConsoleWrite(_PowerShell("get-help help -full") & @LF)
Func _PowerShell($sCmd, $sDir = "")
    Local $text = '', $Pid = Run('"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" ' & $sCmd, $sDir, @SW_HIDE, 2 + 4)
    While 1
        $text &= StdoutRead($Pid, False, False)
        If @error Then ExitLoop
        Sleep(10)
    WEnd
    While 1
        $text &= StderrRead($Pid)
        If @error Then ExitLoop
        ConsoleWrite(@LF & "!#------------------------# Error #---------------------------# " & @LF)
    WEnd
    Return $text
EndFunc   ;==>_PowerShell
Link to comment
Share on other sites

I was able to copy the contents of the command window without using the mouse, though it was not my ideal solution. Using StdoutRead and StdinWrite were not options because the subprocess that I am running from the command prompt does not support it.

The answer was sendToWindow($title,"!{SPACE}"&"ES{ENTER}")

Thanks to everyone for their help. I would like to have access the menu directly but I can live with this solution.

W.

#include 



Func setScriptPrefs()

Opt('WinWaitDelay',500)

Opt('WinDetectHiddenText',1)

Opt('MouseCoordMode',0)

Opt("WinTitleMatchMode", 2) ; ABSOLUTELY NECESSARY FOR WINDOW TITLE MATCHING

Opt("SendKeyDelay",1) ; Alters the length of the brief pause in between sent keystrokes

EndFunc



Func activateWind( $title )

If ( BitAND( WinGetState( $title ), 8 ) ) Then ; window is active

Return( WinGetHandle ( $title) )

Else

WinActivate($title) ;if the window (by title) is not active then make it the active window

return( WinWaitActive($title,"", 5) ) ; return the window handle (non-zero) if the window exists and is active

EndIf

EndFunc





Func sendToWindow( $title, $sendStr )

If ( activateWind( $title ) ) Then

Send( $sendStr )

Sleep( 500 ) ; Let some time to pass (allowing for keystrokes to make it to the screen) before returning

Return( True ) ; we sent string to the title window

Else

Return( False ) ; we were not able to send because the title window was not active

EndIf

EndFunc



Func copyCommandWindowMenu( $title )

sendToWindow($title,"!{SPACE}"&"ES{ENTER}") ;ALT SPACE selects the command menu, "E" chooses EDIT, "S" chooses SELECT ALL

EndFunc





$title = "Capture CMD Info"

$sendStr = "This is a test of the emergency broadcast system. This is only a test."

Run(@ComSpec & " /k TITLE "& $title, "c:\", @SW_MAXIMIZE)



setScriptPrefs()

sendToWindow( $title, $sendStr ) ; check results

copyCommandWindowMenu( $title ) ; check results



MsgBox(0, "Captured", _ClipBoard_GetData() & @LF)

WinClose($title)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...