Jump to content

controlsend to multple windows


Recommended Posts

Is it possible to ControlSend to different instances of the same application (and distinguish which to send to) - something simple.. say a script that can write 'hello world' to two blank notepads via hotkeys, though never needing to take focus nor dependant on any cordinate x,y location... looking at the help fileand using Window Info... it seems every instance of notepad looks exactly the same and even when running the sample script

ControlSend("Untitled", "", "Edit1", "This is a line of text in the notepad window")
it always writes to the notpad that happened to be last active.. i was hoping the "Instance" under the Control tab of Window Info would be unique per instance of Notepad running thus maybe something to key off of however this doesn't seem to be the case. any ideas?

Don't let that status fool you, I am no advanced memeber!

Link to comment
Share on other sites

  • Moderators

WinList("Untitled")

Loop through list and ControlSend()

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

WinList("Untitled")

Loop through list and ControlSend()

thx thx i guess that will work. at first i was thinking it would be to flaky to simply grab all matches and send blindly however if I use autoit to open each notepad then i should be able to be able to designate and keep track of each individual process. Thanks now i have something to work with.

Don't let that status fool you, I am no advanced memeber!

Link to comment
Share on other sites

  • Moderators

thx thx i guess that will work. at first i was thinking it would be to flaky to simply grab all matches and send blindly however if I use autoit to open each notepad then i should be able to be able to designate and keep track of each individual process. Thanks now i have something to work with.

You're not going to send them blindly... be sure to use the handle part of the return and not the title part. Handles are unique to each window.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

You're not going to send them blindly... be sure to use the handle part of the return and not the title part. Handles are unique to each window.

i can see how the handle is unique but failing to apply it to the controlsend.. can the HWnd be use directly or am i missing a step >.<

Run("notepad.exe")
WinWaitActive("Untitled - Notepad")
Send("This is notepad 1")
$Notepad1 = WinList ("Untitled - Notepad")
MsgBox(0, "Window: ", "Title1=" & $Notepad1[1][0] & @LF & "Handle1=" & $Notepad1[1][1])
Broadcast($Notepad1)

Run("notepad.exe")
WinWaitActive("Untitled - Notepad")
Send("This is notepad 2")
$Notepad2 = WinList ("Untitled - Notepad")
MsgBox(0, "Window: ", "Title1=" & $Notepad2[1][0] & @LF & "Handle1=" & $Notepad2[1][1] & @CRLF & "Title2=" & $Notepad2[2][0] & @LF & "Handle2=" & $Notepad2[2][1]& @CRLF)
Broadcast($Notepad2)

Run("notepad.exe")
WinWaitActive("Untitled - Notepad")
Send("This is notepad 3")
$Notepad3 = WinList ("Untitled - Notepad")
MsgBox(0, "Window: ", "Title1=" & $Notepad3[1][0] & @LF & "Handle1=" & $Notepad3[1][1] & @CRLF & "Title2=" & $Notepad3[2][0] & @LF & "Handle2=" & $Notepad3[2][1] & @CRLF & "Title3=" & $Notepad3[3][0] & @LF & "Handle3=" & $Notepad3[3][1])
Broadcast($Notepad3)

exit

; func Broadcast to All
func Broadcast($window)
    For $i = 1 to $window[0][0]
        ControlSend ( $window[$i][0],"",$window[$i][1],"HWND: " & $window[$i][1] & @CRLF)
    Next
EndFunc

Don't let that status fool you, I am no advanced memeber!

Link to comment
Share on other sites

or to be more specific i can see a way to do it with making the window active IE..

Local $hWnd = WinGetHandle("Untitled - Notepad")
Local $sHWND = String($hWnd)  ; Convert to a string
WinSetState(HWnd($sHWND), "", @SW_MAXIMIZE)

however i need to be able to send data without needing the window to be in focus..

EDIT:

function from my example testing above... really really flaky:

func Broadcast($window)
    For $i = 1 to $window[0][0]
        Local $sHWND = String($window[$i][1])  ; Convert to a string
        ControlSend (HWnd($sHWND),"","","HWND: " & $window[$i][1] & @CRLF)
    Next
EndFunc

doesn't give an error but I would expect every time the function runs it would write the HWnd to each window that has been opened thus the first notepad would have it written 3 times... 2nd notepad would have its hwnd written twice... third once... and hopefully / idealy irrelevant if the widows are made active moved minimized etc during the process

Edited by zhenyalix

Don't let that status fool you, I am no advanced memeber!

Link to comment
Share on other sites

the autoit gods finally had mecy on me.... not sure what / why it started working but this seems to do it:

#AutoIt3Wrapper_run_debug_mode=Y

Global $Notepad1[3][3]
Global $Notepad2[3][3]
Global $Notepad3[3][3]

Run("notepad.exe")
WinWaitActive("Untitled - Notepad")
Send("This is notepad 1" & @CRLF)
$Notepad1 = WinList ("Untitled - Notepad")
MsgBox(0, "Window: ", "Title1=" & $Notepad1[1][0] & @LF & "Handle1=" & $Notepad1[1][1] & @CR)
Broadcast($Notepad1)

Run("notepad.exe")
WinWaitActive("Untitled - Notepad")
Send("This is notepad 2" & @CRLF)
$Notepad2 = WinList ("Untitled - Notepad")
MsgBox(0, "Window: ", "Title1=" & $Notepad2[1][0] & @LF & "Handle1=" & $Notepad2[1][1] & @CRLF & "Title2=" & $Notepad2[2][0] & @LF & "Handle2=" & $Notepad2[2][1]& @CRLF)
Broadcast($Notepad2)

Run("notepad.exe")
WinWaitActive("Untitled - Notepad")
Send("This is notepad 3" & @CRLF)
$Notepad3 = WinList ("Untitled - Notepad")
MsgBox(0, "Window: ", "Title1=" & $Notepad3[1][0] & @LF & "Handle1=" & $Notepad3[1][1] & @CRLF & "Title2=" & $Notepad3[2][0] & @LF & "Handle2=" & $Notepad3[2][1] & @CRLF & "Title3=" & $Notepad3[3][0] & @LF & "Handle3=" & $Notepad3[3][1])
Broadcast($Notepad3)

exit

; func Broadcast to All
func Broadcast($window)
    For $i = 1 to $window[0][0]
        MsgBox(0, "Window: ", "Trying to write to window " & $window[$i][0] & @LF & "Handle1=" & $window[$i][1] & @CRLF & "1: " & $Notepad1[1][1]  & @CRLF & "2: " & $Notepad2[1][1]  & @CRLF & "3: " & $Notepad3[1][1])
        ControlSend ($window[$i][1],"","Edit1","HWND: " & $window[$i][1] & @CR)
    Next
EndFunc

Don't let that status fool you, I am no advanced memeber!

Link to comment
Share on other sites

  • Moderators

I get the feeling you are trying to do:

Run("notepad.exe")
Run("notepad.exe")
Run("notepad.exe")
Sleep(2000)
$count = _ControlSendMultiWin("Untitled - Notepad", "", "Edit1", "Sending Exact Messag")
MsgBox(64, "Info", "The message was sent to: " & $count & " windows.")

Func _ControlSendMultiWin($sTitle, $sText, $vCtrl, $sSend, $iFlag = 0)
    Local $aWList = WinList($sTitle), $nCount = 0;$nCount will return the number of windows it sent to
    For $iCC = 1 To $aWList[0][0]
        $nCount += ControlSend($aWList[$iCC][1], $sText, $vCtrl, $sSend, $iFlag)
    Next
    Return $nCount
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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...