Selecting an empty string

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
x-intercept
Posts: 10
Joined: 05 Oct 2022, 02:58

Selecting an empty string

Post by x-intercept » 24 May 2023, 23:55

Code: Select all

; Get the report text from the active window's control
ControlGetText, report_text, %control_name%, A

; Select the current line of text where the cursor is located
SendInput, +{Home}

; Copy the selected line to the clipboard
SendInput, ^c
ClipWait
Sleep, 30


; Move the cursor to the end of the line
SendInput, {Right}
; Get the copied line from the clipboard
line_text := Clipboard
This code operates in a text editor and selects text to the left of the cursor and copies it. Subsequent steps are used to capture some of the text.

This works as expected but if the line contains no characters the +{home} step produces a Windows error. Is there a way I can copy a null string, or otherwise avoid interrupting the code?

User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: Selecting an empty string

Post by mikeyww » 25 May 2023, 05:23

Hello,

Which editor?

RussF
Posts: 1264
Joined: 05 Aug 2021, 06:36

Re: Selecting an empty string

Post by RussF » 25 May 2023, 05:52

Either there is a lot of code that has not been posted from this script or it is AI generated, because it makes no sense at all.

Where is control_name defined and where do you use it?

If, by using the ControlGetText statement, you are grabbing the entire edit window, why then do you need to highlight the line?

After highlighting a line, whether you copy the text or not, if you are not already at the end of the line, {right} does NOT take you there, it only deselects the text that was selected and leaves the cursor where it is.

Issuing a +{Home} on a blank line does not issue a Windows error. It may issue an error from what ever editor or application you are using, but it's not from Windows.

Perhaps post your entire code or let us know exactly what, step by step, you are trying to achieve within which application.

Russ

x-intercept
Posts: 10
Joined: 05 Oct 2022, 02:58

Re: Selecting an empty string

Post by x-intercept » 25 May 2023, 17:27

The editor is a rich text editor in windows forms within a larger proprietary program.

x-intercept
Posts: 10
Joined: 05 Oct 2022, 02:58

Re: Selecting an empty string

Post by x-intercept » 25 May 2023, 17:50

Russ, thank you for taking the time to reply.
RussF wrote:
25 May 2023, 05:52
Either there is a lot of code that has not been posted from this script or it is AI generated, because it makes no sense at all.

Where is control_name defined and where do you use it?
This is a snippet from a much larger script. The control is declared prior to the snippet and may exist in many different versions on different clients.
If, by using the ControlGetText statement, you are grabbing the entire edit window, why then do you need to highlight the line?
A regexmatch is performed on the string within the line and the match captured to identify other information within the control. Sometimes the string is empty, which require an alternate approach.
After highlighting a line, whether you copy the text or not, if you are not already at the end of the line, {right} does NOT take you there, it only deselects the text that was selected and leaves the cursor where it is.
That is correct. The text is edited by different users in a line-by-line fashion. After the text is modified by the script the user enters more text at the original position, so the highlighted text needs to be deselected.

The error occurs within the richtext editor, and in WordPad, but thanks for pointing out that it is not a Windows error.

RussF
Posts: 1264
Joined: 05 Aug 2021, 06:36

Re: Selecting an empty string

Post by RussF » 26 May 2023, 05:27

I can't speak for your particular rich text editor, but in WordPad, only a chime was sounded when I tried a +{Home} on an empty line. I would suggest the following:

Before you issue your ^c command, clear the clipboard:

Code: Select all

Clipboard := ""
ClipWait alone will wait indefinitely for something to appear in the clipboard, which is probably why it is hanging your script. Instead, add a timeout option to the command and then test for an empty clipboard:

Code: Select all

Clipboard := ""
SendInput, ^c
ClipWait, 1   ; waits 1 second for contents to appear, then continues - could be a decimal fraction (.5, .25, etc.) if that works consistently for you
IF (Clipboard != "") {
	; code here for when the clipboard contains data
} else {
	; code here for when the clipboard is empty
}
Hope this helps.

Russ

x-intercept
Posts: 10
Joined: 05 Oct 2022, 02:58

Re: Selecting an empty string

Post by x-intercept » 28 May 2023, 20:13

Thank you, that worked.

Post Reply

Return to “Ask for Help (v1)”