Force var expansion in string from ini? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
kunkel321
Posts: 1057
Joined: 30 Nov 2015, 21:19

Force var expansion in string from ini?

Post by kunkel321 » 21 Oct 2021, 17:34

Hi Folks,
Do you know if there is a way to make %clipboard% "expand" in a string, when the string comes from an ini file? Apparently is doesn't do that by default. With the below code, the first msgbox shows the clipboard content as expected, but the second one only shows the name of the variable...

Code: Select all

myVar = Clipboard content is: %clipboard%
MsgBox % myVar

IniRead, MyEntry, mtFiles\NoticeDefinitions.ini, clipboard test
MsgBox % MyEntry
The ini section is

Code: Select all

[clipboard test]
Here is the clipboard %clipboard%.
ste(phen|ve) kunkel

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

Re: Force var expansion in string from ini?

Post by mikeyww » 21 Oct 2021, 20:02

Code: Select all

ini := StrReplace(A_ScriptFullPath, ".ahk", ".ini")
IniRead, text, %ini%, clipboard test
MsgBox, 64, Expanded text, % expand(text)

expand(text) {
 While RegExMatch(text, "(.*)`%(.+?)`%(.*)", m)
  text := m1 %m2% m3
 Return text
}

User avatar
kunkel321
Posts: 1057
Joined: 30 Nov 2015, 21:19

Re: Force var expansion in string from ini?

Post by kunkel321 » 22 Oct 2021, 11:31

Thank you for this Mike. This is a very clever setup. I think I described my need poorly though. I have a tool that reads boilerplate text snippets and lets me choose/use one. The snippets are stored in an ini file. They are not run as they exist in the ini file though. The text gets processed in several different ways before getting typed into my target text field, though. I've noticed that I can use things like {Tab} or ^a and during the very last Send, %var% command they are turned into the associated actions. If I use %clipboard% in my stored boilerplate text however, it does't expand.

As I was writing this, I thought of a workaround, and it seems to work. If the boilerplate text contains the string %clipboard%, then expand the variable in a separate location and swap it for the string. To wit:

Code: Select all

IniRead, MyEntry, mtFiles\NoticeDefinitions.ini, clipboard test
if instr(MyEntry, "%clipboard%")
{
	myVar = %clipboard%
	myEntry := StrReplace(MyEntry, "%clipboard%", myVar)
}
MsgBox % MyEntry
ste(phen|ve) kunkel

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

Re: Force var expansion in string from ini?

Post by mikeyww » 22 Oct 2021, 11:40

What happens when you run my script?

User avatar
kunkel321
Posts: 1057
Joined: 30 Nov 2015, 21:19

Re: Force var expansion in string from ini?

Post by kunkel321 » 22 Oct 2021, 11:58

mikeyww wrote:
22 Oct 2021, 11:40
What happens when you run my script?
It didn't seem to work. (Maybe I was using it wrong.) I added a msgbox here:

Code: Select all

ini := StrReplace(A_ScriptFullPath, ".ahk", ".ini")
Msgbox, %ini%
IniRead, text, %ini%, clipboard test
MsgBox, 64, Expanded text, % expand(text)

expand(text) {
 While RegExMatch(text, "(.*)`%(.+?)`%(.*)", m)
  text := m1 %m2% m3
 Return text
}
The %ini% var was empty. At that point I realized we were changing the extension so that we could run the hotstring right from the ini file, so I stopped experimenting with it.
ste(phen|ve) kunkel

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

Re: Force var expansion in string from ini?  Topic is solved

Post by mikeyww » 22 Oct 2021, 12:03

You can certainly try with your own file.

Code: Select all

ini = mtFiles\NoticeDefinitions.ini
Msgbox, %ini%
IniRead, text, %ini%, clipboard test
MsgBox, 64, Expanded text, % expand(text)

expand(text) {
 While RegExMatch(text, "(.*)`%(.+?)`%(.*)", m)
  text := m1 %m2% m3
 Return text
}
Anyway, if your method works, there is no need to bother. If you just need a placeholder for a single variable, you could anything, %c% or {c} or whatever. My script just replaces the variable names with their values (more generically).

User avatar
kunkel321
Posts: 1057
Joined: 30 Nov 2015, 21:19

Re: Force var expansion in string from ini?

Post by kunkel321 » 22 Oct 2021, 13:13

It does indeed work!
I put the ini in the same folder as the script and renamed things as below.

Code: Select all

ini = sample.ini
IniRead, text, %ini%, section
MsgBox, 64, Expanded text, % expand(text)

expand(text) {
 While RegExMatch(text, "(.*)`%(.+?)`%(.*)", m)
  text := m1 %m2% m3
 Return text
}
ste(phen|ve) kunkel

Post Reply

Return to “Ask for Help (v1)”