It is currently March 29th, 2024, 1:33 am

Unstable Write Code

Discuss the use of Lua in Script measures.
soundpulse
Posts: 2
Joined: March 6th, 2019, 1:18 pm

Unstable Write Code

Post by soundpulse »

I have created this code to write InputText to a file yesterday, and it only works on random occasions, please help :? :?

Lua Code (LuaWriteFile.lua)

Code: Select all

function Initialize()
	sFileToWrite = SELF:GetOption('FileToWrite')
end -- function Initialize

function Update()

	hWritingFile = io.open(sFileToWrite)
	if not hWritingFile then
		print('LuaWriteFile: unable to open file at ' .. sFileToWrite)
		return
	end
	sContents = SELF:GetOption('Contents')

    return sContents
end -- function Update

function writeToFile(Contents)

    file = io.open("C:\\Users\\total\\OneDrive\\Documents\\Rainmeter\\Skins\\TestSkin\\Main\\input.txt", "a")
	file:write(Contents, "\n")
    file:close()
end
Rainmeter Code

Code: Select all

[MeasureLuaScriptWrite]
Measure=Script
ScriptFile=#CURRENTPATH#LuaWriteFile.lua
FileToWrite=#CURRENTPATH#input.txt

[MeterUpdate]
Meter=String
X=0
Y=175
FontSize=12
FontColor=120,120,120,255
StringStyle=Bold
SolidColor=255,255,255,210
AntiAlias=1
Text=🔎
LeftMouseUpAction=[!CommandMeasure MeasureInput "ExecuteBatch 1-2"]
Padding=3,3,3,5

[MeterInputLabel]
Meter=String
FontFace=Tanuki Permanent Marker
X=33
Y=175
W=170
H=18
FontSize=12
FontColor=120,120,120,255
SolidColor=255,255,255,210
Padding=5,6,5,4
AntiAlias=1
Text=Update List

[MeasureInput]
Measure=Plugin
Plugin=InputText
X=38
Y=179
W=171
H=20
FontFace=Tanuki Permanent Marker
FontSize=12
FontColor=50,50,50,255
SolidColor=233,233,233,230
AntiAlias=1
FocusDismiss=1
DefaultValue=#CurrentInput#
OnDismissAction=[!SetVariable CurrentInput ""]
Command1=!CommandMeasure "MeasureLuaScriptWrite" "writeToFile('$UserInput$')"
Command2=[!SetVariable CurrentInput "[MeasureInput]"][!UpdateMeter "*"][!Redraw]
This code is a modified version of this https://forum.rainmeter.net/viewtopic.php?t=18060 btw, if that helps
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Unstable Write Code

Post by jsmorley »

Not quite sure what you are getting. That skin/script seems to always work for me.

I did make a small change to how the file is opened for append, to eliminate the hard-coded file name.

Code: Select all

function Initialize()
	sFileToWrite = SELF:GetOption('FileToWrite')
end -- function Initialize

function Update()

	hWritingFile = io.open(sFileToWrite)
	if not hWritingFile then
		print('LuaWriteFile: unable to open file at ' .. sFileToWrite)
		return
	end
	sContents = SELF:GetOption('Contents')

	return sContents
end -- function Update

function writeToFile(Contents)

	file = io.open(sFileToWrite, "a")
	file:write(Contents, "\n")
	file:close()
end

I would note that I can't get that emoji of the magnifying glass to work with the default Arial font, and the Unicode representation is outside the boundaries of the Basic Multilingual Plane, so I would be tempted to use Font Awesome instead. https://docs.rainmeter.net/manual/variables/character-variables/#FindingValues
But if that is working for you, good on you.

... Or you could just include an image file.

SearchLeft.png
SearchRight.png
You do not have the required permissions to view the files attached to this post.
soundpulse
Posts: 2
Joined: March 6th, 2019, 1:18 pm

Re: Unstable Write Code

Post by soundpulse »

jsmorley wrote: March 6th, 2019, 1:44 pm Not quite sure what you are getting. That skin/script seems to always work for me.

I did make a small change to how the file is opened for append, to eliminate the hard-coded file name.

...

I would note that I can't get that emoji of the magnifying glass to work with the default Arial font, and the Unicode representation is outside the boundaries of the Basic Multilingual Plane, so I would be tempted to use Font Awesome instead. https://docs.rainmeter.net/manual/variables/character-variables/#FindingValues But if that is working for you, good on you.
Updated the code for write and it seems to work again. Hope it lasts. Thanks a lot.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Unstable Write Code

Post by jsmorley »

On that magnifying glass, You might want to use

Text=⌕

or

Text=[\x2315]

Which is in the Basic Multilingual Plane and supported in most fonts.

It's a tad smaller than that emoji, so you will likely want to mess with the FontSize and Y-position / Padding to get it looking as you want.