Page 1 of 1

Stuck with simple problem

Posted: 15 Nov 2018, 16:33
by Leli196
Hi there,

I haven't done AHK for a while and now I am stuck with a simple problem:

Code: Select all

; INI file
[general]
var1 = some text
scheme = {Tab}var1

Code: Select all

; AHK code
IniRead, temp_ini, % A_WorkingDir "\settings.ini", general
Loop, Parse, temp_ini, `r`n
	value := StrSplit(A_LoopField, "="), var := value[1], %var% := value[2]

F1::
SendInput, %scheme%
Return
I want to send {Tab}some text with F1, but currently I do not know what I should do. I feel like a total noob, but the solution (or a solution) does not come to my mind.

Re: Stuck with simple problem

Posted: 15 Nov 2018, 17:18
by swagfag
did u write ur INI with those spaces around the = signs?
either delete them or use var := Trim(value[1])

Re: Stuck with simple problem

Posted: 15 Nov 2018, 21:20
by Leli196
Yes, I always use spaces before and after =. I thought it does not make a difference. At least in this case it does not change anything.

Re: Stuck with simple problem

Posted: 15 Nov 2018, 21:41
by CyL0N
I think you're complicating it a bit,by parsing ini instead of reading every var from it... but you can do it this way...

Code: Select all

; AHK code
;IniRead, temp_ini, % A_WorkingDir "\settings.ini", general

temp_ini=		;to simplify test...
(
var1 = some text
scheme = {Tab}
)

MsgBox % scheme := StrSplit(StrSplit(temp_ini,"`n")[2]," = ")[2]
				. StrSplit(StrSplit(temp_ini,"`n")[1]," = ")[2]

F1::
SendInput, %scheme%
Return

Re: Stuck with simple problem

Posted: 16 Nov 2018, 17:00
by Leli196
That works, but then the scheme effectively is coded in the AHK code. I want the scheme in the Ini file.

What do mean with "instead of reading every var from it"? How do I just read every var from a Ini file?

Re: Stuck with simple problem

Posted: 17 Nov 2018, 00:49
by CyL0N
Leli196 wrote:
16 Nov 2018, 17:00
That works, but then the scheme effectively is coded in the AHK code. I want the scheme in the Ini file.
I just did that for simplicity,perhaps this is a bit clearer...
>settings.ini

Code: Select all

var1 = some text
scheme = {Tab}
>script

Code: Select all

IniRead, temp_ini, % A_WorkingDir "\settings.ini", general
MsgBox % scheme := StrSplit(StrSplit(temp_ini,"`n")[2]," = ")[2]
				. StrSplit(StrSplit(temp_ini,"`n")[1]," = ")[2]
F1::
SendInput, %scheme%
Return
Leli196 wrote:
16 Nov 2018, 17:00
What do mean with "instead of reading every var from it"? How do I just read every var from a Ini file?
Either use IniRead for every var you intend to use:

Code: Select all

scheme := rINI("general","scheme") . rINI("general", "var1")
F2::SendInput, %scheme%
rINI(ini_section, ini_key, ini_file:="settings.ini"){
	IniRead, thisKey, %ini_file%, %ini_section%, %ini_key%
	Return ( thisKey != "ERROR" ? thisKey : "")
}
Or something like these:

https://autohotkey.com/board/topic/2551 ... -ini-file/
https://autohotkey.com/board/topic/6741 ... -ini-file/

Re: Stuck with simple problem

Posted: 26 Nov 2018, 13:21
by Leli196
Excuse the late reply.

I don't think you understand what I want to achieve. I want to send scheme with SendInput to a window. Scheme contains not only keypresses like {Tab} or {Enter}, but it shall also contain the contents of other variables, which are pieces of text.

Code: Select all

; INI file
[general]
var1 = some
var2 = text
var3 = for
var4 = example

scheme1 = {Tab}var1{Enter}var4
scheme2 = {Up}{Left}var2{Right}var3var4
scheme3 = var3{Enter}var2{Enter}

Code: Select all

; AHK code
IniRead, temp_ini, % A_WorkingDir "\settings.ini", general
Loop, Parse, temp_ini, `r`n
	value := StrSplit(A_LoopField, "="), var := value[1], %var% := value[2]

F1::
SendInput, %scheme1%
Return
So the way you suggested would require me to construct the scheme variables in the AHK code, which I do not want.

Edit:
So I could just do it like this, but I want the scheme vars in the INI file:

Code: Select all

; INI file
[general]
var1 = some
var2 = text
var3 = for
var4 = example

Code: Select all

; AHK code
IniRead, temp_ini, % A_WorkingDir "\settings.ini", general
Loop, Parse, temp_ini, `r`n
	value := StrSplit(A_LoopField, "="), var := value[1], %var% := value[2]

F1::
scheme1 := "{Tab}" var1 "{Enter}" var4
scheme2 := "{Up}{Left}" var2 "{Right}" var3 var4
scheme3 := var3 "{Enter}" var2 "{Enter}"
SendInput, %scheme1%
Return

Re: Stuck with simple problem  Topic is solved

Posted: 26 Nov 2018, 21:55
by CyL0N
Ahh, that's easy enough...

Code: Select all

[general]
var1 = some text
scheme = var1 {Tab} var2 var3
var2 = some more text
var3 = .

Code: Select all

MsgBox % scheme := rINI("general","scheme")
F2::SendInput, %scheme%

;for var's referenced in key's to autoexpand, they must be space delimited...
rINI(ini_section, ini_key, ini_file:="settings.ini", callBackFlag:=""){
	IniRead, thisKey, %ini_file%, %ini_section%, %ini_key%
	thisKey :=  thisKey <> "ERROR" ? thisKey : ""
	If callBackFlag ;for internal use to validate ini key values...
		Return thisKey
	;Expand variables inside ini key that was read...
	For k,v in StrSplit(thisKey, A_Space)
		retVal .= (thisV:=rINI(ini_section,v,ini_file,true)) ? " " thisV " " : " " v " "
	Return Trim(retVal)
}


Re: Stuck with simple problem

Posted: 02 Dec 2018, 20:45
by Leli196
Alright, thanks a lot.