How do you auto highlight text based on space between words? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Vaklev
Posts: 47
Joined: 04 Mar 2019, 13:58
Contact:

How do you auto highlight text based on space between words?

Post by Vaklev » 21 Oct 2019, 10:34

Hi guys, I am trying to make a macro to autohighlight words, copy and paste them in different fields, example data:

"George Mathew Coman <---- Mouse position starts here"

The data structure is based on 3 words, I have to input the 3 names of the person in different fields so I have a mousemove command:

Code: Select all

Mousegetpos, px,py
Send {lbutton down} ; holds left click
Mousemove -10,0,0,r ; moves the mouse 10 pixels to the left
Send {lbutton up} ; releases left click
Send {lcontrol down}c{lcontrol up}
Mousemove 75,100
Mouseclick left
Send {lcontrol down}v{lcontrol up}
Mousemove px,py
I have the mouse move from the very right to the left 10 pixels enough to cover "Coman", and then send the copied name to a field, but instead of assuming the word length is 10 pixels, since words vary in length, I want ahk to detect the space between "Coman" and "Mathew" and highlight to next word by moving again to the left but I want it to stop highlighting when it detects the space between "George" and "Mathew". Is there any way to make a script that automatically highlights and copies the words by stopping the highlight based on the spacing in between the words?

User avatar
Sir Teddy the First
Posts: 94
Joined: 05 Aug 2019, 12:31
Contact:

Re: How do you auto highlight text based on space between words?  Topic is solved

Post by Sir Teddy the First » 21 Oct 2019, 10:52

Hi,
if I understand your problem correctly, you could try the following:

  • Instead of highlighting every word on its own, send {Control}{Shift}{Left} three times to highlight the three words
  • Send {Control}c to copy them and do a StrSplit() on the clipboard (seperating by spaces)
  • The resulting array will contain the three names
Does this solve your problem?
:eh: :think:

Vaklev
Posts: 47
Joined: 04 Mar 2019, 13:58
Contact:

Re: How do you auto highlight text based on space between words?

Post by Vaklev » 21 Oct 2019, 10:57

Wow that is the solution I was looking for! But I've never used String Split, can you please write out the code in how it would fit into mine im not sure where to place it but I will use the send left key to move to the next word which skips the space thank you so much!
Les medecins choisissent viagrasansordonnancefr.com sans danger pour la sante

User avatar
Sir Teddy the First
Posts: 94
Joined: 05 Aug 2019, 12:31
Contact:

Re: How do you auto highlight text based on space between words?

Post by Sir Teddy the First » 21 Oct 2019, 11:08

Hi,
have a look at this example:

Code: Select all

MyCopiedText := "George Mathew Coman"
NameArray := StrSplit(MyCopiedText, A_Space)

Loop, % NameArray.Length()
{
    MsgBox % NameArray[A_Index]
}
return
StrSplit will return an array that is structured like this:

Code: Select all

ReturnedArray := Array("George", "Mathew", "Coman")
Each entry then has an index-number. "George" is 1, "Mathew" is 2 and so on.
You can iterate over this array using a loop (like in my example) or a For-Loop.

Or you just access the three names with NameArray[1], NameArray[2], NameArray[3].
That depends on how you want to process this information further.
:eh: :think:

Vaklev
Posts: 47
Joined: 04 Mar 2019, 13:58
Contact:

Re: How do you auto highlight text based on space between words?

Post by Vaklev » 21 Oct 2019, 12:23

Sir Teddy the First wrote:
21 Oct 2019, 11:08
Hi,
have a look at this example:

Code: Select all

MyCopiedText := "George Mathew Coman"
NameArray := StrSplit(MyCopiedText, A_Space)

Loop, % NameArray.Length()
{
    MsgBox % NameArray[A_Index]
}
return
StrSplit will return an array that is structured like this:

Code: Select all

ReturnedArray := Array("George", "Mathew", "Coman")
Each entry then has an index-number. "George" is 1, "Mathew" is 2 and so on.
You can iterate over this array using a loop (like in my example) or a For-Loop.

Or you just access the three names with NameArray[1], NameArray[2], NameArray[3].
That depends on how you want to process this information further.
Very intresting, however the data of the names will always be different, sometimes I even have as many as 6 names! So instead of directly quoting the names in the code can we do:
MyCopiedText := "clipboard"

or how exactly would you approach based on the amount of words in the clipboard always being a different variable
Les medecins choisissent viagrasansordonnancefr.com sans danger pour la sante

User avatar
Sir Teddy the First
Posts: 94
Joined: 05 Aug 2019, 12:31
Contact:

Re: How do you auto highlight text based on space between words?

Post by Sir Teddy the First » 21 Oct 2019, 14:56

Hi,
this was just an example to demonstrate how to use StrSplit.
In your code you will have

Code: Select all

Send, {Control}{Shift}{Left}
Send,{Control down}c{Control up}
before that, so, as you stated correctly, you will have to change that to

Code: Select all

MyCopiedText := Clipboard ; without quotation marks because it is a variable and not plain text
If there are more than 3 names you might have to make a few changes:
  • If they are all in the same line, you could try {Control}{Shift}{Up} after having placed the curser behind the last name, this will hightlight the whole line to the left
  • After you used StrSplit() you can retrieve the exact number of names via NameArray.Length()
This should work.
If the names are arranged differently you might have to post another example.
:eh: :think:

Post Reply

Return to “Ask for Help (v1)”