So i am making a voice assistant skin based on the plugin dgrace made.
https://forum.rainmeter.net/viewtopic.php?f=18&t=21942&p=130729&hilit=voicecontrol#p130729
But with a lot more functionality.
For the best result it'll requires a grammar file (.grxml) with key words to match the voice commands specified in the plugin measure.
To make it easy on a user of my skin i want to automate this process of renaming inside the .grxml text file.
Found a light command line utility called F.A.R.T. that can Find And Replace Text.
Works great within rainmeter and it looks something like this:
[#@#exe\Fart\fart.exe "#@#VoiceControl\RmCommands.grxml" "OLD TEXT" "NEW TEXT"]
But i wanna ditch this method because if more than one line contain the words OLD TEXT, all of them will be replaced.
So i started looking around for something that could simply replace an entire line of text, just by line number.
Weeell this is where i get lost because of the lack of coding skills :/
Found some lua scripts in the Rainmeter documentation that looks promising.
https://docs.rainmeter.net/snippets/read-write-file/
But i have no idea how to use it.
Any advice or example how i could implement would be appreciated.
Thanks.
It is currently April 28th, 2024, 12:20 pm
Replacing line in a text file?
-
- Posts: 424
- Joined: February 8th, 2015, 10:07 pm
- Location: EU, Denmark.
Replacing line in a text file?
Last edited by StArL0rd84 on January 7th, 2017, 2:23 pm, edited 1 time in total.
([mWorkTime] = 1 ? #Work# : ([mEnergyLoss:%] >= 70% ? #Chillmode# : </>))
-
- Developer
- Posts: 22631
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: Replacing line in a text file?
The Lua to simply replace an entire line of text in a file by line number is not difficult, however it would help if I knew a bit more about how you intend to implement this. The trickier bit may well be the presentation and interaction with the user.
-
- Posts: 424
- Joined: February 8th, 2015, 10:07 pm
- Location: EU, Denmark.
Re: Replacing line in a text file?
For example you could say "open games" where 'games' is a folder name in my folder-skin in the suite.jsmorley wrote:The Lua to simply replace an entire line of text in a file by line number is not difficult, however it would help if I knew a bit more about how you intend to implement this. The trickier bit may well be the presentation and interaction with the user.
and the folder name is obtained using rainfile when the user picked the folder.
so im imagining that with the action of picking the folder with rainfile, the .grxml will be updated with the new line containing the new folder name.
Line looks like this:
<item> games </item>
Line number 182
([mWorkTime] = 1 ? #Work# : ([mEnergyLoss:%] >= 70% ? #Chillmode# : </>))
-
- Developer
- Posts: 22631
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: Replacing line in a text file?
Not sure I entirely follow. Don't see how this is related to any particular "line number" in the text file. At first blush, still sounds like a global search-and-replace, which is what you don't want.StArL0rd84 wrote:For example you could say "open games" where 'games' is a folder name in my folder-skin in the suite.
and the folder name is obtained using rainfile when the user picked the folder.
so im imagining that with the action of picking the folder with rainfile, the .grxml will be updated with the new line containing the new folder name.
You have some example?
-
- Developer
- Posts: 22631
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: Replacing line in a text file?
Seems to me that if the lines are all like:
<item> games </item>
then a global search and replace will still be fine, you just have to search and replace on
<item> SomeText </item>
and replace it with
<item> SomeNewText </item>
<item> games </item>
then a global search and replace will still be fine, you just have to search and replace on
<item> SomeText </item>
and replace it with
<item> SomeNewText </item>
-
- Developer
- Posts: 22631
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: Replacing line in a text file?
Of course, I would still use Lua over an external executable in this case....
-
- Posts: 424
- Joined: February 8th, 2015, 10:07 pm
- Location: EU, Denmark.
Re: Replacing line in a text file?
ill try to explain the steps of events.jsmorley wrote:Not sure I entirely follow. Don't see how this is related to any particular "line number" in the text file. At first blush, still sounds like a global search-and-replace, which is what you don't want.
You have some example?
1. Folder is changed with rainfile.
["#RainFile#" "Folder" "Variables" "Fpath7" "#Var#" "C\"]
2. A path parser script obtains the name from the path.
Code: Select all
[mFolderPathParser7]
Measure=Script
ScriptFile=#@#lua\PathParser.lua
Input=#Fpath7#.exe
Output=Name
I know which line of the .grxml should be changed which is line number 182.
Cant the lua somehow index or count the lines and then replace?
([mWorkTime] = 1 ? #Work# : ([mEnergyLoss:%] >= 70% ? #Chillmode# : </>))
-
- Developer
- Posts: 22631
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: Replacing line in a text file?
Sure, that's not hard at all in Lua, but I don't see how it knows that anything is line number 182... I'm missing the logic here. I don't see what line 182 has to do with it.StArL0rd84 wrote:ill try to explain the steps of events.
1. Folder is changed with rainfile.
["#RainFile#" "Folder" "Variables" "Fpath7" "#Var#" "C\"]
2. A path parser script obtains the name from the path.
So im thinking whenever folder 7's name changes, the .grxml should be updated.Code: Select all
[mFolderPathParser7] Measure=Script ScriptFile=#@#lua\PathParser.lua Input=#Fpath7#.exe Output=Name
I know which line of the .grxml should be changed which is line number 182.
Cant the lua somehow index or count the lines and then replace?
-
- Developer
- Posts: 22631
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: Replacing line in a text file?
I'm not just being a pain in the neck here, it's important that we walk through the logic so we have a full trail from point A to point Z. Is my Lua going to look like:
function textReplace(lineNumber, oldText, newText)
or
function textReplace(oldText, newText)
It matters....
function textReplace(lineNumber, oldText, newText)
or
function textReplace(oldText, newText)
It matters....
-
- Posts: 424
- Joined: February 8th, 2015, 10:07 pm
- Location: EU, Denmark.
Re: Replacing line in a text file?
The script should not care what is at line 182. just delete it and write new text.
And you're not being a pain, i just dont know what you need from me
And you're not being a pain, i just dont know what you need from me
You do not have the required permissions to view the files attached to this post.
([mWorkTime] = 1 ? #Work# : ([mEnergyLoss:%] >= 70% ? #Chillmode# : </>))