It is currently April 19th, 2024, 2:10 am

Content of a random .txt file

Get help with creating, editing & fixing problems with skins
Mr Muffin
Posts: 7
Joined: September 8th, 2018, 2:48 pm

Content of a random .txt file

Post by Mr Muffin »

I'm trying to display the content of a random .txt file on my desktop.
I used the quote plugin and LuaTextFile.lua script

Config:
TehGreyFawkz.zip

Code: Select all

[Rainmeter]
Update=10000
Author=TehGreyFawkz


;;[MEASURES]

[MeasureRandomText]
Measure=Plugin
Plugin=QuotePlugin
PathName=C:\Users\Karol\Documents\Rainmeter\Skins\TehGreyFawkz\Note pad\c\


[MeasureLuaScript]
Measure=Script
ScriptFile="C:\Users\Karol\Documents\Rainmeter\Skins\TehGreyFawkz\Note pad\LuaTextFile.lua"
FileToRead=MeasureRandomText


;;[METERS]

[Background]
Meter=IMAGE
SolidColor=0,0,0,1
X=0
Y=1
W=200
H=30


[Title]
Meter=STRING
Text=C++
X=20
Y=1
H=15
W=150
StringStyle=Normal
FontColor=0,0,0
FontSize=40
FontFace=Disparador stencil
StringEffect=BORDER
FontEffectColor=255,255,255
AntiAlias=1

[Notes]
Meter=STRING
MeasureName=MeasureLuaScript
X=2
Y=32
W=400
H=570
FontColor=200,200,200
FontFace=Arial
FontSize=9
StringEffect=BORDER
FontEffectColor=0,0,0
StringAlign=LEFT
StringStyle=NORMAL
AntiAlias=1
ClipString=1
LuaTextFile.lua

Code: Select all

function Initialize()

	sFileToRead = SELF:GetOption('FileToRead')
	
end

function Update()

	hReadingFile = io.open(sFileToRead)
	if not hReadingFile then
		print('LuaTextFile: unable to open file at ' .. sFileToRead)
		return
	end
	
	sAllText = hReadingFile:read("*all")
	
	sAllText = string.gsub(sAllText, "\t", "     ")
	
	io.close(hReadingFile)
	
	return tostring(sAllText)	
	
end

And I ended up with "0":
moiYbfl - Imgur.png
When I replace "FileToRead=MeasureRandomText" with a direct path, it works fine:
FileToRead="C:\Users\Karol\Documents\Rainmeter\Skins\TehGreyFawkz\Note pad\c\Bool.txt"
3g0T6Lo - Imgur.png
I also checked the quote plugin and it's working fine too.
MeasureName=MeasureRandomText
Bez tytułu.png
I'm new to the Rainmeter scripting and I don't know what to do at this point :17flag
So I'll be thankfull for any help
You do not have the required permissions to view the files attached to this post.
Last edited by Mr Muffin on September 9th, 2018, 3:25 pm, edited 2 times in total.
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Content of a random .txt file

Post by jsmorley »

Mr Muffin wrote:I'm trying to display the content of a random .txt file on my desktop.
I used the quote plugin and LuaTextFile.lua script

Code: Select all

[Rainmeter]
Update=10000
Author=TehGreyFawkz


;;[MEASURES]

[MeasureRandomText]
Measure=Plugin
Plugin=QuotePlugin
PathName=C:\Users\Karol\Documents\Rainmeter\Skins\TehGreyFawkz\Note pad\c\


[MeasureLuaScript]
Measure=Script
ScriptFile="C:\Users\Karol\Documents\Rainmeter\Skins\TehGreyFawkz\Note pad\LuaTextFile.lua"
FileToRead=MeasureRandomText


;;[METERS]

[Background]
Meter=IMAGE
SolidColor=0,0,0,1
X=0
Y=1
W=200
H=30


[Title]
Meter=STRING
Text=C++
X=20
Y=1
H=15
W=150
StringStyle=Normal
FontColor=0,0,0
FontSize=40
FontFace=Disparador stencil
StringEffect=BORDER
FontEffectColor=255,255,255
AntiAlias=1

[Notes]
Meter=STRING
MeasureName=MeasureLuaScript
X=2
Y=32
W=400
H=570
FontColor=200,200,200
FontFace=Arial
FontSize=9
StringEffect=BORDER
FontEffectColor=0,0,0
StringAlign=LEFT
StringStyle=NORMAL
AntiAlias=1
ClipString=1
LuaTextFile.lua

Code: Select all

function Initialize()

	sFileToRead = SELF:GetOption('FileToRead')
	
end

function Update()

	hReadingFile = io.open(sFileToRead)
	if not hReadingFile then
		print('LuaTextFile: unable to open file at ' .. sFileToRead)
		return
	end
	
	sAllText = hReadingFile:read("*all")
	
	sAllText = string.gsub(sAllText, "\t", "     ")
	
	io.close(hReadingFile)
	
	return tostring(sAllText)	
	
end

And I ended up with "0":
https://imgur.com/a/19V213b

When I replace "FileToRead=MeasureRandomText" with a direct path, it works fine:
FileToRead="C:\Users\Karol\Documents\Rainmeter\Skins\TehGreyFawkz\Note pad\c\Bool.txt"
https://imgur.com/a/wsnamYt

I also checked the quote plugin and it's working fine too.
I'm new to the Rainmeter scripting and I don't know what to do at this point :17flag
So I'll be thankfull for any help
Try this:

Code: Select all

[MeasureLuaScript]
Measure=Script
ScriptFile="C:\Users\Karol\Documents\Rainmeter\Skins\TehGreyFawkz\Note pad\LuaTextFile.lua"
FileToRead=[MeasureRandomText]
DynamicVariables=1
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Content of a random .txt file

Post by jsmorley »

The only place that a measure name is "assumed" to be the value of a measure, and not just some random text, is in the Formula option of a Calc measure. This is because text / strings can't be part of a mathematical formula in any case, so it is safe to assume that you mean the value of a named measure. Aside from that, you must tell Rainmeter that you want the value of the measure by using it as a [SectionVariable], and set DynamicVariables=1 where you are using it.

https://docs.rainmeter.net/manual/variables/section-variables/
Mr Muffin
Posts: 7
Joined: September 8th, 2018, 2:48 pm

Re: Content of a random .txt file

Post by Mr Muffin »

Thanks for fast reply but I still seem to have the "0" thing
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Content of a random .txt file

Post by jsmorley »

Mr Muffin wrote:Thanks for fast reply but I still seem to have the "0" thing
What are you getting in the Log from Lua?

I suspect the issue is that when you use value of the path you retrieved as a string to io.open() it is seeing the "\" path separators as "escapes" and throwing them away. If that is the case, we can use a string.gsub to turn "\" into "\\" which will then make them a single literal "\" character when used as a string, or you can just change the path in the skin to use "/" instead of "\", which actually works fine for paths in Rainmeter.
Mr Muffin
Posts: 7
Joined: September 8th, 2018, 2:48 pm

Re: Content of a random .txt file

Post by Mr Muffin »

I'm getting:
"LuaTextFile: unable to open file at "

Can you forward me to a site where I can learn how to manipulate string this way?
User avatar
balala
Rainmeter Sage
Posts: 16144
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Content of a random .txt file

Post by balala »

Mr Muffin, please pack the whole config and upload it to can check what's going on.
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Content of a random .txt file

Post by kyriakos876 »

I had this issue sometime ago and I saw a space in my "path". That explains why a direct path works. Try checking your path for any spaces.. also, always use " " as mentioned above. Spaces can be a real bitch I'm telling ya
Mr Muffin
Posts: 7
Joined: September 8th, 2018, 2:48 pm

Re: Content of a random .txt file

Post by Mr Muffin »

I updated the main post with the config

Also, I removed spaces in my paths. It didn't help
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Content of a random .txt file

Post by jsmorley »

Skin:

Code: Select all

;;___________________________________________________________
;;___________________________________________________________

[Rainmeter]
Update=1000
Author=TehGreyFawkz


;;[MEASURES]

[MeasureRandomText]
Measure=Plugin
Plugin=QuotePlugin
PathName=#CURRENTPATH#c
UpdateDivider=10
OnChangeAction=[!UpdateMeasure MeasureLuaScript]

[MeasureLuaScript]
Measure=Script
ScriptFile=#CURRENTPATH#LuaTextFile.lua
FileToRead=[MeasureRandomText]
DynamicVariables=1
UpdateDivider=-1

;;[METERS]

[Background]
Meter=IMAGE
SolidColor=0,0,0,1
X=0
Y=1
W=200
H=30


[Title]
Meter=STRING
Text=C++
X=20
Y=1
H=15
W=150
StringStyle=Normal
FontColor=0,0,0
FontSize=40
FontFace=Disparador stencil
StringEffect=BORDER
FontEffectColor=255,255,255
AntiAlias=1

[Notes]
Meter=STRING
MeasureName=MeasureLuaScript
X=2
Y=32
W=400
H=570
FontColor=200,200,200
FontFace=Arial
FontSize=9
StringEffect=BORDER
FontEffectColor=0,0,0
StringAlign=LEFT
StringStyle=NORMAL
AntiAlias=1
ClipString=1
LuaTextFile.lua:

Code: Select all

function Update()

	sFileToRead = SELF:GetOption('FileToRead')
	
	hReadingFile = io.open(sFileToRead)
	if not hReadingFile then
		print('LuaTextFile: unable to open file at ' .. sFileToRead)
		return
	end
	
	sAllText = hReadingFile:read("*all")
	
	sAllText = string.gsub(sAllText, "\t", "     ")
	
	io.close(hReadingFile)
	
	return tostring(sAllText)	
	
end
So the biggest issue was in the Lua, where you are getting the value of FileToRead from SELF: in the Initialize() function of the script. The problem is, when the skin / script is first initialized, the Quote plugin hasn't yet done its work, so there is no value yet for FileToRead. Plus, don't forget that FileToRead is going to periodically change. If you move that into the Update() function of the script then all is well.

That was really the only problem all along...

The only other thing that I changed is the "timing" of when the file is opened and read. You only want to do that when the Quote plugin has a "different" file name returned, and certainly not on every update of the skin. That is just going to thrash your hard drive for no good reason.

So I set Update in Rainmeter to the default 1000 (you seldom want to increase that from the default, control timing with UpdateDivider on measures) and added an UpdateDivider of 10 to the Quote plugin measure, so it gets a new file every 10 seconds. Use that UpdateDivider to make that timing what you want.

Then I added UpdateDivider=-1 to the Lua script measure, as you don't really ever (other than the very first update of the skin) want that to update on its own. You want it to update when the value of the Quote plugin measure changes. You want the Quote plugin measure to "drive" the Lua script measure...

So I added an OnChangeAction to the Quote plugin measure, which will force an update of the Lua script measure.

Other than that, you pretty much had it right. Good job! I did change the fully qualified fixed paths to stuff in the skin to use built-in variables, which is both shorter, and would allow you to distribute the skin to other users.

I don't have your font, so it looks like crap for me, but should work fine for you.
1.jpg
This was never about spaces in paths or files names, while we don't recommend that, it's fine.
This was never about "quotes", you almost never need them in Rainmeter except in bangs.
This was never about "\" being treated as an escape. While that can be an issue in Lua, it wasn't in this case.
You do not have the required permissions to view the files attached to this post.