RegEXMatch or replace and move to caret position.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
iilabs
Posts: 296
Joined: 07 Jun 2020, 16:57

RegEXMatch or replace and move to caret position.

Post by iilabs » 30 Jun 2022, 13:32

Hello everyone,

I am trying to using RegExMatch and the more I learn the more confusing it gets. Lol.

What I am trying to do is find [] at the end of a text in an active window if it exists (EradRIS) and move the cursor or caret to that position and highlight it.

For example here is the haystack:
Findings
The right kidney measures [8.9 x 3.8 x 4.5] cm demonstrating hydronephrosis containing an inferior echogenic focus measuring 0.8 x 0.3 x 1.0 cm with shadowing.

The left kidney measures [12.8 x 6.8 x 5.3] cm and is unremarkable.

Visualized aorta is normal in caliber measuring up to 1.9 cm.

Unremarkable IVC.

Incidental finding: Fatty liver.

IMPRESSION
[]
So far I get the code to find "IMPRESSION []" but I am not sure how to select just the "[]" :facepalm:

Code: Select all

CLEARUS:
 if WinExist(EradRis)
        WinActivate 
        Setwindelay, 10
                Clipboard := ""
                sleep, 10
                GuiControl, Focus, MainEdit
                Sendinput, {Ctrl down}ac{Ctrl up}
                ClipWait, 1
                USTEXT := Clipboard
                ;msgbox, % USTEXT
                RegExMatch(USText, "IMPRESSION", USTEX) 
                ;msgbox, %USTEX%
                if WinExist(EradRis)
                WinActivate 
                GuiControl, Focus, MainEdit
Return
I am wondering if I need to use RegExReplace instead. Maybe need to find the position in string and then move cursor or caret to that position?

Thank you!

Descolada
Posts: 1100
Joined: 23 Dec 2021, 02:30

Re: RegEXMatch or replace and move to caret position.

Post by Descolada » 30 Jun 2022, 13:48

Hello,
The RegEx part is fairly simple: IMPRESSION\r?\n\K\[] should select only the empty brackets.

What is difficult is moving the caret and highlighting it... A few ideas though:
First you must somehow get the text. If ControlGetText works, then you are in luck, this would probably be the best option. Otherwise perhaps sending Ctrl+A to select all and analyze the text through the clipboard. Or UIAutomation might work also.
Once you have the text, it depends on how you can interact with the editbox.
1) If ControlGetText worked, then SendMessage with the EM_SETSEL message should be able to select text or move the caret.
2) If the Ctrl+A and Ctrl+C method worked, then you could analyze the text and calculate how much you should move the caret from the beginning (and send the caret to the beginning) and use for example Down or Right-Left arrows.
3) UIAutomation might be able to move the caret as well, depending on the edit element.

Also a note: GUIControl only works with GUIs created by AutoHotkey, so it won't work with your external program. There you must use Control... functions and SendMessage/PostMessage.

Best of luck ;)

User avatar
iilabs
Posts: 296
Joined: 07 Jun 2020, 16:57

Re: RegEXMatch or replace and move to caret position.

Post by iilabs » 30 Jun 2022, 14:07

Thanks for your reply.

Yes I was having trouble just selecting the "[]" and your edit worked great.

So only thing works is Ctrl-A,C instead of the ControlGetText since external program so I will try to figure out that bit.
By analyzing the text do you mean parse it out? Find the "[]" and do some kind of mouse move function?

Most of the time this [] as at the bottom of the report so I could just do a send, pgdn? Still doesn't select the [] but maybe there is a way?

PS, I've been meaning to implement FindText() one of these days and really appreciate your tutorial.
Last edited by iilabs on 30 Jun 2022, 14:33, edited 1 time in total.

Descolada
Posts: 1100
Joined: 23 Dec 2021, 02:30

Re: RegEXMatch or replace and move to caret position.

Post by Descolada » 30 Jun 2022, 14:27

For external programs (like in your case) sometimes ControlGetText works, but usually these are older Win32 windows. You can test that by using Window Spy (which is accessible by right-clicking a running AHK script icon and selecting it from the menu) and seeing whether the control (and its text) shows up. Otherwise UIAutomation or Acc library might also access the text (and sometimes allow changing it, moving the caret etc).

By analyzing the text, I mean calculating the position where to move the caret. You can use RegexMatch for that and maybe further optimize it by calculating how many line breaks there are, which would let you move the caret down by lines with Down arrow and once on the correct line you can move it to the correct position with Right arrow. Btw, sometimes ControlSend also works here instead of Send (even without specifying the target control, which would send the keystrokes to the window instead and this sometimes works).

FindText is of course also an option, since the font and size of your text probably doesn't change. :)

User avatar
iilabs
Posts: 296
Joined: 07 Jun 2020, 16:57

Re: RegEXMatch or replace and move to caret position.

Post by iilabs » 30 Jun 2022, 14:37

Ok great will look into all those options. Maybe a good excuse to learn FindText()

UI automation is that a program or something written in AHK?

Most of the time this [] as at the bottom of the report so I could just do a send, pgdn? Still doesn't select the [], so I have to backspace and delete it, but maybe there is a way? Id prefer not sending "SEND" commands but maybe an option while I research your advice.

Is there a way to go ahead and delete the [] when found and then send the PgDn send command? Maybe RegReplace? Do you know off hand how I would convert to RegReplace and replace the [] with a blank or ""?

User avatar
iilabs
Posts: 296
Joined: 07 Jun 2020, 16:57

Re: RegEXMatch or replace and move to caret position.

Post by iilabs » 03 Jul 2022, 18:42

Very sorry to be a noob and not know you built that UI Automation your referring too. Amazing work and exactly something I need for other work. Appreciate all the suggestions.

Descolada
Posts: 1100
Joined: 23 Dec 2021, 02:30

Re: RegEXMatch or replace and move to caret position.

Post by Descolada » 03 Jul 2022, 23:05

Hey,
It seems I missed your June 30th post :/ Sending PgDn is an even better option indeed, or Ctrl+End should send the caret to the end of the document. Then you can delete the empty brackets or use Shift+Left to select characters to the left of the caret (perhaps select two characters, send Ctrl+C and check if the Clipboard actually contains "[]" or not).
You can use RegexReplace as well, yes: RegexReplace(USText, "IMPRESSION\r?\n\K\[]", "") should remove the brackets.

And I didn't actually create the UIA library myself, but I built upon a previously created one :)

User avatar
iilabs
Posts: 296
Joined: 07 Jun 2020, 16:57

Re: RegEXMatch or replace and move to caret position.

Post by iilabs » 06 Jul 2022, 12:26

Thank you!

I tried the RegexReplace code you have but seems to not remove the []. I was hoping to not add keyboard commands
(Ctrl-end and shift arrows - which did work!) Any idea why the RegExReplace command didn't work?

I tired,

Code: Select all

RegexReplace(USText, "IMPRESSION\r?\n\K\[]", "")
msgbox, % USTEXT

 RegexReplace(USText, "IMPRESSION\r?\n\K\[]", USTEXT)
 msgbox, % USTEXT       
                
I do get the [] in message if I use

Code: Select all

RegExMatch(USText,"IMPRESSION\r?\n\K\[]", USTEXT)
 msgbox, % USTEXT       
Is there a way to just select that [] and delete somehow? Worse comes to worse, Ill use your keyboard commands instead.

sofista
Posts: 645
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: RegEXMatch or replace and move to caret position.

Post by sofista » 06 Jul 2022, 14:35

iilabs wrote:
06 Jul 2022, 12:26

Code: Select all

RegexReplace(USText, "IMPRESSION\r?\n\K\[]", "")
msgbox, % USTEXT
Is there a way to just select that [] and delete somehow? Worse comes to worse, Ill use your keyboard commands instead.
You are not assigning the result to a variable. Try this:

Code: Select all

USTexT := RegexReplace(USText, "IMPRESSION\R\K\[]", "")
msgbox, % USTexT

User avatar
iilabs
Posts: 296
Joined: 07 Jun 2020, 16:57

Re: RegEXMatch or replace and move to caret position.

Post by iilabs » 06 Jul 2022, 16:06

Great, Thank you. This worked!

Post Reply

Return to “Ask for Help (v1)”