AndyReifman
AndyReifman

Reputation: 1834

autohotkey text replacement with variable

I am trying to figure out a way to do text replacement with a variable if possible.

Basically let's say I have a string: The Color is Red

I can say ::color::The Color is Red which will work, but now what if I want to be able to specify the color on demand. So if I typed color blue it would print out The Color is Blue instead.

Is there a way to do that? Or will I have to define all possible variations of my sentence in my script.

Upvotes: 1

Views: 580

Answers (1)

Relax
Relax

Reputation: 10543

You can use a parsing Loop (or a For Loop in an array) and the Hotstring() function to create the hotstrings dynamically:

colors := "Blue,Red,White"
Loop, parse, colors, `,
Hotstring(":*:" "color " A_Loopfield, "The Color is "  A_Loopfield, On)

The above code has to be placed in the auto-execute section (top of the script, before the first hotkey, hotstring or return) otherwise the script can't create the hotstrings.

Upvotes: 0

Related Questions