Select text up to a certain character

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
RyanAyton
Posts: 49
Joined: 14 Aug 2017, 09:53

Select text up to a certain character

Post by RyanAyton » 09 Dec 2017, 14:11

Hello

Just a quick one, how would I use +{Right} to keep going until it meets a certain character, for example a "/"?

Many thanks,
Ryan

Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Select text up to a certain character

Post by Osprey » 09 Dec 2017, 15:13

You could use the clipboard. Each time that you cursor right, copy the current selection to the clipboard and check whether the last character is a / or not.

Code: Select all

SendMode Input                                                  ; The following works a lot better with Send's Input mode

$+Right::
  Send, {shift down}{right}{ctrl down}{c}{ctrl up}{shift up}    ; Move the cursor right and copy the current selection to the clipboard
  Sleep, 25                                                     ; Give the system time to update the clipboard
  StringRight, lastchar, clipboard, 1                           ; Store the last character of the selection to variable lastchar
  If lastchar = /                                               ; If the last character is a /
    Send, {shift down}{left}{ctrl down}{c}{ctrl up}{shift up}   ; Move the cursor left (so that the / isn't included) and copy the current selection to the clipboard
Return
I found that a sleep of 25 was the minimum needed on my system to ensure that the clipboard was updated. You may need to increase that a bit if you find that the cursor breaks past the / and continues on when you're holding down the keys.

RyanAyton
Posts: 49
Joined: 14 Aug 2017, 09:53

Re: Select text up to a certain character

Post by RyanAyton » 10 Dec 2017, 07:41

That's brilliant, thanks Osprey
How would I go about making it automatically keep going right until it finds "/"?
Just now when I'm running it I need to keep pressing the assigned key for it to work.

Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Select text up to a certain character

Post by Osprey » 10 Dec 2017, 16:42

If you mean that you want to release Shift and Right and have it continue selecting characters, a loop is required:

Code: Select all

SendMode Input                                                  ; The following works a lot better with Send's Input mode

$+Right::
  endloop := 0
  Loop
  {
    Send, {shift down}{right}{ctrl down}{c}{ctrl up}{shift up}    ; Move the cursor right and copy the current selection to the clipboard
    Sleep, 25                                                     ; Give the system time to update the clipboard
    StringRight, lastchar, clipboard, 1                           ; Store the last character of the selection to variable lastchar
  }
  Until endloop or lastchar = "/"                                 ; End loop when last character = / (must be in quotes because it's an expression)
  Send, {shift down}{left}{ctrl down}{c}{ctrl up}{shift up}       ; Move the cursor left (so that the / isn't included) and copy the current selection to the clipboard
Return

~Esc::endloop := !endloop
The references to "endloop" aren't necessary, but I figure that you might want some way to stop it, so I set Esc to do that.

dark_side_69
Posts: 1
Joined: 27 May 2023, 12:41

Re: Select text up to a certain character

Post by dark_side_69 » 27 May 2023, 13:19

I knot whis topic is quide old but it saved my life today, however, I was wondering if this operation could be sped up. I modified the script to select whole words but I cannot figure out how to make AHK check if my specific character is contained anywhere in clipboard and then revert selection back by one word. What I wanted to achieve is the following sequence: Select first word, check if clipboard contains my character (/ in the example above). If not, repeat to select another word and copy two words to clipboard, then check if it contains the character and so on until it finds this character in clipboard. If it does it should move selection one word back. Selection sequences are clear to me but I cannot make it look for specific character in clipboard content. I will be extremely grateful for your help.

Post Reply

Return to “Ask for Help (v1)”