So.. In short.. The trick is to initialize with formatting and then read / write as normal... I'm posting the script here separately ( it is part of my ahk framework: https://bitbucket.org/Acecool/acecoolah ... k/overview ) but you'll still need the string.Column code which is used to set up nice looking text-based columns separated by tabs / spaces and which always lines up - AcecoolAHK_Framework\classes\library_string.ahk for the function( s ) required. If you want to have spaces so all equals are lined up you need to set the value with formatting and add a second =s ( It needs to be = from what I've tried out... all other characters [ even if you try substr to remove the last char, etc.. it doesn't retain formatting or the keys double up, or whatever... with = it stays and works ] ) at the end of all the spaces so they aren't auto trimmed.. then after you call IniWrite with the formatted key, you call it right away again without formatting and the equals will be removed but the spacing will remain the same...
This is a very simple yet very effective trick which improves readability of the ini file 10 fold all without having to re-invent the wheel but I do add helper functions ( because you can't use expressions / function-calls / etc.. in the 1 line AHK commands ) for Set / SetDefault / Get / Delete / Exists
Here's what my old config.ini looked like:
And this is what it looks like now:
Code: Select all
[AcecoolAHK_Framework] __this__=config.ini Author=Josh 'Acecool' Moser Website=https://bitbucket.org/Acecool/acecoolahk_framework version=0.4.19 [__BIN__] NirCmd=_assets\bin\NirCmd_x64\nircmd.exe [DoubleTapSpeeds] ToggleCapsLockViaShift=150 [AudibleAlerts] NUMLOCK_ON=C:\WINDOWS\Media\Speech On.wav NUMLOCK_OFF=C:\WINDOWS\Media\Speech Off.wav SCROLLLOCK_ON=C:\WINDOWS\Media\Speech On.wav SCROLLLOCK_OFF=C:\WINDOWS\Media\Speech Off.wav CAPSLOCK_ON=C:\WINDOWS\Media\Speech On.wav CAPSLOCK_OFF=C:\WINDOWS\Media\Speech Off.wav [AudioDeviceCycler] OnSwitchSound=*64 OnSwitchTextToSpeech=yes Selected=0 Devices=2 Out_0=Speakers Mic_0=Microphone Out_1=Headset Mic_1=Headset Microphone [Cloud] DropBox_UserID=12345678 PrivateScreenshotsPath=C:\Users\Acecool\AppData\Roaming\..\..\Dropbox\Screenshots\ PublicScreenshotsPath=C:\Users\Acecool\AppData\Roaming\..\..\Dropbox\Public\Screenshots\
For people that use scripts and aren't into the coding side of things ( even know it is a config file ), some people get confused with all of the jumble.. But with it organized like this, nice and clean... Everything is easily identifiable and easy to manage...
Code: Select all
[AcecoolAHK_Framework] __this__ = config.ini Author = Josh 'Acecool' Moser Website = https://bitbucket.org/Acecool/acecoolahk_framework version = 0.4.20 [__BIN__] NirCmd = _assets\bin\NirCmd_x64\nircmd.exe [DoubleTapSpeeds] ToggleCapsLockViaShift = 150 [AudibleAlerts] NUMLOCK_ON = C:\WINDOWS\Media\Speech On.wav NUMLOCK_OFF = C:\WINDOWS\Media\Speech Off.wav SCROLLLOCK_ON = C:\WINDOWS\Media\Speech On.wav SCROLLLOCK_OFF = C:\WINDOWS\Media\Speech Off.wav CAPSLOCK_ON = C:\WINDOWS\Media\Speech On.wav CAPSLOCK_OFF = C:\WINDOWS\Media\Speech Off.wav [AudioDeviceCycler] OnSwitchSound = *64 OnSwitchTextToSpeech = yes Selected = 0 Devices = 2 Out_0 = Rosewill Headset Mic_0 = Rosewill Headset Mic Out_1 = Desktop Speakers Mic_1 = Webcam Mic [Cloud] DropBox_UserID = 12345678 PrivateScreenshotsPath = C:\Users\Acecool\Dropbox\Screenshots\ PublicScreenshotsPath = C:\Users\Acecool\Dropbox\Public\Screenshots\
If you want to try my framework which lets you drag and drop your scripts into various folders and have them automatically run / included when the framework loads as part of the framework or standalone ( you can even drop your favorite executables in the addons_exe directory and whitelist them and have them launch when the framework launches; use _assets\_setup\setup.bat in admin mode to point AHK\Lib to AcecoolAHK_Framework\libraries\ as symbolic link and replace Documents\AutoHotkey.ahk with the launcher so AutoHotkey.exe will launch the framework - note: a backup is made of your Lib\ dir and AutoHotkey.ahk in Documents if you have one -- https://bitbucket.org/Acecool/acecoolah ... k/overview )
Here's the config script: https://bitbucket.org/Acecool/acecoolah ... config.ahk
which needs functions from:
https://bitbucket.org/Acecool/acecoolah ... ry_ini.ahk
https://bitbucket.org/Acecool/acecoolah ... y_file.ahk
https://bitbucket.org/Acecool/acecoolah ... string.ahk
And possibly a few others...But the basic logic is in the library_config and library_ini files but you will need string.FormatColumn ( which basically adds spaces to line up text and truncates what goes over so all of the config.ini vars will line up properly )...