Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

How to put a text into a textbox in an other program?



  • Please log in to reply
16 replies to this topic
wuc
  • Members
  • 51 posts
  • Last active: Feb 18 2015 07:06 PM
  • Joined: 05 Oct 2013

Hi everyone,

 

I think I've looked prettty precisely for a topic that will help me, but cause I found nothing, here's my problem:

 

Is it possible to create a script in Autohotkey, that will be sending a text puted in an Autohotkey textbox to a textbox from an other program? Is it possible to determine, where should be this text puted in?

 

Thats what I need to do:

 

1. Open a program made in AHK (textbox with an RUN button, etc)

2. Put a text into the textbox

3. Hit RUN button (an other program will be opened)

4. The value from the textbox should be sended into a defined textbox in the opened program

 

Thanks in advance for any help!



lifeweaver
  • Members
  • 868 posts
  • Last active: Jan 25 2016 01:29 AM
  • Joined: 04 Mar 2013

Hi wuc,

 

This is indeed possible, you will need to use a few of the built in commands in AutoHotkey.

 

Example using Notepad: (Use Alt + z to active this hotkey)

!z::
Gui, Destroy ; If you try to use this hotkey more than once and don't have this line it will complain.
Gui, Add, Edit, vEdit1, ; Adds an Edit box(textbox) with the name 'Edit1'
Gui, Add, Button, Default gRunBtn, Run ;Adds a button that when clicked  runs the GoSub 'RunBtn'.
Gui, Show,,Run!!! ; Shows the gui
return

RunBtn: ;GoSub that clicking the 'Run' button starts.
Run, notepad.exe ; Uses the AutoHotkey 'Run' command to start notepad.exe.
WinWaitActive, Untitled - Notepad,,2 ;Uses the AutoHotkey 'WinWaitActive' command to wait for 2 seconds for the notepad.exe window to appear
if errorLevel ; If notepad didn't become active in two seconds this will run the below command.
  WinActivate, Untitled - Notepad ; Activates the notepad window using it's title, 'Untitled - Notepad'

Gui, Submit, nohide ; This line 'submits' the gui so the 'Edit1' variable holds the text you entered in the Edit box earlier.
Send %Edit1% ; Uses the AutoHotkey command 'Send' to "Send" the value of the variable 'Edit1' to the active window(notepad in this case).
return

For your case you might need to add a step before you send the value depending on what the default 'control' is on your gui.


My script runs in A_AhkVersion:=1.1.22.07, get the latest version at http://ahkscript.org/download/

Check out this AutoHotkey tutorial: http://ahkscript.git...o/AHK_Tutorial/

Read the documentation: http://ahkscript.org.../AutoHotkey.htm


wuc
  • Members
  • 51 posts
  • Last active: Feb 18 2015 07:06 PM
  • Joined: 05 Oct 2013

Thanks a lot for the quick response, lifeweaver!

 

How about sending a text to a textbox, when I have few textboxes in my gui and none of them is active? Is it possible to "decide" (by AHK), where should be this Value puted in?



lifeweaver
  • Members
  • 868 posts
  • Last active: Jan 25 2016 01:29 AM
  • Joined: 04 Mar 2013

Sounds like you want to use the AutoHotkey command 'ControlSend' this 'lets you send keystrokes to a window or control'.  You will need to use the built-in 'Windows Spy' tool to figure out the 'ClassNN' of the control.

 

For example the notepad's text entry area shows up as 'Edit1' when using the Windows Spy tool.

>>>>>>>>>( Now Under Mouse Cursor )<<<<<<<<
ClassNN:    Edit1
Text:    
Color:    0xF0F0F0  (Blue=F0 Green=F0 Red=F0)

 

So a command to send text to a control might look like:

ControlSend, Edit1, This is a line of text in the notepad window., Untitled - Notepad

This sends the text 'This is a line of text in the notepad window.' to a control 'Edit1' in a window with the title 'Untitled - Notepad'.

 

Note: If you use ControlSend the end window doesn't have to be the 'active' window.

 

So you could change the above code to this if you wanted:

!z::
Gui, Destroy
Gui, Add, Edit, vEdit1,
Gui, Add, Button, Default gRunBtn, Run
Gui, Show,,Run!!!
return

RunBtn:
Run, notepad.exe
WinWait, Untitled - Notepad,,2 ; Waits up to two seconds for the window to exist.
Gui, Submit, nohide
ControlSend, Edit1, %Edit1%, Untitled - Notepad
return

My script runs in A_AhkVersion:=1.1.22.07, get the latest version at http://ahkscript.org/download/

Check out this AutoHotkey tutorial: http://ahkscript.git...o/AHK_Tutorial/

Read the documentation: http://ahkscript.org.../AutoHotkey.htm


wuc
  • Members
  • 51 posts
  • Last active: Feb 18 2015 07:06 PM
  • Joined: 05 Oct 2013

It seems to work fine for me (I'll test it more specificaly in a few days) but in Your example this script is only opening a new Notepad window, without puting any value into.

 

Is it because on non english version of Windows maybe? As far as I looked, the Class from the Notepad window is obviously the same, only the Untitled new document has a different name.

 

Any ideas?



lifeweaver
  • Members
  • 868 posts
  • Last active: Jan 25 2016 01:29 AM
  • Joined: 04 Mar 2013

I have no knowledge of AutoHotkey and how it works with non-English versions of Windows.

 

However, as an alternative to using the actual title you might try replacing:

ControlSend, Edit1, %Edit1%, Untitled - Notepad

with

ControlSend, Edit1, %Edit1%, ahk_class Notepad

This uses the 'ahk_class' of the notepad window, in this case 'Notepad', to identify the targeted window, I don't know if the result will be any different, however it's worth a try.  To learn more about 'ahk_class' and similar AutoHotkey constructs look here.


My script runs in A_AhkVersion:=1.1.22.07, get the latest version at http://ahkscript.org/download/

Check out this AutoHotkey tutorial: http://ahkscript.git...o/AHK_Tutorial/

Read the documentation: http://ahkscript.org.../AutoHotkey.htm


wuc
  • Members
  • 51 posts
  • Last active: Feb 18 2015 07:06 PM
  • Joined: 05 Oct 2013

Thanks a lot lifeweaver for Your huuuuuge help!

 

I have another question - is it possible to check, if notepad is already opened, and when I change the value in the textbox and hit RUN, the values will simply change in the same instance of notepad window (without opening a new notepad window)? Briefly - is there any possibility to check if program is already opened and when yes, just change the values?



lifeweaver
  • Members
  • 868 posts
  • Last active: Jan 25 2016 01:29 AM
  • Joined: 04 Mar 2013
✓  Best Answer

To check if a window exists use IfWinExist.

 

To answer your second question - use ControlSetText this sets the text of the control so it doesn't matter if it had a value or not.

 

Example code:

!z::
Gui, Destroy
Gui, Add, Edit, vEdit1,
Gui, Add, Button, Default gRunBtn, Run
Gui, Show,,Run!!!
return

RunBtn:
Gui, Submit, nohide
IfWinNotExist, ahk_class Notepad ; Only do the steps in the bracket if notepad doesn't exist.
{
  Run, notepad.exe
  WinWait, Untitled - Notepad,,2 ; Waits upto two seconds for the window to exist.
}
ControlSetText, Edit1, %Edit1%, ahk_class Notepad
return

My script runs in A_AhkVersion:=1.1.22.07, get the latest version at http://ahkscript.org/download/

Check out this AutoHotkey tutorial: http://ahkscript.git...o/AHK_Tutorial/

Read the documentation: http://ahkscript.org.../AutoHotkey.htm


wuc
  • Members
  • 51 posts
  • Last active: Feb 18 2015 07:06 PM
  • Joined: 05 Oct 2013

One more problem - I have no clue, how to use ControlSetText by sending a value from one textbox to another textbox using hwnd handle. 

 

I would be really greatful for helping me!



lifeweaver
  • Members
  • 868 posts
  • Last active: Jan 25 2016 01:29 AM
  • Joined: 04 Mar 2013

To refer to a window with it's hwnd instead of ahk_class or title use:

ahk_id your_Hwnd_here

I really recommend you take a look at a few of the links I've provided to save yourself some time, the line where I had 'AutoHotkey constructs look here.' in an above post has this info.

 

To use ahk_id in an actual script it might look like:

ControlSetText, Edit1, %Edit1%, ahk_id 555555

My script runs in A_AhkVersion:=1.1.22.07, get the latest version at http://ahkscript.org/download/

Check out this AutoHotkey tutorial: http://ahkscript.git...o/AHK_Tutorial/

Read the documentation: http://ahkscript.org.../AutoHotkey.htm


Harry Binswanger
  • Members
  • 85 posts
  • Last active: Oct 27 2016 09:33 PM
  • Joined: 26 Aug 2010

My problem seems to be similar. I want to put a person's name into a WordPress text field (for "author"). I can't find in Window Spy any unique identifier of that particular field, and there are many on the WordPress page I'm working with. I can do a ^f for the title of the text box in question, and it's found, but that doesn't allow me to then get inside that textbox to enter the author's name.

 

As if that weren't enough, the "author" text box has a dropdown that will jump to the entry for the first name, but the space in the name makes it get the wrong person. But first, I just want to be able to get to the dropdown containing all the author names.

 

Again, Window Spy shows no change (not even of cursor position!) relating to which text box I'm (manually) clicked into.

 

Thanks for any help.



bruno
  • Members
  • 635 posts
  • Last active: Nov 04 2015 02:26 PM
  • Joined: 07 Mar 2011

i entered this is a test! (with the bang) in the test box, but what it sent to the notepad was this is a test (without the bang). :/



Harry Binswanger
  • Members
  • 85 posts
  • Last active: Oct 27 2016 09:33 PM
  • Joined: 26 Aug 2010

I didn't get a response, because maybe my problem isn't similar after all.

 

Am I missing some general command for locating and clicking on a button with specific text (but not a unique color)--and for locating a specific text field to Send my text to, when there are several text fields? Window Spy doesn't shows the same ClassNN for all (Chrome_RenderWidgetHostHWND1). I can't rely on it being done by x-y position on the page, because that varies somewhat for various reasons.

 

Thank you.



Harry Binswanger
  • Members
  • 85 posts
  • Last active: Oct 27 2016 09:33 PM
  • Joined: 26 Aug 2010

I didn't get a response, because maybe my problem isn't similar after all.

 

Am I missing some general command for locating and clicking on a button with specific text (but not a unique color)--and for locating a specific text field to Send my text to, when there are several text fields? Window Spy doesn't shows the same ClassNN for all (Chrome_RenderWidgetHostHWND1). I can't rely on it being done by x-y position on the page, because that varies somewhat for various reasons.

 

Thank you.

 

P.S. Again, this is for automating entries on a web page under Word Press.



Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

I am not familiar with WordPress, but is that not a site that is for personal blogs and the like?

 

If this is a browser, you're going to have trouble using just WinSpy. You will either need to break out of AutoHotKey and go with some JavaScript (plugin Scriptish or Greasemonkey on Chrome or FireFox), or if you want to keep with AHK, you'll want to use the COM interface in Internet Explorer.

 

Webpages aren't loading up with AHK-accessible elements. Some parts of the browser itself might be accessible, like the searchbox or address field, but not on the webpage itself.