Jump to content

ControlSend a variable to a program


Recommended Posts

Hi everyone,

I am totally new to autoit.

I need help trying to pass a variable with the controlsend function. My program will ask for an input from the user. For example, name, then it sends it to another program like word or notepad. I understand we can use the send command but I would prefer to use the controlsend so that other windows can be accessed while the program is running.

Yes, I have done a search in this forum. All the results or suggestions do not work. I have tried all the possible variants and nothing seems to work. It always sends the actual variable name only and not the actual user input. Here are parts of my script.

The gui input:

GUICtrlCreateGroup("What is your name?", 15, 75, 130, 55)

$username = GUICtrlCreateInput ("", 30, 95, 100, 20)

The send function works fine but the window has to be up front and active

Send (GUICtrlRead($username), 1)

The control send variants from my searches on this forum and other things I have tried.

ControlSend ("[CLASS:Notepad]", "", "", GUICtrlRead($username))

ControlSend ("[CLASS:Notepad]", "", "", "GUICtrlRead($username))

ControlSend ("[CLASS:Notepad]", "", "", "$username", 1)

ControlSend ("[CLASS:Notepad]", "", "", "$username", 0)

ControlSend("[CLASS:Notepad]", '', '', "{" & $username & " Down}")

All these options send the following text: $username

If in the GUI, the user puts in JohnDoe, I would like JohnDoe to be sent and not $username

I spent a good 2-3 hours on this and would appreciate any help. Thanks!

Lastly, how do you post your codes in a code box? Sorry, im new...

Link to comment
Share on other sites

Hi LearnersPermit,

Welcome to scripting in AutoIt! ;) and the forums!!

You may want to check out the Wiki, its full of great tutorials and stuff to get you proficient in AutoIt, also check out the help-file (press F1 from SciTE editor).

As for sending the text, this is how its done, note the "Edit1" parameter.

ControlSend("[CLASS:Notepad]", "", "Edit1", GUICtrlRead($username))
You were so close :), your mistake was that you left out the Control ID of the control with which you wanted to interact.

Oh and showing how to use AutoIt tags ;)

Also, you might want to take a look at the forum rules its a short read.

Happy coding :)

smartee

Link to comment
Share on other sites

Smartee

Thanks for the quick reply. You are right it does work. I made a simple script and it works perfectly. Oh, and it even works without the "Edit1" control ID also.

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
$Running = 0
HotKeySet("{F1}", "start")
HotKeySet("{ESC}", "quit")
Func start()
    $Running = 1
EndFunc
Func stop()
    $Running = 0
EndFunc
Func quit()
    Exit
EndFunc
$main_window = GUICreate("Test Application", 300, 80)
GUICtrlCreateGroup("First Name:", 15, 15, 130, 55)
$fname = GUICtrlCreateInput ("", 30,  35, 100, 20)
GUICtrlCreateGroup("Last Name:", 165, 15, 130, 55)
$lname = GUICtrlCreateInput ("", 180, 35, 100, 20)
GUISetState(@SW_SHOW, $main_window)
While 1
   If $Running Then
Send ("#r")
sleep (200)
Send ("{DEL}")
sleep (200)
send ("notepad.exe")
sleep (200)
send ("{ENTER}")
sleep (1500)
ControlSend("[CLASS:Notepad]", "", "", GUICtrlRead($fname))
send ("{ENTER}")
ControlSend("[CLASS:Notepad]", "", "Edit1", GUICtrlRead($lname))
send ("{ENTER}")
   EndIf
stop()
WEnd

I must have an error somewhere in my main script or maybe have too many select case statements. The scipt tries to pull the variable from a location nested in 3 deep. The script keeps sending a "0". I'll just have to move some stuff around. But, thanks for the help!

Edited by LearnersPermit
Link to comment
Share on other sites

Some tips,

1. Rather than automating the windows Run box, try using the ShellExecute function

2. Instead of sleeping a little to wait on windows to exist, try functions such as WinWait and WinExists it makes more robust code.

Here is a quick demo :)

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$Form1 = GUICreate("ControlSend Demo", 300, 107)
$Input1 = GUICtrlCreateInput("Username", 10, 10, 280, 21)
$Input2 = GUICtrlCreateInput("Password", 10, 41, 280, 21)
$Button1 = GUICtrlCreateButton("ControlSend", 170, 72, 120, 25)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _Button1()
    EndSwitch
WEnd

Func _Button1()
    Local $sUsername = GUICtrlRead($Input1)
    Local $sPassword = GUICtrlRead($Input2)

    If Not WinExists("[CLASS:Notepad]") Then
        If Not ShellExecute("notepad.exe") Then
            MsgBox(48, "Warning", "Could not launch notepad.")
        EndIf
    EndIf

    If WinWait("[CLASS:Notepad]", "", 5) Then
        ControlSend("[CLASS:Notepad]", "", "[CLASS:Edit; INSTANCE:1]", "Username: " & $sUsername & @CRLF & "Password: " & $sPassword & @CRLF)
    Else
        MsgBox(48, "Warning", "Could not find notepad window.")
    EndIf
EndFunc   ;==>_Button1
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...