It is currently March 28th, 2024, 5:31 pm

(Modified) How to empty text file? > Specify File Path

Get help with creating, editing & fixing problems with skins
gkmaz9
Posts: 67
Joined: August 26th, 2019, 10:42 am

(Modified) How to empty text file? > Specify File Path

Post by gkmaz9 »

I want to empty test.txt...and It's almost done!
However, I have never used Lua.
I'm not sure how to modify the file path.
I'd like to locate it as a resource folder, not as a folder containing Lua.
@resources\Test.txt





Lua Source

Code: Select all

function Initialize()
   FilePath = SKIN:MakePathAbsolute('Test.txt')
   local File = io.open(FilePath, 'w')
   File:write('[variables]')
   File:close()
end -- function Initialize
Clear.ini

Code: Select all

[meterdata]

[rainmeter]
Update=1000

[Variables]
Url=file://C:\Users\USER\Documents\Rainmeter\Skins\Tree\@Resources\Test.txt

[MeasureScript]
Measure=Script
ScriptFile="#CURRENTPATH#Test.lua"
FileName=C:\Users\USER\Documents\Rainmeter\Skins\Tree\@Resources\Test.txt
UpdateDivider=-1

[Button]
Meter=String
X=50
Y=0
W=50
H=50
DynamicVariables=1
SolidColor=0,255,255,50
LeftMouseUpAction=!Execute [!CommandMeasure MeasureScript]


[Showtxt]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=#Url#
RegExp=(?siU)^(.*)$
StringIndex=1
RegExpSubstitute=1

[OutputShowtxt]
meter=String
MeasureName=Showtxt
FontSize=12
FontColor=255,255,255,255
SolidColor=255,255,255,30
DynamicVariables = 1 
Text=%1
X=150
Y=0
W=200
H=200

@resources\Text.txt

Code: Select all

[Variables]
1=a
2=b
3=c
4=d
5=e
Last edited by gkmaz9 on August 29th, 2019, 12:34 pm, edited 2 times in total.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: How to empty text file? (Lua)

Post by jsmorley »

Like this:

Test.ini

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Variables]
MyFile=#@#Test.inc

[Lua]
Measure=Script
ScriptFile=#CURRENTPATH#Test.lua
Disabled=1

[Button]
Meter=Image
X=50
Y=0
W=50
H=50
SolidColor=0,255,255,50
DynamicVariables=1
LeftMouseUpAction=[&Lua:EmptyIt('#MyFile#')]

[Showtxt]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=file://#MyFile#
RegExp=(?siU)^(.*)$
StringIndex=1

[OutputShowtxt]
Meter=String
MeasureName=Showtxt
FontSize=12
FontColor=255,255,255,255
SolidColor=255,255,255,30
AntiAlias=1
Text=%1
X=150
Y=0
W=200
H=200
Test.inc:

Code: Select all

[Variables]
1=a
2=b
3=c
4=d
5=e
Test.lua:

Code: Select all

function EmptyIt(fileArg)

	filePath = SKIN:MakePathAbsolute(fileArg)
	print('Emptying file: '..filePath)
	local fileHandle = io.open(filePath, 'w')
	fileHandle:close()

end
https://docs.rainmeter.net/manual/lua-scripting/inline-lua/

There is no need to write an empty string to the file. The act of opening it with 'w' will create it if it does not exist, and truncate it if it does.
gkmaz9
Posts: 67
Joined: August 26th, 2019, 10:42 am

Re: (Modified) How to empty text file? > Specify File Path

Post by gkmaz9 »

I got a reply while I was correcting my writing.
I understand how to do.
Thank you for your help. :thumbup:
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: (Modified) How to empty text file? > Specify File Path

Post by jsmorley »

You most certainly don't want to do this in Initialize() in the Lua, as then it will empty the file the instant the skin is loaded or refreshed. You don't want that. You want this in a function() that you can call on demand.

You can do it by defining the file name in the Lua Script measure as you have started to do, and use the SELF:() function to get the file name from the measure in the script, but I prefer just passing the name to the script as a part of calling the function() with Inline Lua.

Inline Lua is insanely cool...
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: (Modified) How to empty text file? > Specify File Path

Post by jsmorley »

Do keep in mind that if you are going to also be "writing" to this .inc file with Lua, that Lua can't deal with UTF-16 (Unicode) text files, so you will want to be careful that you don't write any characters to it that are outside the 255-character ASCII set. If you do, it will become the UTF-8 w/o BOM format, and then Rainmeter won't read it properly.

The rub here is that Rainmeter wants things like .inc files to be either ANSI (vanilla, only the ASCII character set) or UTF-16 (16-bit Unicode) or it won't read them. Lua on the other hand wants only ANSI or UTF-8 (8-bit Unicode) or it won't read them.

So it's hard to use Lua to really "manage" the .inc files you might use in Rainmeter with @Include.

Be careful you don't go down the rabbit-hole here.
gkmaz9
Posts: 67
Joined: August 26th, 2019, 10:42 am

Re: (Modified) How to empty text file? > Specify File Path

Post by gkmaz9 »

Lua is very difficult, but it seems to be fun.
I'll play with it more.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: (Modified) How to empty text file? > Specify File Path

Post by jsmorley »

gkmaz9 wrote: August 29th, 2019, 12:40 pm Lua is very difficult, but it seems to be fun.
I'll play with it more.
It really is a lot of fun. As programming languages go, it's actually pretty simple, but certainly more "geeky" than native Rainmeter .ini code is.

Rule #1 with Lua is to remember that it is ALWAYS case-sensitive, so you can't ever be lazy about that.
gkmaz9
Posts: 67
Joined: August 26th, 2019, 10:42 am

Re: (Modified) How to empty text file? > Specify File Path

Post by gkmaz9 »

I'll keep it in mind.

I am currently developing an interesting scheduler.
It has this function.
1 Pressing a icon for multiple words(things that I do often) will automatically record it(word) in the scheduler.
2. Organize by time zone and organize into text files by date.
3. The number of actions is recorded by clicking on the icon.

All you have to do is look at the "Google Calendar(Time unit)" where time and schedule are automatically recorded with just one click.

However, there are many things that cannot be done with the rain meter function alone.
Scripts are often longer, and many functions are missing.
I want to learn Lua and finish skin neatly.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: (Modified) How to empty text file? > Specify File Path

Post by jsmorley »

Very ambitious. Good luck, but be aware that I won't be able to go on this journey with you. I can try to answer very specific questions about how to do this or that, but won't and can't replicate what you are trying to do.

Just keep in mind the above about file "encoding". You can quickly hit a wall that there is no way around.

If there is any possibility that you are going to be dealing with any characters outside the ASCII character set, you will have challenges trying to read and write with Lua that can then be shared by the Rainmeter skin.

The only common denominator will be WebParser, which can read any format. So you can create or edit UTF-8 files in Lua, and then read them with WebParser. You won't be able to read them with @Include or QuotePlugin in Rainmeter. You can write to files in Rainmeter with !WriteKeyValue, but in that case the file must already exist, and if the file is UTF-8, so Lua can interact with it, nothing but WebParser will be able to read it in Rainmeter.

https://docs.rainmeter.net/tips/unicode-in-rainmeter/

So the safest rule-of-thumb is to never read or write to any .inc (include) or .ini (skin) files with Lua. You can read or write to UTF-8 files to your hearts content in Lua, and read the results in Rainmeter with WebParser. Just don't touch anything you want to read with @Include or QuotePlugin with Lua. It's going to end in tears.
gkmaz9
Posts: 67
Joined: August 26th, 2019, 10:42 am

Re: (Modified) How to empty text file? > Specify File Path

Post by gkmaz9 »

as you say, I won't depend too much on Forum.
Sometimes it will be enough to ask for help.

In fact,...The work is almost finished.
All that's left is...
1. Creating a text file with the current date and moving the logs in order

2. Making the 'bang command' work automatically

3. Arrange variables in chronological order

4. Modify Design
5. Simplify the script.(I don't know if I should.)

I will try the number 1, 2, 3 on my own.
If not, I'll ask for help here.

Of course, it would be nice to give a hint anytime. ;-)

Language problems are nerve-wracking.
In the Rainmeter script, I was writing using a "Codepage=".

Part of the script was written in a foreign language.
I'm a foreigner. The language was conveniently used.
I hope it don't crash in Lua. :-(
Post Reply