How to send text to a location in a window? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: How to send text to a location in a window?

23 Jul 2020, 14:55

Another potential issue:

Code: Select all

run https://82187.csb.app		; gregster's prototype webpage with just a text box.
sleep 1000
WinGet nAHK_ID_ZipSearch, ID, Parcel Sandbox
You should be using: WinWait
The window most likely does not have the wintitle in 1sec.

for ahk_parent Read here:
ControlSend
If this parameter is ahk_parent, the keystrokes will be sent directly to the target window instead of one of its controls
Newer2AHK
Posts: 59
Joined: 24 Jun 2020, 10:32

Re: How to send text to a location in a window?

23 Jul 2020, 16:22

gregster wrote:
29 Jun 2020, 13:29
in Chrome (in debug mode), using Chrome.ahk by GeekDude:

Code: Select all

...
ChromeInst := new Chrome("ChromeProfile")
winwait, - Google Chrome
...
@gregster, I have a question about the above two lines of code from Edit 2 of your previous post.

I understand that the first creates an object that is an instance of the class Chrome defined in Chrome.ahk . I'm not very experienced with classes and objects, so I hope this isn't a stupid question. I think the "Chrome("ChromeProfile")" is a call to a constructor for that class. I don't see anything that looks like that in the script, Chrome.ahk. Can you tell me where the code for that constructor is?

In the second quoted line, you're telling AHK to wait until a window has finished loading with a title containing "- Google Chrome". But when I modify the code to pause at that point, the page has the title "about: blank" and I don't see any text "Google Chrome" anywhere on the page. But the WinWait command must be finding a title containing that text because the script proceeds past that point. Can you tell me where it is getting that title from?
Newer2AHK
Posts: 59
Joined: 24 Jun 2020, 10:32

Re: How to send text to a location in a window?

23 Jul 2020, 17:25

Xtra wrote:
23 Jul 2020, 14:55
You should be using: WinWait
The window most likely does not have the wintitle in 1sec.
Actually, I'm pretty sure it does because I remember running the code in a debugger and checking the value of "nAHK_ID_ZipSearch", and it had the correct AHK ID of the "Parcel Sandbox" window as determined by a separate script run to detect that value independently.

However, it seems that WinWait would be a better than the 1-sec. sleep for two reasons. If something slows things down so it does take more than a second, then sleep followed by WinGet will fail, whereas WinWait followed by WinGet will still work. And conversely, when the page is ready in less than a second, then WinWait followed by WinGet won't sit around waiting longer than it needs to. So I'm going to use WinWait instead of sleep in this sort of situation in the future. In addition to you, I also got that from gregster's code for use with Chrome.ahk, which I'm working on implementing.

Just to be sure, I went ahead and modified the code of "put text in a background window - 1.ahk" and "put text in a background window - 2.ahk" that were given in my previous post, replacing "sleep 1000" with "WinWait Parcel Sandbox" for the first window and "WinWait Google" for the second. The behavior reported in that post did not change, so it still appears that ControlSend is not behaving correctly.
Newer2AHK
Posts: 59
Joined: 24 Jun 2020, 10:32

Re: How to send text to a location in a window?

23 Jul 2020, 17:46

malcev wrote:
23 Jul 2020, 11:54
You can see what controlsend sends with spy++.
Yes, I've just learned about Spy++ in another thread. I've installed VS to get it. Now I just have to find the time to get up to speed in it! It looks interesting and I'm looking forward to seeing what I learn from it.
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: How to send text to a location in a window?

23 Jul 2020, 17:57

Newer2AHK wrote:
23 Jul 2020, 17:25
so it still appears that ControlSend is not behaving correctly.
If you cant get it working with ControlSend you will not get it working by switching to PostMessage. You must be doing something wrong as you got it to work previously with Malcev's code.

I would ditch all this nonsense and control the browser directly with selenium or chrome.ahk. (as previously mentioned by others) It will be more reliable in the long run. Its your choice gl
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: How to send text to a location in a window?

23 Jul 2020, 18:12

Hi,

it is possible to programmatically - and somehow - set values in firefox from ahk. The following example demonstrate how to do this using a custom temporary extension - admittedly, this is for specific real use cases and has limited possibilities.

. restart firefox and navigate to about:debugging#/runtime/this-firefox
. run the script
. @ about:debugging#/runtime/this-firefox click on 'charger un module complémentaire' (or the equivalent in your language)
. select the manifest created by the script (A_ScriptDir . "\testahktest\manifest.json")
. open a new tab from about:debugging#/runtime/this-firefox and navigate to https://82187.csb.app/ but making sure you don't move the window until you are at https://82187.csb.app/
. press ALT+D to disable firefox
. press ALT+J to execute javascript in firefox from ahk

Code: Select all

#NoEnv
#Warn
SendMode Input
SetWorkingDir %A_ScriptDir%
#SingleInstance force

if not FileExist(dir:=A_ScriptDir . "\testahktest") {
	FileCreateDir, % dir
	manifest =
	(Join`r`n
	{
		"manifest_version": 2,
		"name": "test",
		"version": "1.0",
		"description": "just a test",
		"content_scripts": [
			{
				"matches": ["https://82187.csb.app/"],
				"js": ["inject.js"]
			}
		]
	}
	) ; https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Your_first_WebExtension
	FileAppend, % manifest, % dir . "\manifest.json", UTF-8
	js =
	(Join`r`n
	var i = 0;
	window.addEventListener("resize", function(_event) {
		if (window.outerWidth == 500)
			document.getElementById("OZipCode").value = ++i;
	});
	)
	FileAppend, % js, % dir . "\inject.js", UTF-8
}
OnExit, handleExit
return

!x::
handleExit:
	if WinExist("ahk_class MozillaWindowClass ahk_exe firefox.exe")
		WinClose
ExitApp

#If WinExist("ahk_class MozillaWindowClass ahk_exe firefox.exe")
	!d::WinSet, Disable
	; !e::WinSet, Enable
	!j::
		WinMove, ahk_class MozillaWindowClass ahk_exe firefox.exe,,,, 500
		WinMove, ahk_class MozillaWindowClass ahk_exe firefox.exe,,,, 501
	return
#If
As suggested by others I would use instead chrome.ahk or COM with IE.

A_AhkUser
my scripts
Newer2AHK
Posts: 59
Joined: 24 Jun 2020, 10:32

Re: How to send text to a location in a window?

23 Jul 2020, 20:31

@Xtra and @A_AhkUser, yes, I have downloaded Chrome and Chrome.ahk and have started working on implementing them in my project. It looks like Chrome.ahk will be a good and helpful facility once I get up to speed in it.

In the meantime, I remain curious about what's going on with ControlSend. When I said above that malcev's code worked, that was with Firefox in the active window. The code did not work with the window in the background, and that's the problem here. As I said, I'm still curious about that issue, but I am moving on to solve the problem with Chrome.ahk.

For that reason, I hope someone can answer my questions about the two lines from @gregster's sample code for running Chrome.ahk in my post above.
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: How to send text to a location in a window?

24 Jul 2020, 04:29

Newer2AHK wrote:
23 Jul 2020, 16:22
gregster wrote:
29 Jun 2020, 13:29
in Chrome (in debug mode), using Chrome.ahk by GeekDude:

Code: Select all

...
ChromeInst := new Chrome("ChromeProfile")
winwait, - Google Chrome
...
I understand that the first creates an object that is an instance of the class Chrome defined in Chrome.ahk .
You're right. See __New meta-function.

Know that you can use ListLines to display the script lines most recently executed:

Code: Select all

inst := new C()
ListLines
Pause
MsgBox % inst.p
return


Class C {
	__New() {
		this.p := "test"
		MsgBox % A_ThisFunc
	}
}
Newer2AHK wrote:
23 Jul 2020, 16:22
In the second quoted line, you're telling AHK to wait until a window has finished loading with a title containing "- Google Chrome". But when I modify the code to pause at that point, the page has the title "about: blank" and I don't see any text "Google Chrome" anywhere on the page. But the WinWait command must be finding a title containing that text because the script proceeds past that point. Can you tell me where it is getting that title from?
It works since about: blank is not the browser's window title but the currently focused tab's name - and while Chrome appends - Google Chrome to the currently focused tab to set its window title accordingly.

One can see that, in the Chrome.ahk's constructor (that is, in the Chrome.__New meta-function), the script sets the instance's own PID property (cf. Chrome.ahk#L97).
So I guess the following should also work (not tested):

Code: Select all

ChromeInst := new Chrome("ChromeProfile")
winwait % "ahk_pid " . ChromeInst.PID
As for the controlsend command, I have no idea why this doesn't work in this specific case.

Hope this helps,

A_AhkUser
my scripts
Newer2AHK
Posts: 59
Joined: 24 Jun 2020, 10:32

Re: How to send text to a location in a window?

24 Jul 2020, 05:51

A_AhkUser wrote:
24 Jul 2020, 04:29
See __New meta-function.
Ah! Excellent. Now I know what those mysterious __New() methods in Chrome.ahk are. I've added that link to the documentation of Chrome.ahk that I prepared.

And thanks for the rest of your answer. All helpful. Now I understand @gregster's Chrome.ahk script.
Newer2AHK
Posts: 59
Joined: 24 Jun 2020, 10:32

Solved -- and next issue

24 Jul 2020, 11:50

Solved. I've implemented Chrome and Chrome.ahk and confirmed that it solved the problem. I've selected @gregster's post with the Chrome.ahk script as the solution because I used that code as the prototype to work from. But I wish I could have also selected replies from @Xtra and @A_AhkUser that were also critical in getting it to work.

Next issue. One problem I notice with Chrome is that I am being blocked when just doing test runs, so Google is somehow detecting what I'm doing in a way that didn't happen in Firefox. I figured I'd need to use proxies when I started running things in volume, but I didn't expect to need it at this stage. But I'm going to have to start looking into that. Suggestions would be welcome.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333, TheDewd and 271 guests