Loop read edit box entries and IniWrite them with loop SOLVED Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
theimmersion
Posts: 181
Joined: 09 Jul 2016, 08:34
Location: Serbia

Loop read edit box entries and IniWrite them with loop SOLVED

23 Sep 2017, 18:06

So, ive been fiddling with this all day. Can you incrementally/loop write variables to files?

I can read them fine and populate edit boxes per entry.

Code: Select all

#Persistent
#SingleInstance, Force

IniRead, vReadIn, Config.inc, Variables, In, ERROR!
vReadInB := vReadIn-1

;%A_index%

Gui, Add, Text,, BOOKMARK NAME
Loop, %vReadIn%
{
	IniRead, vReadName, Bookmarks.txt, Variables, Bookmark%A_index%Name, ERROR!
	Gui , Add , Edit , xm w+130 vStoreName%A_index%, %vReadName%
}
Gui, Add, Text, ys, BOOKMARK ADDRESS
Loop, %vReadIn%
{
	IniRead, vReadAddress, Bookmarks.txt, Variables, Bookmark%A_index%Address, ERROR!
	Gui , Add , Edit , w+200 vStoreAddress%A_index%, %vReadAddress%
}
	Gui, Add, Button, xm+250 gSave, Save
	Gui, Add, Button, x+1 gGuiClose, Cancel
Gui , Show
Return

Save:
Gui, Submit, NoHide

Loop, %vReadIn%
IniWrite, %vStoreNameA_index%, Bookmarks.txt, Variables, Bookmark%A_index%Name
Loop, %vReadIn%
IniWrite, %vStoreAddressA_index%, Bookmarks.txt, Variables, Bookmark%A_index%Address

Goto,GuiClose
Return

GuiClose:
ExitApp
I just need a way to loop index read those edit boxes and save them to the file. :eh:
Last edited by theimmersion on 23 Sep 2017, 21:19, edited 1 time in total.
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Loop read edit box entries and IniWrite them with loop  Topic is solved

23 Sep 2017, 19:03

Hi theimmersion,

V: Variable. Associates a variable with a control. Immediately after the letter V, specify the name of a global variable (...)
source:associated output variable

In other words, to retrieve your associated output variable you should omit the v which is meaningful only the moment you create it as a GUI control option - just like you've correctly done it with your g-label Save btw. Besides, in this case, using pseudo-arrays (e.g. Bookmark%A_index%Address) it is necessary to use the percent sign:

Code: Select all

MyVar1 := 3
x := 1
MsgBox % MyVar%x%
see also:percent-space

Code: Select all

#Persistent
#SingleInstance, Force

MyIniFile := A_ScriptDir . "/test.ini" ; a file for test purposes

if not (FileExist(MyIniFile)) ; create the test if it doesn't exist
{
	FileAppend,
	(LTrim
	[Variables]
	Bookmark1Name=a
	Bookmark2Name=b
	Bookmark3Name=c
	Bookmark4Name=d
	Bookmark1Address=e
	Bookmark2Address=f
	Bookmark3Address=g
	Bookmark4Address=h
	), % MyIniFile, utf-16 ; IniRead and IniWrite rely on the external functions to read and write values. These functions support Unicode only in UTF-16 files
}
IniRead, OutputVarSection, % MyIniFile, Variables ; Omit the Key parameter to read an entire section
pairs := StrSplit(OutputVarSection, "`n") ; separates the string into an array of substrings using the "`n" (linefeed) as delimiter.

Gui, Add, Text,, BOOKMARK NAME

Loop % pairs.length()/2 ; loops the array
{
	IniRead, vReadName, % MyIniFile, Variables, Bookmark%A_index%Name, ERROR!
	Gui, Add, Edit, xm w+130 vStoreName%A_index%, %vReadName%
}

Gui, Add, Text, ys, BOOKMARK ADDRESS

Loop % pairs.length()/2
{
	IniRead, vReadAddress, % MyIniFile, Variables, Bookmark%A_index%Address, ERROR!
	Gui, Add, Edit, w+200 vStoreAddress%A_index%, %vReadAddress%
}

Gui, Add, Button, xm+250 gSave, Save
Gui, Add, Button, x+1 gGuiClose, Cancel
Gui, Show
return ; end of the auto-execute section

Save: ; save subroutine
Gui, Submit, NoHide
Loop % pairs.length()/2
{
; % StoreAddress%a_index% ---> first a_index is replaced by the index of the current iteration (for example StoreAddress1) which is itself evalauted as an expression using the percent sign
IniWrite, % StoreName%a_index%, % MyIniFile, Variables, Bookmark%A_index%Name
IniWrite, % StoreAddress%a_index%, % MyIniFile, Variables, Bookmark%A_index%Address
}
sleep, 2000
run % "notepad " . MyIniFile ; run notepad to check changes
GuiClose:
ExitApp
my scripts
User avatar
theimmersion
Posts: 181
Joined: 09 Jul 2016, 08:34
Location: Serbia

Re: Loop IniRead edit box entries and loop IniWrite them back SOLVED

23 Sep 2017, 21:12

Jesus... i totally forgot about it. :problem: What a waste of topic space i am. :crazy:
That v and g for goto in the gui made more than enough trouble for me a while back. I just got back to making some new ahk scripts and forgot about those nig nags. xD

Big big thanks!

So, since i posted the not working one, should at least post the working one.
I hated when i find a topic that has something that could help me but the examples are missing and instead someone just says, i figured it out. xD
Mind boggling! xD

EDIT II
This is the currently final version of the bookmark editor/manager backend script for a Rainmeter skin of mine.
Can be re-purposed i guess.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

;	theimmersion
;	I will be comenting the code as best i can and only key points that i think should be mentioned.
;	The rest should be explorable or self explanitory. :)
;	You can always ask or suggest. :)
;	Hope its useful for some people.

;	Standard procedure.
#Persistent
#SingleInstance, Force

;	This will be the global to set when something changes so we dont call uneeded functions.
;	Especially true if there are read and writes of files involved and with loop.
;	The files can get big and re-reading or re-writing them is pointless.
vDataChanged = 0

;	Read the configuration file for how many entries there are in a file that will be used to modify.
;	In this case, "In" stands for Item Number that is being used to render the amount of bookmarks by a different app.
;	This information will be used to add appropriate amout of entries and to populate them with the data.
;	Example: 2 Items = 2 sets of data in "Bookmars.txt". Every set contains two variables, the name and the address of the bookmark.
;	So in the "Config.inc" it looks like this:
;	[Variables]
;	In=2	<- This will be used to set the count for all loops.
;	The data that is being manipulated is in "Bookmarks.txt" and looks like this:
;	[Variables]
;	Bookmark1Name=Google
;	Bookmark1Address=www.google.com
;	Bookmark2Name=AHK
;	Bookmark2Address=www.autohotkey.com
IniRead, vReadIn, Config.inc, Variables, In, Error reading variable!

Gui,+AlwaysOnTop

;	Add a label.
Gui, Add, Text, h20, #
;	Add X amount of numbers in a column direction.
Loop, %vReadIn%
{
	Gui, Add, Text, y+10 h+16, %A_index%
}

;	Add a label.
Gui, Add, Text, ys h20, BOOKMARK NAME
;	Add X amount of Edit boxes and populate them with data from "Bookmarks.txt".
Loop, %vReadIn%
{
	IniRead, vReadName, Bookmarks.txt, Variables, Bookmark%A_index%Name,
	Gui , Add , Edit , w+130 h20 vStoreName%A_index%, %vReadName%
}

;	Add a label.
Gui, Add, Text, ys h20, BOOKMARK ADDRESS
;	Add X amount of Edit boxes and populate them with data from "Bookmarks.txt".
Loop, %vReadIn%
{
	IniRead, vReadAddress, Bookmarks.txt, Variables, Bookmark%A_index%Address,
	Gui , Add , Edit , w+400 h20 vStoreAddress%A_index%, %vReadAddress%
}

;	Add a label.
Gui, Add, Text, ys h20, #
;	Add X amount of Buttons named in numbered order to be used for switching bookmarks around.
Loop, %vReadIn%
{
	Gui , Add , Button , w+20 h20 gSwitch, %A_index%
}

;	Adds controls and info text to tell user what the script is doing. Pretty self explanitory.
Gui, Add, Button, xm+250 h20 gSave, Save	;	Button save all the entries to "Bookmarks.txt"
Gui, Add, Button, x+1 h20 gGuiClose, Close	;	Button to close/exit script.
Gui, Add, Button, x+1 h20 gRefresh, Refresh	;	Button to re-read all the data again.

Gui, Add, Text, ys h20, SWAP				;	Label
Gui, Add, Text, w+50 h20 +Wrap vShowSwapA, 	;	Show first selected item to be swaped in text form.
Gui, Add, Button, h20 gSwitchEntries, Swap	;	Button to swap selected entries.
Gui, Add, Text, h20, 						;	White space, delimeter.
Gui, Add, Text, w+150 h20 vInfo,			;	Info of what script is doing in text form. Ex: Saved, Not saved, Error etc...
Gui, Add, Text, xp+50 ys h20, 				;	White space, delimeter.
Gui, Add, Text, w+50 h20 +Wrap vShowSwapB, 	;	Show second selected item to be swaped in text form.
Gui, Add, Button, h20 gClearSwitch, Clear	;	Button to clear the swap selections.

;	Show gui, very important! xD
Gui , Show
Return

Switch:
	If (vSwitchA = "")	;	If nothing is yet selected, enable user to select essentially a row number for swaping.
	{
		MouseGetPos,,, id, NN
		ControlGetText, vSwitchA, %NN%, ahk_id %id%
		GuiControl,,ShowSwapA, Swap: %vSwitchA%
	}
	Else If (vSwitchB = "")	;	Select the second row to be swaped with.
	{
		MouseGetPos,,, id, NN
		ControlGetText, vSwitchB, %NN%, ahk_id %id%
		GuiControl,,ShowSwapB, With: %vSwitchB%
	}
Return

ClearSwitch:	;	Clear the selection and information if user made a mis-click.
	vSwitchA := ""
	vSwitchB := ""
	GuiControl,,ShowSwapA,
	GuiControl,,ShowSwapB,
Return

SwitchEntries:	;	Switch out the entires for each other by first placing all data in a temporary container.
	Gui, Submit, NoHide
	TempNameA := StoreName%vSwitchA%
	TempAddressA := StoreAddress%vSwitchA%
	TempNameB := StoreName%vSwitchB%
	TempAddressB := StoreAddress%vSwitchB%
	GuiControl,,StoreName%vSwitchA%,%TempNameB%
	GuiControl,,StoreAddress%vSwitchA%,%TempAddressB%
	GuiControl,,StoreName%vSwitchB%,%TempNameA%
	GuiControl,,StoreAddress%vSwitchB%,%TempAddressA%
	vSwitchA := ""
	vSwitchB := ""
	GuiControl,,ShowSwap,
	vDataChanged = 1	;	Now we changed something so we know we can enable saving.
Return

;	We modified the "Bookmarks.txt" externally with a text editor or we swaped entries but dont want to commit-
;	and want to re-read for new edits without exiting or reloading script.
Refresh:
	Loop, %vReadIn%
	{
		IniRead, vReadName, Bookmarks.txt, Variables, Bookmark%A_index%Name,
		GuiControl,,StoreName%A_index%,%vReadName%
		IniRead, vReadAddress, Bookmarks.txt, Variables, Bookmark%A_index%Address,
		GuiControl,,StoreAddress%A_index%,%vReadAddress%
	}
	vDataChanged = 0	;	Now we know everything is back to original state with no changes so we disable saving.
	
	;	Tell the user it has been refreshed for less confusion if user didnt change anything but wasnt sure and refreshed anyway-
	;	so he knows the command worked.
	GuiControl,,Info,Refreshed
;	Reload
Return

Save:	;	Save all the data.
	If (vDataChanged = 1)	;	Check if anything is modifed first before commiting to wirting to file.
	{
		Gui, Submit, NoHide

		Loop, %vReadIn%
		{
			vNameData := StoreName%A_index%
			IniWrite, %vNameData%, Bookmarks.txt, Variables, Bookmark%A_index%Name
			IniRead, vReadName, Bookmarks.txt, Variables, Bookmark%A_index%Name,
			GuiControl,,StoreName%A_index%,%vReadName%
		}
		Loop, %vReadIn%
		{
			vAddressData := StoreAddress%A_index%
			IniWrite, %vAddressData%, Bookmarks.txt, Variables, Bookmark%A_index%Address
			IniRead, vReadAddress, Bookmarks.txt, Variables, Bookmark%A_index%Address,
			GuiControl,,StoreAddress%A_index%,%vReadAddress%
		}

		;	This is for the external app that is using data from "Bookmarks.txt" to render them on screen.
		;	This app sets the item count variable "In" that it renders in "Config.inc" and the variable sets for names and addresses in "Bookmarks.txt".
		;	Like we dont know what Rainmeter is or does. xD But can be used by any program i guess?
		;	After changes are made and saved to "Bookmarks.txt", this command will refresh the app to show the changes on screen.
		;	Remove if not used for that program.
		SendMessage, 0x111, 4001,,, ahk_class RainmeterMeterWindow
		
		GuiControl,,Info,Saved	;	Tell the user that we saved new data.
		vDataChanged = 0	;	We saved the changes so we revert back the state to not modified so user cant save twice, second being the same data.
	}
	Else
		GuiControl,,Info,Not saved, nothing edited.	;	Tell user that he dint change anything so no need to write same data to file again.
;	Goto,GuiClose	;	Optional, we can either close the program after each save or we can leave it open for user to inspect one last time.
Return

;	Close app. Also very important.
GuiClose:
	ExitApp
	MsgBox, Error closing script!
Return
















;	Lots of white space because i like to do stuff around the middle of my screen but if no white space here, cant scroll enough. xD
;	Im weird...
I tried to comment it as best as i can. Dont do much tutorials, hope this helps someone out. I found so many good help in random posts that had exactly what i needed.
Sometimes even had something i never knew i needed. xD

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww and 326 guests