First of all to say is, the ini file itself is not touched in anyway. Other than the AutoHotkey built-in commands, these ini_ functions works on the variables only. Reaso is superior speed if needed more than one operation, especially at loops etc, because the file is read and saved once.
And yes, you have to save the ini content back to file.
In example instead of
X Code:
ini_insertKey(ini_filepath ...)
you should work on variable
O Code:
ini_insertKey(ini_content ...)
Here is a working example:
Code:
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
inifile=%A_scriptDir%\Particulars.ini
GoSub, init
Return ; End of Autoexecute section
; Initialization. This is done once at startup.
init:
inifile := ini_load(inicontent,inifile)
If (ErrorLevel = 1)
{
MsgBox, 48, File not found, The does not exist or was not loaded successfully. Script terminates.
ExitApp
}
Else
{
keys := ini_getAllKeyNames(inicontent,"name")
; Count only keys with name format "name1".
; It could have other keys, which should not be counted.
Loop, Parse, keys, `,
{
If (A_LoopField = "name " . A_Index)
{
KeyCount++
}
}
}
Return
; Hotkey Ctrl+Alt+c copies text to clipboard and adds that content to ini file.
; Clipboard is restored.
^!c::
; Save clipboard before operation begins.
ClipBackup := ClipboardAll
Clipboard := ""
Send, ^c
ClipWait, 0.5, 1
If (ErrorLevel = 0)
{
; Continue only on success and restore original clipboard.
textFromClip := Clipboard
Clipboard := ClipBackup
; Process with adding the key.
KeyCount++
keyToAdd := "name " . KeyCount . "=". textFromClip
MsgBox, 36, debug, Add key?`n----`n%keyToAdd%`n----
IfMsgBox, Yes
{
ini_insertKey(inicontent, "name", keyToAdd)
GoSub, SaveFile
}
}
Else
{
Clipboard := ClipBackup
}
Return
; Overwrites the file.
SaveFile:
FileDelete, %inifile%
FileAppend, %inicontent%, %inifile%
Return
Its not extensively tested, but it works. The Hotkey Ctrl+Alt+c adds the copied content to file. A MsgBox askes before that is done.