How to edit a text file?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
chonga2520
Posts: 2
Joined: 22 Mar 2023, 00:03

How to edit a text file?

Post by chonga2520 » 22 Mar 2023, 00:21

I use EqualizerAPO to change the sound of my system. It is easily configurable from a config.txt file.

File Destination: C:\Program Files\EqualizerAPO\config\config.txt
File Contents:

Code: Select all

Filter: ON LSC Fc 170 Hz Gain 0 dB Q 0.44
Filter: ON HSC Fc 2096 Hz Gain 0 dB Q 0.44
Preamp: 0 dB
All i want to do is increment/decrement the gain value on both the filters when i press Ctrl+4/5 respectively. How do i go about this?

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to edit a text file?

Post by swagfag » 22 Mar 2023, 02:15

Code: Select all

#Requires AutoHotkey v2.0.2

^4::ChangeGain(+1)
^5::ChangeGain(-1)

ChangeGain(value) {
	static path := 'C:\Program Files\EqualizerAPO\config\config.txt'

	fileContents := FileOpen(path, 'r').Read()

	static gainDigitsRegex := 'm)Gain \K\d+'
	GetCurrentGain() {
		if !RegExMatch(fileContents, gainDigitsRegex, &M)
			throw Error('couldnt find "Gain <numbers>" inside the file, aborting', -1, path)

		return M[0] 
	}
	
	newFileContents := RegExReplace(fileContents, gainDigitsRegex, GetCurrentGain() + value)

	FileOpen(path, 'w').Write(newFileContents)
}

chonga2520
Posts: 2
Joined: 22 Mar 2023, 00:03

Re: How to edit a text file?

Post by chonga2520 » 22 Mar 2023, 03:07

Tested the code, and it works flawlessly for positive gain values, but the ChangeGain() function seems to break down when -ve values are introduced:
scrnsht.png
sry for flashbang lol
scrnsht.png (119.19 KiB) Viewed 293 times
(and yes i put the script in a separate config file. also changed paths accordingly.)

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to edit a text file?

Post by swagfag » 22 Mar 2023, 04:02

Code: Select all

ChangeGain(value) {
	static path := 'C:\Program Files\EqualizerAPO\config\config.txt'

	fileContents := FileOpen(path, 'r').Read()

	static gainDigitsRegex := 'm)Gain \K[-+]?[0-9]*\.?[0-9]+'
	GetCurrentGain() {
		if !RegExMatch(fileContents, gainDigitsRegex, &M)
			throw Error('couldnt find "Gain <numbers>" inside the file, aborting', -1, path)

		return M[0] 
	}
	
	newFileContents := RegExReplace(fileContents, gainDigitsRegex, Round(GetCurrentGain() + value, 2))

	FileOpen(path, 'w').Write(newFileContents)
}

Post Reply

Return to “Ask for Help (v2)”