It is currently April 20th, 2024, 2:17 pm

Help with Lua Code

Discuss the use of Lua in Script measures.
User avatar
donutmtoazt
Posts: 50
Joined: July 10th, 2011, 9:15 am
Location: Fresno, CA

Help with Lua Code

Post by donutmtoazt »

Hey guys, I've been looking through the forums for help on this, but I can't seem to find anything on it. I created a skin that would parse information from Kongregate (http://www.kongregate.com/badges) and present the badge of the day, and the Lua skin I wrote for it is supposed to take the first badge presented on the site, compare it to the previous badge that it recorded, and then save the current badge name to the file. The Lua script returns a 0 for no new badge, and a 1 for a new badge, and when it equals 1, the skin shows an image that tells me new badges are up.

It worked the first time, but then it just stopped. When looking at About Rainmeter, the only thing I can deduce is that the Lua script is just written poorly, and I barely know anything about Lua. To write it I just learned what I needed instead of learning all the ins and out of it, so I don't know where I went wrong. Any help would be appreciated.

I'm currently running Rainmeter 2.0.0 rev 745 (64-bit).

This is the skin:

Code: Select all

;Metadata added by RainBrowser
;http://rainmeter.net/cms/Rainmeter101-EditingSkins

[Metadata]
Name=Kongregate's "Badge of the Day"
Config=Kong Badge
Description=This skin presents the current badge of the day with information regarding it's difficulty and requirements.
Instructions=
Version=1.0
Tags=Kongregate
License=Creative Commons Attribution-Non-Commercial-Share Alike 3.0
Variant=
Preview=#SKINSPATH#Kong Badge\preview.png

;End of added Metadata

[Rainmeter]
Author=donutmtoazt@mail.fresnostate.edu
Update=1000
DynamicWindowSize=1

[Variables]
TextColor=255,255,255,255
Width=214

[BotDMeasure]
Measure=Plugin
Plugin=Plugins\WebParser.dll
UpdateRate=300
Url=http://www.kongregate.com/badges
RegExp="(?siU)<h3 class="mbs">(.*)</h3>.*height="36" src="(.*)" title=.*<span alt="(.*)" class=.*<strong><a href="(.*)" class=.*&#40;(.*)&#41;.*<p class="prs">(.*)<a href.*">(.*)</a></p>.*<img alt=(.*) height="36""
StringIndex=1
Debug=1
FinishAction=!Execute [!RainmeterRedraw][!RainmeterEnableMeasure LuaScript]

[Image2]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[BotDMeasure]
StringIndex=2
Download=1

[Name3]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[BotDMeasure]
StringIndex=3

[Link4]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[BotDMeasure]
StringIndex=4

[Diff5]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[BotDMeasure]
StringIndex=5

[Text6]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[BotDMeasure]
StringIndex=6

[Text7]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[BotDMeasure]
StringIndex=7

[New8]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[BotDMeasure]
StringIndex=8

[LuaScript]
Measure=Script
ScriptFile="#CURRENTPATH#lua.lua"
TableName=Kong
UpdateDivider=300
Disabled=1

[NewCalc]
Measure=Calc
Formula=[LuaScript]
DynamicVariables=1
IfEqualValue=1
IfEqualAction=!Execute [!RainmeterShowMeter New]

[Background]
Meter=IMAGE
W=214
H=70
SolidColor=255,255,255,1
UpdateDivider=.001
MouseOverAction=!Execute [!RainmeterShowMeter Mouse][!RainmeterShowMeter Description]
MouseLeaveAction=!Execute [!RainmeterHideMeter Mouse][!RainmeterHideMeter Description]
LeftMouseUpAction=!Execute [[Link4]]

[Title]
Meter=STRING
MeasureName=BotDMeasure
X=11
Y=0
FontFace=Tahoma
FontSize=11
StringStyle=BOLD
FontColor=#TextColor#
AntiAlias=1

[BadgeImage]
Meter=IMAGE
MeasureName=Image2
X=17
Y=27
W=36
H=36
LeftMouseUpAction=!Execute [[Link4]]

[BadgeBorder]
Meter=IMAGE
ImageName=badgeborder.png
X=-15r
Y=-4r
W=55
H=44
LeftMouseUpAction=!Execute [[Link4]]

[BadgeName]
Meter=STRING
Text=[Name3] Badge
X=112
Y=22
W=104
H=50
FontFace=Tahoma
FontSize=9
StringStyle=BOLD
StringAlign=CENTER
FontColor=#TextColor#
AntiAlias=1
ClipString=1
DynamicVariables=1
LeftMouseUpAction=!Execute [[Link4]]

[Difficulty]
Meter=IMAGE
ImageName=[Diff5] 2.png
X=170
Y=35
DynamicVariables=1
LeftMouseUpAction=!Execute [[Link4]]

[Mouse]
Meter=IMAGE
ImageName=pop.png
X=10
Y=76
Hidden=1

[Description]
Meter=STRING
Text=[Text6][Text7]
X=14
Y=80
W=186
H=44
FontFace=Tahoma
FontSize=8
FontColor=34,34,34,255
ClipString=1
AntiAlias=1
DynamicVariables=1
Hidden=1

[New]
Meter=IMAGE
ImageName=new.png
X=220
Y=10
H=50
FontFace=Tahoma
FontSize=8
FontColor=34,34,34,255
AntiAlias=1
Hidden=1
LeftMouseUpAction=!Execute [http://www.kongregate.com/badges][!RainmeterHideMeter New]
And this is the Lua script:

Code: Select all

PROPERTIES =
{

}

function Initialize()

	new = SKIN:GetMeasure("New8")
	file = "C:/Users/james cunn/Documents/Rainmeter/Skins/Kong Badge/Saves.txt"
	a = 0
	
end -- function Initialize

function Update()

	curtop = new:GetStringValue()

	f = io.open(file, "r")
	if f:read("*all") == curtop then a = 0 else a = 1 end
	f:close()
	
	f = io.open(file, "w")
	f:write(curtop)
	f:close()

end -- function Update

function GetStringValue()
	return tostring(a)
end -- function GetStringValue

Update:
Switching to the 2.1 Beta seems to have resolved my problem!
This is my keyboard. There are many like it, but this one is mine.
poiru
Developer
Posts: 2872
Joined: April 17th, 2009, 12:18 pm

Re: Help with Lua Code

Post by poiru »

donutmtoazt wrote:Update:
The Rainmeter log says that it cannot "enable the measure LuaScript (there is no such thing in Enigma\Sidebar\Reader\News)".
I'm not sure why it's telling me this since my skin is in a different folder...
This is a bug in WebParser, which has been been fixed in the 2.1 beta releases. If you want to continue using 2.0, use the following workaround under [BotDMeasure]:

Code: Select all

FinishAction=!Execute [!RainmeterRedraw][!RainmeterEnableMeasure LuaScript "#CURRENTCONFIG#"]
User avatar
donutmtoazt
Posts: 50
Joined: July 10th, 2011, 9:15 am
Location: Fresno, CA

Re: Help with Lua Code

Post by donutmtoazt »

Thanks Poiru, but I thought it was about time that I switched to 2.1 and checked it out. It's working well now that I have edited some of the code to match the update.

Thanks a lot though!
This is my keyboard. There are many like it, but this one is mine.
User avatar
Yggdrasil
Posts: 24
Joined: June 25th, 2011, 5:09 pm

Re: Help with Lua Code

Post by Yggdrasil »

Hello,
Just a side note: if you want to make it work for everybody (I mean if you want to publicate it), you should select a better path for the temp file.
First solution:

Code: Select all

    local uname = os.getenv("username")
    file = "C:/Users/"..uname.."/Documents/Rainmeter/Skins/Kong Badge/Saves.txt"
But it works just on Windows Vista or 7, on XP it fails as that folder doesn't exist. The best solution - imho - is this:

Code: Select all

    file = os.getenv("tmp").."/Saves.txt"
It works on every Windows machine, and the temp folder is that what you should use in this case :)
Regards :welcome:
Image
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Help with Lua Code

Post by jsmorley »

One important side note to this discussion is that as Lua is platform independent and really comes out of a unix world, it uses the "/" character for "paths", instead of the Windows convention of "\".

So:

f = io.open("C:/Users/Jeffrey Morley/Documents/Rainmeter/Skins/TestSkin/TestFile.txt", "w")

is correct, but

f = io.open("C:\Users\Jeffrey Morley\Documents\Rainmeter\Skins\TestSkin\TestFile.txt", "w")

is not.

Also, be careful about using "relative" paths when manipulating files in a Rainmeter Lua script. I find it to be unreliable in the extreme, and a specific path should be used.

this:

f = io.open("./testtest.txt", "w")

May open the file in god knows what location. For me it created the file in "C:\Program Files\Rainmeter\Addons\RainThemes\testtest.txt" which has absolutely nothing to do with anything that makes sense. RainThemes was not open, there is no mention of it in the skin or the Lua, and I have no idea why it decided to drop that file there.
User avatar
Yggdrasil
Posts: 24
Joined: June 25th, 2011, 5:09 pm

Re: Help with Lua Code

Post by Yggdrasil »

jsmorley, you are almost right, but the thing you mentioned is not in relation with Unix. Lua accepts \ as separator, but you should duplicate it:
f = io.open("C:\\Users\\Jeffrey Morley\\Documents\\Rainmeter\\Skins\\TestSkin\\TestFile.txt", "w")
The reason is that \ has a special meaning in strings. If it's followed by 2 or 3 digit, it returns with the character of that code (from 0 to 255)
print("\076\117\097")
It will print Lua. But \ is an escape character as well, so if you escape \ with itself, it will reproduce the backslash character, which is accepted in paths :)
Image
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Help with Lua Code

Post by jsmorley »

Yggdrasil wrote:jsmorley, you are almost right, but the thing you mentioned is not in relation with Unix. Lua accepts \ as separator, but you should duplicate it:
f = io.open("C:\\Users\\Jeffrey Morley\\Documents\\Rainmeter\\Skins\\TestSkin\\TestFile.txt", "w")
The reason is that \ has a special meaning in strings. If it's followed by 2 or 3 digit, it returns with the character of that code (from 0 to 255)
print("\076\117\097")
It will print Lua. But \ is an escape character as well, so if you escape \ with itself, it will reproduce the backslash character, which is accepted in paths :)
That is true. I didn't think to escape the "\" chars. Probably safest to use the "/" char in any case, and certainly simpler. Thanks.
User avatar
donutmtoazt
Posts: 50
Joined: July 10th, 2011, 9:15 am
Location: Fresno, CA

Re: Help with Lua Code

Post by donutmtoazt »

Thanks a lot for your replies, guys!
Like I said, I'm just starting, so any criticism on this is helpful while I'm learning Lua.
This is my keyboard. There are many like it, but this one is mine.