It is currently June 16th, 2021, 11:55 am

Problem with Inline Lua file writing

Help with creating, editing & fixing problems with skins
NEPTUNIUMLI
Posts: 4
Joined: June 1st, 2021, 6:59 am

Problem with Inline Lua file writing

Post by NEPTUNIUMLI »

I am new to Rainmeter and wish to write a desktop memo. I plan to mix up the Input Text plugin and use Lua to write it into a TXT file. However, as I followed the documentation of Rainmeter I met some problems with Lua scripting. Can someone please help me fix it?

#Modification(should have stated the question clearer): The typer functions alright, I can change the text and the variable 'FirstVar' in Type_History.inc is changed accordingly as well. However, the TXT file I intend to modify stays blank. So I guess there was something wrong with the Lua code.

I realized that the variable #FirstVar is placed in a different file. Will this prevent lua obtaining it?


Here's my code

Code: Select all

[Rainmeter]
Update=1000
AccurateText=1
DynamicWindowSize = 1
@include = Type_History.inc

[Metadata]
License=Creative Commons Attribution - Non - Commercial - Share Alike 3.0

[lua]
Measure = Script
ScriptFile = #@#StoreHistory.lua
OutputFile = #CURRENTPATH#Type_History.txt
Disabled = 1

[Variables]
FontSize = 20

[Meter_Background]
; Background
Meter=Image
SolidColor=100,200,230,200
W=320
H=105

[Measure_Input]
; Main window
Measure = PlugIn
Plugin = InputText
SolidColor = 76A0E8FF
FontColor = 255,255,255,205
FontFace = 微软雅黑
FontSize = #FontSize#
X = 5
Y = 5
H = 30
W = 300
DefaultValue = #FirstVar#
Command1 = !WriteKeyValue Variables FirstVar "$UserInput$" "#CURRENTPATH#Type_History.inc"
Command2 = !Refresh #CURRENTCONFIG#

[Meter_WriteKeyValue]
Meter = STRING
X = 5
Y = 5
H = 100
W =300
FontSize = #FontSize#
FontFace = 微软雅黑
FontColor = 255,255,255,255
AntiAlias = 1
ClipString = 1
Text = #FirstVar#
LeftMouseUpAction = [!CommandMeasure "Measure_Input" "ExecuteBatch 1-2"] [!CommandMeasure lua "Store("[#FirstVar]")"]
DynamicVariables = 1
and the Lua code

Code: Select all

function Initialize()

	filePath = SKIN:MakePathAbsolute(SELF:GetOption('OutputFile'))

end

function Update()

end

function Store(stringarg)

          local File = io.open(filePath , 'a+')

          File:write(stringarg)

          File:close()
end
Last edited by NEPTUNIUMLI on June 2nd, 2021, 12:39 pm, edited 2 times in total.
User avatar
CodeCode
Posts: 390
Joined: September 7th, 2020, 2:24 pm

Re: Problem with Inline Lua file writing

Post by CodeCode »

Try converting all of your files to UTF-8

Sometimes that creates issues.

But that is just a shot in the dark.
ƈǟռ'ȶ ʄɨӼ ɨȶ ɨʄ ɨȶ ǟɨռ'ȶ ɮʀօӄɛ - ʊռʟɛֆֆ ɨȶ ɨֆ ɨռ ƈօɖɛ.
NEPTUNIUMLI
Posts: 4
Joined: June 1st, 2021, 6:59 am

Re: Problem with Inline Lua file writing

Post by NEPTUNIUMLI »

CodeCode wrote: June 2nd, 2021, 7:08 am Try converting all of your files to UTF-8

Sometimes that creates issues.

But that is just a shot in the dark.
I have my skin.ini/inc documents in UTF16 LE, and the same for my .lua scripts. I do have the txt file in utf8 encoding, but nothing changed. No text appeared in the text document.
I checked the RM documentation for this issue so I guess I've followed orders?
Reading and writing external Unicode files in Lua
As noted above, you must always save the Lua script .lua file encoded as UTF-16 to use Unicode with Lua and Rainmeter. However, there may be times you want to read or write to external files from your Lua script. If you want to use Unicode with these external files, things have to be handled in a particular way.

I mentioned earlier that Lua always treats text as 8-bit single-byte characters. What the means in this context is that in effect Lua "internally" views all text as UTF-8, even though we always have the .lua file encoded as UTF-16 to work with Rainmeter. So Lua requires that any text file it reads or creates itself be encoded in UTF-8 to handle Unicode characters properly.
Thanks for the advice though.
User avatar
death.crafter
Posts: 176
Joined: April 24th, 2021, 8:13 pm

Re: Problem with Inline Lua file writing

Post by death.crafter »

NEPTUNIUMLI wrote: June 2nd, 2021, 6:19 am I am new to Rainmeter and wish to write a desktop memo. I plan to mix up the Input Text plugin and use Lua to write it into a TXT file. However, as I followed the documentation of Rainmeter I met some problems with Lua scripting. Can someone please help me fix it?

Here's my code

Code: Select all

[Measure_Input]
...

DefaultValue = #FirstVar#
Command1 = !WriteKeyValue Variables FirstVar "$UserInput$" "#CURRENTPATH#Type_History.inc"
Command2 = !Refresh #CURRENTCONFIG#

[Meter_WriteKeyValue]
...

LeftMouseUpAction = [!CommandMeasure "Measure_Input" "ExecuteBatch 1-2"] [!CommandMeasure lua "Store("[#FirstVar]")"]
The problem here is this:

Code: Select all

Command1 = !WriteKeyValue Variables FirstVar "$UserInput$" "#CURRENTPATH#Type_History.inc"
Command2 = !Refresh #CURRENTCONFIG#
LeftMouseUpAction = [!CommandMeasure "Measure_Input" "ExecuteBatch 1-2"] [!CommandMeasure lua "Store("[#FirstVar]")"]

And the cherry on the top is !WriteKeyValue.
The value changed by !WriteKeyValue doesn't count until skin is refreshed.

Correct way:
ONE

Code: Select all

Command1 =[!SetVariable FirstVar "$UserInput$"][!WriteKeyValue Variables FirstVar "[#FirstVar]" "#CURRENTPATH#Type_History.inc"]

Code: Select all

LeftMouseUpAction = [!CommandMeasure "Measure_Input" "ExecuteBatch 1"][!CommandMeasure lua "Store("[#FirstVar]")"][!Refresh]
TWO

Code: Select all

Command1 =[!SetVariable FirstVar "$UserInput$"]
Command2=[!CommandMeasure lua "Store("[#FirstVar]")"]

Code: Select all

LeftMouseUpAction = [!CommandMeasure "Measure_Input" "ExecuteBatch 1"][!CommandMeasure lua "Store("[#FirstVar]")"][!Refresh]

Code: Select all

LeftMouseUpAction = [!CommandMeasure "Measure_Input" "ExecuteBatch 1-2"]

Code: Select all

----------------------------------------------redundant functions... remove them if you have no use of them
function Initialize()

	filePath = SKIN:MakePathAbsolute(SELF:GetOption('OutputFile'))

end

function Update()
end
----------------------------------------------

function Store(stringarg)

	  local filePath = SKIN:MakePathAbsolute(SELF:GetOption('OutputFile'))

          local File = io.open(filePath , 'a+')

          File:write(stringarg)

          File:close()
          
          SKIN:Bang('!WriteKeyValue', 'Variables', 'FirstVar', stringarg, SKIN:GetVariable('CURRENTPATH')..'Type_History.inc')
          
          SKIN:Bang('!Refresh')
end
Second way is optional.
from the Realm of Death
NEPTUNIUMLI
Posts: 4
Joined: June 1st, 2021, 6:59 am

Re: Problem with Inline Lua file writing

Post by NEPTUNIUMLI »

death.crafter wrote: June 2nd, 2021, 11:37 am The problem here is this:

Code: Select all

Command1 = !WriteKeyValue Variables FirstVar "$UserInput$" "#CURRENTPATH#Type_History.inc"
Command2 = !Refresh #CURRENTCONFIG#
LeftMouseUpAction = [!CommandMeasure "Measure_Input" "ExecuteBatch 1-2"] [!CommandMeasure lua "Store("[#FirstVar]")"]

And the cherry on the top is !WriteKeyValue.
The value changed by !WriteKeyValue doesn't count until skin is refreshed.

Correct way:
ONE

Code: Select all

Command1 =[!SetVariable FirstVar "$UserInput$"][!WriteKeyValue Variables FirstVar "[#FirstVar]" "#CURRENTPATH#Type_History.inc"]

Code: Select all

LeftMouseUpAction = [!CommandMeasure "Measure_Input" "ExecuteBatch 1"][!CommandMeasure lua "Store("[#FirstVar]")"][!Refresh]
TWO

Code: Select all

Command1 =[!SetVariable FirstVar "$UserInput$"]
Command2=[!CommandMeasure lua "Store("[#FirstVar]")"]

Code: Select all

LeftMouseUpAction = [!CommandMeasure "Measure_Input" "ExecuteBatch 1"][!CommandMeasure lua "Store("[#FirstVar]")"][!Refresh]

Code: Select all

LeftMouseUpAction = [!CommandMeasure "Measure_Input" "ExecuteBatch 1-2"]

Code: Select all

----------------------------------------------redundant functions... remove them if you have no use of them
function Initialize()

	filePath = SKIN:MakePathAbsolute(SELF:GetOption('OutputFile'))

end

function Update()
end
----------------------------------------------

function Store(stringarg)

	  local filePath = SKIN:MakePathAbsolute(SELF:GetOption('OutputFile'))

          local File = io.open(filePath , 'a+')

          File:write(stringarg)

          File:close()
          
          SKIN:Bang('!WriteKeyValue', 'Variables', 'FirstVar', stringarg, SKIN:GetVariable('CURRENTPATH')..'Type_History.inc')
          
          SKIN:Bang('!Refresh')
end
Second way is optional.
Thank you but I think that may not be where the problem is because the typer built on my desktop runs pretty smoothly. The TXT modification function is where I encountered a problem.
I tried the code suggested but the txt file is still unmodified
User avatar
death.crafter
Posts: 176
Joined: April 24th, 2021, 8:13 pm

Re: Problem with Inline Lua file writing

Post by death.crafter »

NEPTUNIUMLI wrote: June 2nd, 2021, 12:34 pm Thank you but I think that may not be where the problem is because the typer built on my desktop runs pretty smoothly. The TXT modification function is where I encountered a problem.
I tried the code suggested but the txt file is still unmodified
In that case you have to use "a" mode of file I/O.

local File = io.open(filePath, 'a')
from the Realm of Death
NEPTUNIUMLI
Posts: 4
Joined: June 1st, 2021, 6:59 am

Re: Problem with Inline Lua file writing

Post by NEPTUNIUMLI »

Problem solved! The .lua file should be in UTF8 rather than UTF16
Big thanks to everybody in this post.

RM Code:

Code: Select all

[Rainmeter]
Update=1000
AccurateText=1
DynamicWindowSize = 1
@include = Type_History.inc

[Metadata]
License=Creative Commons Attribution - Non - Commercial - Share Alike 3.0

[lua]
Measure = Script
ScriptFile = #CURRENTPATH#Type_History.lua
OutputFile = #CURRENTPATH#Type_History.txt
Disabled = 1

[Variables]
FontSize = 20

[Meter_Background]
; Background
Meter=Image
SolidColor=100,200,230,200
W=320
H=105

[Measure_Input]
; Main window
Measure = PlugIn
Plugin = InputText
SolidColor = 76A0E8FF
FontColor = 255,255,255,205
FontFace = 微软雅黑
FontSize = #FontSize#
X = 5
Y = 5
H = 37
W = 300
FocusDismiss = 0
DefaultValue = #FirstVar#
Command1 = [!WriteKeyValue Variables FirstVar "$UserInput$" "#CURRENTPATH#Type_History.inc"]
Command2 = !Refresh

[Meter_WriteKeyValue]
Meter = STRING
X = 5
Y = 5
H = 100
W =300
FontSize = #FontSize#
FontFace = 微软雅黑
FontColor = 255,255,255,255
AntiAlias = 1
ClipString = 1
Text = #FirstVar#
LeftMouseUpAction = [!CommandMeasure "Measure_Input" "ExecuteBatch 1-2"]
DynamicVariables = 1

[Shape_Save]
Meter = Image
SolidColor = 255,145,112,200
Y = 108
H = 40
W = 78
LeftMouseUpAction = [!CommandMeasure lua "Store('[#FirstVar]')"]

[Meter_Save]
Meter = STRING
X = 5
Y = 110
H = 30
W = 50
FontSize = #FontSize#
FontFace = 微软雅黑
FontColor = 255,255,255,255
AntiAlias = 1
Text = Save!
lua Script:

Code: Select all

function Store(stringarg)

          local filePath = SKIN:MakePathAbsolute(SELF:GetOption('OutputFile'))

          local File = io.open(filePath , 'a')

          local time = os.date("%Y-%m-%d")
          local Hour = os.date("%H")
          File:write("\n")
          File:write(time.."    Hour"..Hour.."   ---- "..stringarg)

          File:close()
end
User avatar
CodeCode
Posts: 390
Joined: September 7th, 2020, 2:24 pm

Re: Problem with Inline Lua file writing

Post by CodeCode »

NEPTUNIUMLI wrote: June 4th, 2021, 5:45 am Problem solved! The .lua file should be in UTF8 rather than UTF16
CodeCode wrote: June 2nd, 2021, 7:08 am Try converting all of your files to UTF-8

Sometimes that creates issues.

But that is just a shot in the dark.
:confused:
ƈǟռ'ȶ ʄɨӼ ɨȶ ɨʄ ɨȶ ǟɨռ'ȶ ɮʀօӄɛ - ʊռʟɛֆֆ ɨȶ ɨֆ ɨռ ƈօɖɛ.