Loop .ini file and separate values?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Tyson
Posts: 4
Joined: 17 Nov 2018, 03:32

Loop .ini file and separate values?

21 Nov 2018, 15:57

Hi,
I started with AutoHotKey like 3 weeks ago and it's quite fun. ;)
Been downloading scripts and trying to puzzle together with each other and so on.
What I want to do is a easy template script, where you can select a template by clicking on it in a menu or typing the hotstring command (Key value).

Right now im stuck on this Gui where I can edit or delete templates.
How do I loop out the vaules from the .ini file and separate them (Section, Key and Value) into this form/Gui? :crazy:

Code: Select all

Gui, Add, DropDownList, x12 y9 w150 h20 , DropDownList ; List of Section's
Gui, Add, Button, x172 y9 w130 h20 , Save
Gui, Add, Button, x372 y9 w140 h20 , Delete
Gui, Add, ListBox, x12 y39 w150 h200 , ListBox ; List of Keys from Section selected in DropDownList.
Gui, Add, Edit, x172 y39 w340 h200 , Edit ; Key Value based on Key selected in Listbox.
Gui, Show, w526 h254, Untitled GUI
The .ini file looks like this;

Code: Select all

[Template_Common]
Phone=List
Members=A B C
Error=code
[Template_Mail]
Send=Email
Reply=Email
CC=Email
[Template_Request]
Call=Phone number
I also need to know how to check when creating a new template if the Key name already exist as the Key name needs to be unique for the hotstrings to work.
SunAndSuch
Posts: 46
Joined: 05 Oct 2015, 12:11

Re: Loop .ini file and separate values?

22 Nov 2018, 14:46

Code: Select all

iniContent:=
(
"[Template_Common]
Phone=List
Members=A B C
Error=code
[Template_Mail]
Send=Email
Reply=Email
CC=Email
[Template_Request]
Call=Phone number"
)
FileDelete,iniFile.ini
FileAppend,% iniContent,iniFile.ini
oIni:={}
oKeys:={}
IniRead,sectNames,iniFile.ini
for a,b in oSectNames:=StrSplit(sectNames,"`n","`r")
{	oIni[b]:={}
	IniRead,Section,iniFile.ini,% b
	ddl.=b "|"
	for c,d in StrSplit(Section,"`n","`r")
	{	RegExMatch(d,"^([^=]*?)=(.*)",match)
		if oKeys.HasKey(match1)
			MsgBox,% "Key already exits: """ match1 """"
		else
		{	oKeys[match1]:=match2
			oIni[b,match1]:=match2
		}
	}
}
for a,b in oIni[oSectNames[1]]
	Lb.=a "|"
Gui, Add, DropDownList, x12 y9 w150 r3 Choose1 vvSection ggSelectSect,% SubStr(ddl,1,-1) ; List of Section's
Gui, Add, Button, x172 y9 w130 h20 , Save
Gui, Add, Button, x372 y9 w140 h20 , Delete
Gui, Add, ListBox, x12 y39 w150 h200 vvLb ggInsertVal,% SubStr(Lb,1,-1) ; List of Keys from Section selected in DropDownList.
Gui, Add, Edit, x172 y39 w340 h200 vvEdit, Edit ; Key Value based on Key selected in Listbox.
Gui, Show, w526 h254, Untitled GUI
return
GuiClose:
ExitApp
gInsertVal:
Gui Submit, NoHide
GuiControl,,vEdit,% oKeys[vLb]
return
gSelectSect:
Gui Submit, NoHide
Lb:="|"
for a,b in oIni[vSection]
	Lb.=a "|"
GuiControl,,vLb,% SubStr(Lb,1,-1)
return
Windows 10, Ahk v1 x64-bit.
Tyson
Posts: 4
Joined: 17 Nov 2018, 03:32

Re: Loop .ini file and separate values?

24 Nov 2018, 07:09

Thanks!
My plan was to have another GUI to create new templates on, but maybe its possible to do something like this?

Code: Select all

Gui, Add, Button, x172 y9 w90 h20 , New
Gui, Add, Button, x292 y9 w90 h20 , Save
Gui, Add, Button, x412 y9 w90 h20 , Delete
When pressing the New button it should remove the ListBox of Key's and replace it with a Edit where I can type in a new Key name and it should be saved under the selected category with the value also.

I tried to combine your script with another to make line breaks but could not make it work.
The value in the Keys can be multiple lines, so I need to convert like <br> with a new line when reading it into the form or pasting the template anywhere.
I also don't know how to save(update) selected key as I could not combine it with another script where I could. :D

Im bad at explaining but I'll try update this thread later today, but right now I need to get some food. :crazy:
Tyson
Posts: 4
Joined: 17 Nov 2018, 03:32

Re: Loop .ini file and separate values?

24 Nov 2018, 11:41

Im stuck again. :D
I need help with write;
new template to correct section (also check so no other key have the same value).
update selected template.
and delete template.
I managed to write a new template but as soon as I used more lines it broke the ini file... :angel:

The GUI is kinda done now I think;

Code: Select all

/*
iniContent:=
(
"[Template_Common]
Phone=List
Members=A B C
Error=code
[Template_Mail]
Send=Email
Reply=Email
CC=Email
[Template_Request]
Call=Phone number"
)
FileDelete,iniFile.ini
FileAppend,% iniContent,iniFile.ini
*/
oIni:={}
oKeys:={}
IniRead,sectNames,iniFile.ini
for a,b in oSectNames:=StrSplit(sectNames,"`n","`r")
{	oIni[b]:={}
	IniRead,Section,iniFile.ini,% b
	ddl.=b "|"
	for c,d in StrSplit(Section,"`n","`r")
	{	RegExMatch(d,"^([^=]*?)=(.*)",match)
		if oKeys.HasKey(match1)
			MsgBox,% "Key already exits: """ match1 """"
		else
		{	oKeys[match1]:=match2
			oIni[b,match1]:=match2
		}
	}
}
for a,b in oIni[oSectNames[1]]
	Lb.=a "|"
Gui, Add, DropDownList, x12 y9 w150 r3 Choose1 vvSection ggSelectSect,% SubStr(ddl,1,-1) ; List of Section's
Gui, Add, Button, x172 y9 w90 h20 ggNewTemplate vvNewTemplate, New Template
Gui, Add, Button, x292 y9 w90 h20 ggUpdateTemplate vvUpdateTemplate, Update
Gui, Add, Button, x292 y9 w90 h20 ggSaveTemplate vvSaveTemplate, Save
Gui, Add, Button, x412 y9 w90 h20 ggDeleteTemplate vvDeleteTemplate, Delete
Gui, Add, ListBox, x12 y39 w150 h200 vvLb ggInsertVal,% SubStr(Lb,1,-1) ; List of Keys from Section selected in DropDownList.
Gui, Add, Edit, y39 x12 h20 w150 vvNewKey
Gui, Add, Edit, x172 y39 w340 h200 vvEdit ; Key Value based on Key selected in Listbox.
Gui, Show, w526 h254, Untitled GUI
GuiControl, hide, vNewKey
GuiControl, hide, vSaveTemplate
return

gInsertVal:
Gui Submit, NoHide
GuiControl,,vEdit,% oKeys[vLb]
return

gSelectSect:
Gui Submit, NoHide
Lb:="|"
for a,b in oIni[vSection]
	Lb.=a "|"
GuiControl,,vLb,% SubStr(Lb,1,-1)
return

gNewTemplate:
if hide {
	GuiControl, Show, vLb
	GuiControl, hide, vNewKey
	GuiControl,,vEdit,% oKeys[vLb]
	GuiControl,,vNewTemplate, New Template
	GuiControl, Show, vUpdateTemplate	
	GuiControl, Hide, vSaveTemplate
	GuiControl, Show, vDeleteTemplate
}
else {
	GuiControl, Hide, vLb
	GuiControl, show, vNewKey
	GuiControl,,vEdit
	GuiControl,,vNewTemplate, Template list
	GuiControl, hide, vUpdateTemplate	
	GuiControl, Show, vSaveTemplate
	GuiControl, Hide, vDeleteTemplate
}
hide:=not hide
return

gUpdateTemplate:
Gui, Submit, NoHide
MsgBox, 1,Update Template?, Template: %vLb%`n%vEdit%
IfMsgBox OK
    MsgBox You pressed OK.
else
    MsgBox You pressed Cancel.
return

gSaveTemplate:
Gui, Submit, NoHide
MsgBox, 1,Save Template?, Category: %vSection%`nKey: %vNewKey%`n%vEdit%
IfMsgBox OK
    MsgBox You pressed OK.
else
    MsgBox You pressed Cancel.
return

gDeleteTemplate:
Gui, Submit, NoHide
MsgBox, 4,Delete Template?, Are you sure you want to delete template: %vLb%?
	IfMsgBox Yes
		MsgBox You pressed Yes.
	else
		MsgBox You pressed No.
	return

GuiClose:
ExitApp

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Theda and 267 guests