Place prompt in hotstring text

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
StefOnSteroids
Posts: 77
Joined: 08 Aug 2015, 10:22

Place prompt in hotstring text

26 Apr 2019, 14:51

After ahk has inserted my replacement text, I want to start typing at a certain position, not at the end of the replacement text. Other than stepping dozens of chars left again {Left 25}, is there a better way to position the prompt? I was thinking of some kind of placeholder like

Code: Select all

::text1::
(
After ahk has inserted my replacement text, I want to start typing
__RIGHT HERE AT THIS POSITION__
and not at the end of the whole enchilada that follows here until the end of the replacement text.
)
but could not find anything in the help file.
Maybe some workaround with a variable that I can then jump to?
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Place prompt in hotstring text

26 Apr 2019, 19:47

I'm not sure that there's a better solution than using {Left}. Instead of doing it manually, though, if you want to make it part of the hotstring, you could do:

Code: Select all

::text1::
    text1 = 
(
After ahk has inserted my replacement text, I want to start typing
__RIGHT HERE AT THIS POSITION__
and not at the end of the whole enchilada that follows here until the end of the replacement text.
)
    ReplaceText(text1)
Return

ReplaceText(Text)
{
    Delimiter := "__RIGHT HERE AT THIS POSITION__"			; Set the text to remove and insert at
    cursorPosition := InStr(Text, Delimiter)				; Get the position of the text to remove and insert at
    TextPart1 := SubStr(Text, 1, cursorPosition - 1)			; Get the text before the delimiter
    TextPart2 := SubStr(Text, cursorPosition + StrLen(Delimiter))	; Get the text after the delimiter
    TextPart2Len := StrLen(TextPart2)					; Get the length of the text after the delimiter
    SendInput, {Text}%TextPart1%%TextPart2%				; Send the text
    SendInput, {Left %TextPart2Len%}					; Cursor left to the position where the delimiter was
}
I know that it can be simplified, but it may be clearer this way. Also, the ReplaceText function is just to make it more reusable if you have other hotstrings that you want to do this for. If not, you could move that function's lines into the hotstring, itself. Anyways, maybe that's not really what you're after, but it might give you an idea.
Last edited by Osprey on 26 Apr 2019, 22:44, edited 3 times in total.
Ridwan
Posts: 144
Joined: 17 Oct 2015, 21:06
Location: Indonesia

Re: Place prompt in hotstring text

26 Apr 2019, 20:40

I'm agree with Osprey, i think using {Left} is best way to do it.

Here are another version inspired from the previous script.

Code: Select all

text1 =
(LTrim Join`s
    After ahk has inserted my replacement text, I want to start typing
    __RIGHT HERE AT THIS POSITION__
    and not at the end of the whole enchilada that follows here until the end of the replacement text.
)

::text1::
    target_pos := InStr(text1, "__RIGHT HERE AT THIS POSITION__") + StrLen("__RIGHT HERE AT THIS POSITION__")
    text1 := StrReplace(text1, "__RIGHT HERE AT THIS POSITION__")
    SendInput, %text1%{Left %target_pos%}
Return
StefOnSteroids
Posts: 77
Joined: 08 Aug 2015, 10:22

Re: Place prompt in hotstring text

26 Apr 2019, 23:41

Impressive, thank you both, Osprey and Ridwan.
Although neither approach offers a speed advantage, they're much more flexible and comfortable to handle. No need anymore to count and recount the chars before I finally get it right.

I like the idea of tucking it away in a function and letting ahk take care of the counting.
And it was good reminder that I can use multiple options like (LTrim Join`s

Thank you very much.
Ridwan
Posts: 144
Joined: 17 Oct 2015, 21:06
Location: Indonesia

Re: Place prompt in hotstring text

27 Apr 2019, 01:04

Glad it works for you.., :)
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Place prompt in hotstring text

27 Apr 2019, 05:36

@Ridwan Your code works only the first time. You replace the text1 content within the hotstring so the next use of the hotstring get the text1 content different from the starting value of text1.
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Ridwan
Posts: 144
Joined: 17 Oct 2015, 21:06
Location: Indonesia

Re: Place prompt in hotstring text

27 Apr 2019, 06:55

Odlanir wrote:
27 Apr 2019, 05:36
@Ridwan Your code works only the first time. You replace the text1 content within the hotstring so the next use of the hotstring get the text1 content different from the starting value of text1.
Thanks Odlanir. That's a very fatal mistake.

Here is the fixed code.

Code: Select all

::text1::
    text1 =
    (LTrim Join`s
        After ahk has inserted my replacement text, I want to start typing
        __RIGHT HERE AT THIS POSITION__
        and not at the end of the whole enchilada that follows here until the end of the replacement text.
    )
    
    target_pos := InStr(text1, "__RIGHT HERE AT THIS POSITION__") + StrLen("__RIGHT HERE AT THIS POSITION__")
    text1 := StrReplace(text1, "__RIGHT HERE AT THIS POSITION__")
    SendInput, %text1%{Left %target_pos%}
Return
StefOnSteroids
Posts: 77
Joined: 08 Aug 2015, 10:22

Re: Place prompt in hotstring text

27 Apr 2019, 08:29

Thanks again.
Also @Odlanir for pointing it out.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], scriptor2016 and 307 guests