Replacing words inside text Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Rostov
Posts: 129
Joined: 09 Apr 2020, 07:57

Replacing words inside text

Post by Rostov » 25 Jul 2021, 15:53

I wrote something like this:

Code: Select all

#IfWinActive ahk_exe mirc.exe
:X://z3::SendInput, 83.
:X://04::SendInput, 4.
:X://x0::SendInput, 16.
:X://6e::SendInput, 110.
This should replace the text as you enter it into the irc client text box. It works fine when I type '//z3' in a blank text field and hit space. But it doesn't work when I type 'z304x06e', add slashes and separate with a space. Why? Why does such a constructed text replacement not work inside the already written text? How to make it possible?

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

Re: Replacing words inside text

Post by mikeyww » 25 Jul 2021, 16:00

Because each hotstring is independent. You can use the "?" option inside a word, but you would still need to provide the hotstring text. An alternative is to accept an entire string and then parse it as you wish.
Last edited by mikeyww on 25 Jul 2021, 16:09, edited 2 times in total.

gregster
Posts: 8886
Joined: 30 Sep 2013, 06:48

Re: Replacing words inside text

Post by gregster » 25 Jul 2021, 16:02

I assume you are looking for ? option for hotstrings:
https://www.autohotkey.com/docs/Hotstrings.htm#Options wrote:? (question mark): The hotstring will be triggered even when it is inside another word; that is, when the character typed immediately before it is alphanumeric. For example, if :?:al::airline is a hotstring, typing "practical " would produce "practicairline ".
Edit: too late, but with a link, so I'll keep it here.
Edit2: to me, it is not absoultely clear, what this means: "But it doesn't work when I type 'z304x06e', add slashes and separate with a space."
Where do you add the slashes ? don't see any... Like mikeyww said, you'll need to type the complete hotstring that you defined, or use another solution.

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

Re: Replacing words inside text

Post by mikeyww » 25 Jul 2021, 16:09

One option to get the whole string:

Code: Select all

:*://::
Input, str, V, {Space}
len := StrLen(str) + 1
str := StrReplace(str, "z3", "83.")
str := StrReplace(str, "04", "4.")
str := StrReplace(str, "x0", "16.")
str := StrReplace(str, "6e", "110.")
Send {BS %len%}%str%
Return
Or:

Code: Select all

:B0*://::
Input, str, V, {Space}
len := StrLen(str) + 3
For k, v in {z3: 83, "04": 4, x0: 16, 6e: 110}
 str := StrReplace(str, k, v ".")
SendInput {BS %len%}%str%
Return
The second one has a few extra features:
  • "//" remains in view until ready to parse
  • Easy to add more strings
  • SendInput may improve reliability in this routine
Last edited by mikeyww on 25 Jul 2021, 16:29, edited 1 time in total.

Rostov
Posts: 129
Joined: 09 Apr 2020, 07:57

Re: Replacing words inside text

Post by Rostov » 25 Jul 2021, 16:28

Maybe I'll explain what this is for. On the irc channel that I visit, the IP addresses of people entering are visible only in encrypted form. For example, the IP address '83.4.16.110 'is' z304x06e'. I was able to write a script that enters the user's encoded IP into the IRC client's text input field. After pressing the space after 'z304x06e' I would like to convert to the decoded form, which is '83.4.16.110 '. How to do it?

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

Re: Replacing words inside text

Post by mikeyww » 25 Jul 2021, 16:33

Code: Select all

F3::
InputBox, str, Decode, Enter the coded string.,, 300, 125
If (ErrorLevel || str = "")
 Return
For k, v in {z3: 83, "04": 4, x0: 16, 6e: 110}
 str := StrReplace(str, k, v ".")
SendInput % Trim(str, ".")
Return
This won't work perfectly because it doesn't parse in pairs, but you could adjust the script to do that.

Rostov
Posts: 129
Joined: 09 Apr 2020, 07:57

Re: Replacing words inside text

Post by Rostov » 25 Jul 2021, 16:44

@mikeyww, For the example I gave, it works fine. I only noticed that you had to put the value 04 in quotation marks. Why? I need to enter 256 of these examples, so I would like to know which values I need to enclose in quotation marks.

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

Re: Replacing words inside text

Post by mikeyww » 25 Jul 2021, 16:46

OK.

Another example, maybe better:

Code: Select all

map := {z3: 83, 4: 4, x0: 16, 6e: 110}
F3::
InputBox, str, Decode, Enter the coded string.,, 300, 125
If (ErrorLevel || str = "")
 Return
new =
Loop, % StrLen(str) / 2
 new .= map[SubStr(str, 2 * A_Index - 1, 2)] "."
SendInput % Trim(new, ".")
Return
The "04" was quoted because it otherwise would not handle the zero properly. In the new example above, this is not needed because the array element number is first evaluated as an expression.

Alternative:

Code: Select all

map := {z3: 83, 4: 4, x0: 16, 6e: 110}
F3::
InputBox, str, Decode, Enter the coded string.,, 300, 125
If (ErrorLevel || str = "")
 Return
new =
Loop, % StrLen(str) / 2
 sub := SubStr(str, 2 * A_Index - 1, 2), new .= (map.HasKey(sub) ? map[sub] : sub) "."
SendInput % Trim(new, ".")
Return
This retains unmapped numbers.
Last edited by mikeyww on 25 Jul 2021, 16:57, edited 2 times in total.

User avatar
flyingDman
Posts: 2776
Joined: 29 Sep 2013, 19:01

Re: Replacing words inside text

Post by flyingDman » 25 Jul 2021, 16:55

If for whatever reason you need to start with // and end a string with " ", you can use Hotstrings (see here: https://github.com/Paris/AutoHotkey-Scripts/blob/master/Hotstrings.ahk):

Code: Select all

hotstrings("//(\w+)\s", "label")
return

label:
str := $1
For k, v in {z3: 83, "04": 4, x0: 16, 6e: 110}
 str := StrReplace(str, k, v ".")
SendInput % Trim(str, ".")
return
(The last 3 lines were borrowed from @mikeyww...)
14.3 & 1.3.7

Rostov
Posts: 129
Joined: 09 Apr 2020, 07:57

Re: Replacing words inside text

Post by Rostov » 25 Jul 2021, 17:11

mikeyww wrote:
25 Jul 2021, 16:46
OK.

Another example, maybe better:

Code: Select all

map := {z3: 83, 4: 4, x0: 16, 6e: 110}
F3::
InputBox, str, Decode, Enter the coded string.,, 300, 125
If (ErrorLevel || str = "")
 Return
new =
Loop, % StrLen(str) / 2
 new .= map[SubStr(str, 2 * A_Index - 1, 2)] "."
SendInput % Trim(new, ".")
Return
@mikeyww I pasted this code again and it stopped returning the decoded form of IP. Have you changed anything?

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

Re: Replacing words inside text

Post by mikeyww » 25 Jul 2021, 17:17

It worked in my test just now. Use this code without the other. Close other scripts.

Activate your target window.
Press F3.
Input: z304x06e
Output: 83.4.16.110

Rostov
Posts: 129
Joined: 09 Apr 2020, 07:57

Re: Replacing words inside text

Post by Rostov » 25 Jul 2021, 17:50

@mikeyww, weird. The other two versions of your script work when I paste them at the end of the file with other scripts, and only this one needs to be in a separate file and I can't use it when I have a separate file with my scripts enabled.

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

Re: Replacing words inside text

Post by mikeyww » 25 Jul 2021, 17:58

The initial mapping definition (map := ...) needs to be placed at the top of the script.

Explained: https://www.autohotkey.com/docs/Scripts.htm#auto

Rostov
Posts: 129
Joined: 09 Apr 2020, 07:57

Re: Replacing words inside text

Post by Rostov » 26 Jul 2021, 07:33

@mikeyww, in my script file I have a timer which also needs to be on top of the script to run. When I paste your code first, the timer stops working. How to put two pieces of code in one file that must be on top to be executed? I don't want to create two separate ahk files.

EDIT: When I put your code in a separate file and run it along with my script file, yours won't work.

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

Re: Replacing words inside text

Post by mikeyww » 26 Jul 2021, 09:06

Put the mapping line at the top. Put the hotkey routine at the bottom.

Rostov
Posts: 129
Joined: 09 Apr 2020, 07:57

Re: Replacing words inside text

Post by Rostov » 26 Jul 2021, 09:20

@mikeyww, but that's just the structure of the code you provided. I didn't change anything about it. I just paste it into a file where I have dozens of other hotkeys. However, your code only works if I put it on the first line of the file. The problem is that in the same file I have a timer based shortcut key which also only works if it's on the first line of the file. How do I make both hotkeys work?

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

Re: Replacing words inside text

Post by mikeyww » 26 Jul 2021, 09:23

Put the mapping line (first line) on the first (or second) line of your script. Put the F3 hotkey routine-- from F3 to the last Return-- in my script at the bottom of your script.

Feel free to post your revision if needed.

Rostov
Posts: 129
Joined: 09 Apr 2020, 07:57

Re: Replacing words inside text

Post by Rostov » 26 Jul 2021, 09:37

@mikeyww maybe I'll explain what it looks like in my ahk file:

Code: Select all

#KeyHistory 500
#InstallKeybdHook 
#InstallMouseHook

; CHROME - CURSOR MOVE

SetTitleMatchMode, RegEx
SetTimer, Curs, 5000
return

Curs:
If fullscreenn() && WinActive("Administrator.*") {
     If (A_TimeIdle > 5000)
     ; CoordMode, Mouse, Client
     MouseMove, 1365, 365, 0
}
return

fullscreenn() {
	WinGetPos,,, w, h, A
	return (w = A_ScreenWidth && h = A_ScreenHeight)
}
return

; HASH DECODER

map := {z3: 83, 4: 4, x0: 16, 6e: 110}
F3::
InputBox, str, Decode, Enter the coded string.,, 300, 125
If (ErrorLevel || str = "")
 Return
new =
Loop, % StrLen(str) / 2
 sub := SubStr(str, 2 * A_Index - 1, 2), new .= (map.HasKey(sub) ? map[sub] : sub) "."
SendInput % Trim(new, ".")
Return
Only the 'CHROME - CURSOR' script works in this system. If I put your script (I called it 'HASH DECODER') first, that works, but 'CHROME - CURSOR' doesn't. What is wrong?

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

Re: Replacing words inside text

Post by mikeyww » 26 Jul 2021, 09:48

I've said it twice, but I'll say it a third time. Move map := {z3: 83, 4: 4, x0: 16, 6e: 110} to the top of the script.

Rostov
Posts: 129
Joined: 09 Apr 2020, 07:57

Re: Replacing words inside text

Post by Rostov » 26 Jul 2021, 13:14

@mikeyww, I'm very sorry. My English isn't very good (as you can see) and I realized that this line should be at the top of your code, not at the top of my scripts file. Now it works perfectly. Once again, I'm so sorry for the confusion. :(

I have two more questions.

1. What's the difference between these two hotkeys? I don't understand what "This retains unmapped numbers" means.

2. I'm trying to call your hotkey with my IP encrypted download hotkey

Code: Select all

#IfWinActive ahk_exe mirc.exe
^Tab::
     Thread, NoTimers, True
     KeyWait, Tab
     Send, {F1}
     Send, {Backspace 8}+{Left 8}^x^a{Backspace}
     Sleep, 500
     Send, {F3}
return
But when it comes to 'Send, {F3}', the input box doesn't appear. Physical pressing the F3 key on keyboard works. Why?

Post Reply

Return to “Ask for Help (v1)”