Re: Random stringindex problem - for "quote of the day"
Posted: May 1st, 2012, 3:44 am
To follow up on my comment in my last post:
Using your text file exactly as it is as Test.txt...
Skin:
Test.lua:
What this does is initialize the Lua script when the skin is loaded or refreshed. During the initialization, the Lua reads in the entire text file, and creates a table with one full quote per "row" of the table. It adds Rainmeter #CRLF# (carriage return / line feed) commands where they are needed so the line breaking stays the same as the text file.
Then on each update of the Lua script (every 30 seconds in my sample) it generates a random number between 1 and the total number of quotes (#Quotes = Total number of items in the table) and sets the Text= option of the meter with the selected random quote.
One measure in the skin instead of 100 or more...
Let me know if you have any interest in this approach and need any questions answered about what or why or how it works.
Using your text file exactly as it is as Test.txt...
Skin:
Code: Select all
[Rainmeter]
Update=1000
DynamicWindowSize=1
[Variables]
Font=Arial
[MeasureLuaScript]
Measure=Script
ScriptFile=Test.lua
UpdateDivider=30
[QuoteText]
Meter=STRING
X=1
Y=1
W=800
H=600
FontColor=255,255,255,255
FontSize=15
StringAlign=Left
StringStyle=BOLD
FontFace=#Font#
StringEffect=Shadow
FontEffectColor=0,0,0,140
Antialias=1
ClipString=1
Text=Reading...
Code: Select all
function Initialize()
CURRENTPATH = SKIN:GetVariable('CURRENTPATH')
Quotes = {}
i = 0
for line in io.lines(CURRENTPATH..'Test.txt') do
if string.sub(line, 1, 7) == '<title>' then
i = i + 1
Quotes[i] = string.sub(line, 8)
else
Quotes[i] = Quotes[i]..'#CRLF#'..line
end
end
end -->Initialize
function Update()
rand = math.random(1, #Quotes)
SKIN:Bang('!SetOption QuoteText Text """'..Quotes[rand]..'"""')
return 'Quote number: '..rand
end -->Update
Then on each update of the Lua script (every 30 seconds in my sample) it generates a random number between 1 and the total number of quotes (#Quotes = Total number of items in the table) and sets the Text= option of the meter with the selected random quote.
One measure in the skin instead of 100 or more...
Let me know if you have any interest in this approach and need any questions answered about what or why or how it works.