It is currently April 30th, 2024, 8:46 pm

Please tell me an example of luascript

Get help with creating, editing & fixing problems with skins
Kotofanchik
Posts: 95
Joined: March 15th, 2024, 7:30 pm

Please tell me an example of luascript

Post by Kotofanchik »

Please tell me an example of luascript that erases the contents of a specific file. if this is possible. And also an example of writing to a file. I tried to understand how it works using examples here and in https://docs.rainmeter.net/manual/lua-scripting/ and didn’t understand anything and didn’t get any results
RicardoTM
Posts: 271
Joined: December 28th, 2022, 9:30 pm
Location: México

Re: Please tell me an example of luascript

Post by RicardoTM »

Kotofanchik wrote: April 3rd, 2024, 4:01 pm Please tell me an example of luascript that erases the contents of a specific file. if this is possible. And also an example of writing to a file. I tried to understand how it works using examples here and in https://docs.rainmeter.net/manual/lua-scripting/ and didn’t understand anything and didn’t get any results
I would use cmd/powershell for that instead, using a runcommand measure.

Here you can find 2 commands to do it.
User avatar
balala
Rainmeter Sage
Posts: 16190
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Please tell me an example of luascript

Post by balala »

Kotofanchik wrote: April 3rd, 2024, 4:01 pm Please tell me an example of luascript that erases the contents of a specific file. if this is possible.
It definitely is possible. For instance create the Script.lua file into the @Resources folder of your config (obviously you can use any valid name, just make sure to set the proper name in the [MeasureLuaScript] measure of yoru skin):

Code: Select all

function EmptyFile(file)
	io.open(file,"w"):close()
end
Create any kind of (simple) meter in your skin and add the following option to it: LeftMouseUpAction=[&MeasureLuaScript:EmptyFile('x://PathOfMyFile//MyFileToEmpty.txt')]. Make sure to replace the red path with the proper path of your file you want to empty through the lua script.
Kotofanchik wrote: April 3rd, 2024, 4:01 pm And also an example of writing to a file.
Add the following function to the previously created Script.lua file, beside the already existing EmptyFile function:

Code: Select all

function WriteFile(file, stringTowrite)
	filewrite = io.open(file, "w")
	filewrite:write(stringTowrite)
	filewrite:close()
end
Add the following RightMouseUpAction option to the same meter to which you've added the above LeftMouseUpAction: RightMouseUpAction=[&MeasureLuaScript:WriteFile('x://PathOfMyFile//MyFileToEmpty.txt', 'Text to be written into the file')]. Once again take care to replace the above red colored file path and name with the path and name of the file you'd like to alter with the lua script and the green text with the text you'd like to write into the file.
If you did this, when you left click the meter you've added the above two options to, the appropriate file is emptied, while when you right click it, the Text to be written into the file text is written to the file.
Obviously if you simply right click the meter (without left clicking it first), the Text to be written into the file string is written to the file, without emptying it first.

Please let me know if you can't get any of the above functions to work.
Kotofanchik
Posts: 95
Joined: March 15th, 2024, 7:30 pm

Re: Please tell me an example of luascript

Post by Kotofanchik »

I won't be able to test it quickly. I'm slow to figure it out. Thanks for answers.
User avatar
balala
Rainmeter Sage
Posts: 16190
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Please tell me an example of luascript

Post by balala »

Kotofanchik wrote: April 3rd, 2024, 5:44 pm I won't be able to test it quickly. I'm slow to figure it out. Thanks for answers.
Alright, I attach an installer, which installs an example skin (called ExampleLua\ExampleSkin.ini). If you install it, when Rainmeter restarts, you get a simple skin, showing a Left click to empty / Right click to write string. Check the content of the included @Resources\TextFile.txt file (this is the file which will be altered by the Lua script and at first it contains a Check this file string). If you left click the skin, the @Resources\TextFile.txt file is emptied. If you right click the string, you get written in the file the Welcome to Lua string, as written as the second parameter of the WriteFile(...) function, in the RightMouseUpAction option of the [MeterToWorkWithFile] meter of the skin.
Try to check all this, give it a few tries, alter the content of the skin and so on. If any question arises, feel free to come back and ask.
You do not have the required permissions to view the files attached to this post.
Kotofanchik
Posts: 95
Joined: March 15th, 2024, 7:30 pm

Re: Please tell me an example of luascript

Post by Kotofanchik »

Thank you. The finished skins are the best way for me to understand how things work.
User avatar
balala
Rainmeter Sage
Posts: 16190
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Please tell me an example of luascript

Post by balala »

Kotofanchik wrote: April 3rd, 2024, 6:59 pm Thank you. The finished skins are the best way for me to understand how things work.
You're welcome. Check it and come back if any question arises.
Kotofanchik
Posts: 95
Joined: March 15th, 2024, 7:30 pm

Re: Please tell me an example of luascript

Post by Kotofanchik »

Yes, everything works, the file is written text and the text is erased. Thank you.
User avatar
balala
Rainmeter Sage
Posts: 16190
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Please tell me an example of luascript

Post by balala »

Kotofanchik wrote: April 3rd, 2024, 7:57 pm Yes, everything works, the file is written text and the text is erased. Thank you.
:thumbup:
Kotofanchik
Posts: 95
Joined: March 15th, 2024, 7:30 pm

Re: Please tell me an example of luascript

Post by Kotofanchik »

For the future, is it possible to read text in a file located not in the Rainmeter folders? In the browser cache for example?