It is currently March 29th, 2024, 12:03 pm

Help with my first skin

Discuss the use of Lua in Script measures.
Jonno
Posts: 1
Joined: August 8th, 2011, 12:25 pm

Help with my first skin

Post by Jonno »

Hello

I am attempting to make a rainmeter skin that pulls user profile information from reddit using the default illustro skin, here is what I have currently:

Code: Select all

[Rainmeter]
; This section contains general settings that can be used to change how Rainmeter behaves.
Author=jonathan
AppVersion=1003000
Update=1000

[Metadata]
; Contains basic information of the skin
Name=Reddit
Description=Shows reddit user info
License=Creative Commons BY-NC-SA 3.0
Version=1.0.0

[Variables]
; Variables declared here can be used later on between two # characters (e.g. #MyVariable#).
fontName=Trebuchet MS
textSize=8
colorBar=235,170,0,255
colorText=255,255,255,205

; ----------------------------------
; MEASURES return some kind of value
; ----------------------------------

[measureFeed]
Measure=Plugin
Plugin=Plugins\WebParser.dll
RegExp=(?siU)has_mail": (.*),.*name": "(.*)".*created_utc":(.*),.*link_karma": (.*),.*comment_karma": (.*),
Url="http://www.reddit.com/user/Jonno_FTW/about.json?feed=MYSUPERSECRETHASH&user=Jonno_FTW&mark=false"
UpdateRate=1000
Substitute="":"N/A"

[styleTitle]
StringAlign=CENTER
StringCase=UPPER
StringStyle=BOLD
StringEffect=SHADOW
FontEffectColor=0,0,0,50
FontColor=#colorText#
FontFace=#fontName#
FontSize=10
AntiAlias=1
ClipString=0


[styleLeftText]
StringAlign=LEFT
; Meters using styleLeftText will be left-aligned.
StringCase=NONE
StringStyle=BOLD
StringEffect=SHADOW
FontEffectColor=0,0,0,20
FontColor=#colorText#
FontFace=#fontName#
FontSize=#textSize#
AntiAlias=1
ClipString=1

[styleRightText]
StringAlign=RIGHT
StringCase=NONE
StringStyle=BOLD
StringEffect=SHADOW
FontEffectColor=0,0,0,20
FontColor=#colorText#
FontFace=#fontName#
FontSize=#textSize#
AntiAlias=1
ClipString=1

[styleSeperator]
SolidColor=255,255,255,15

; ----------------------------------
; METERS display images, text, bars, etc.
; ----------------------------------


[meterBackground]
Meter=IMAGE
ImageName=#SKINSPATH#\illustro\SHARED\Background5Line.png
; #SKINSPATH# is a global variable that stands for your skin path. It is
; usually Documents\Rainmeter\Skins.
X=0
Y=0
[MeasureName]
Measure=Plugin
Plugin=Plugins\WebParser.dll
URL=[measureFeed]
StringIndex=2
[MeasureComment]
Measure=Plugin
Plugin=Plugins\WebParser.dll
URL=[measureFeed]
StringIndex=5
[MeasureLink]
Measure=Plugin
Plugin=Plugins\WebParser.dll
URL=[measureFeed]
StringIndex=4
[MeasureAge]
Measure=Plugin
Plugin=Plugins\WebParser.dll
URL=[measureFeed]
StringIndex=3
[MeasureMail]
Measure=Plugin
Plugin=Plugins\WebParser.dll
URL=[measureFeed]
StringIndex=1

[MeasureCreatedImageChoice]
Measure=Calc
Formula=MeasureMail > 0 ? 1 : 0
IfAboveValue=0
IfAboveAction=!SetOption MeterCreateYesNo ImageName #SKINSPATH#\junk\mail.png
IfBelowValue=1
IfBelowAction=!SetOption MeterCreateYesNo ImageName #SKINSPATH#\junk\mailgray.png

[MeterCreateYesNo]
;Show the mail link
Meter=Image
LeftMouseUpAction=!Execute ["http://www.reddit.com/message/inbox/"]
Y=120
X=180
[meterTitle]
Meter=STRING
MeterStyle=styleTitle
X=100
Y=12
W=190
H=18
Text="Reddit"


[meterNameLabel]
Meter=STRING
MeterStyle=styleLeftText
MeasureName=MeasureName
X=10
Y=40
W=75
H=14
Text="Name"

[MeterName]
Meter=String
MeterStyle=styleRightText
MeasureName=MeasureName
Y=40
X=200
W=240

[MeterLink]
Meter=String
MeterStyle=styleRightText
MeasureName=MeasureLink
Y=80
X=200
W=190
[MeterComment]
Meter=String
MeterStyle=styleRightText
MeasureName=MeasureComment
Y=60
X=200
W=190

[MeterAge]
Meter=String
MeterStyle=styleRightText
MeasureName=MeasureAge
X=200
Y=100
W=100
[meterSeperator1]
Meter=IMAGE
MeterStyle=styleSeperator
X=10
Y=52
W=190
H=1

[meterCommentLabel]
Meter=STRING
MeterStyle=styleLeftText
MeasureName=measureFeed
X=10
Y=60
W=150
H=14
Text="Comment Karma"

[meterSeperator2]
Meter=IMAGE
MeterStyle=styleSeperator
X=10
Y=72
W=190
H=1

[meterLinkLabel]
Meter=STRING
MeterStyle=styleLeftText
X=10
Y=80
W=150
H=14
Text="Link Karma"

[meterSeperator3]
Meter=IMAGE
MeterStyle=styleSeperator
X=10
Y=92
W=190
H=1

[meterAgeLabel]
Meter=STRING
MeterStyle=styleLeftText
MeasureName=
X=10
Y=100
W=100
H=14
Text="Age"

[meterSeperator4]
Meter=IMAGE
MeterStyle=styleSeperator
X=10
Y=112
W=190
H=1

[meterMailLabel]
Meter=STRING
MeterStyle=styleLeftText
MeasureName=
X=10
Y=120
W=190
H=14
Text="Mail"


For some reason, the alignment of the values has truncated them and I don't know why (I just installed rainmeter this afternoon, go figure).

Edit: fixed that problem using ClipString=0 in the right style.

Image

I would like to calculate the age from the UTC seconds, to show age in years,months,days. Also, I would like the reddit mail link to go to http://www.reddit.com/message/inbox/ when there is no mail (this happens already) and to http://www.reddit.com/message/unread/ when there is mail.

Those 2 last points are the things I couldn't get and any help would be appreciated.

Thankyou
User avatar
Seahorse
Posts: 1175
Joined: June 9th, 2010, 5:56 pm
Location: Locks heath, UK

Re: Help with my first skin

Post by Seahorse »

I think this should pull the years from the total seconds. Clearly this doesn't account for leap years and alike. This simply divides the figure by seconds/minutes/hours/days to get the year. If this works ok, you should be able to split the rest off using the same method. You can tweak the accuracy (the number of figures AFTER the decimal place to suit).

Code: Select all

[Variables]
Accuracy=1

[AgeCalc]
Measure=Calc
Formula=ROUND(([MeasureAge]/60/60/24/365),#Accuracy#)
I'm betting there is a simpler way using lua (over my head) or an elegant way (cue Smurfier), but this is how I'd have a go at it initially... :thumbup:
"Regrettably your planet is one of those scheduled for demolition"
Mike

My Skins at DeviantArt

User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Help with my first skin

Post by jsmorley »

Yeah, what he wants is the number of seconds difference from MeasureAge to "now" broken up and displayed as

xxx Years, xxx Months, xxx Days

This is going to take Lua for sure. I also think we are going to have to live with some math based on "average seconds in a year and month" in order to turn the "number of seconds", which is 100% accurate, into "years/months/days", since that "number of seconds" is in fact just a number and in no way related to the calendar. Building this to find the difference in seconds between two dates (which is easy) and turning that number into "calendar based" separate time values, accounting for different numbers of days in different months and leap years all that would presumably be a much more complicated thing, unless there is some trick I am missing. As far as I know, there are no more capable built-in "date math functions" in Lua. Just os.date(), os.time() and os.difftime().

Anyway, in the skin:

Code: Select all

[measureFeed]
Measure=Plugin
Plugin=Plugins\WebParser.dll
RegExp=(?siU)has_mail": (.*),.*name": "(.*)".*created_utc": (.*).0,.*link_karma": (.*),.*comment_karma": (.*),
Url="http://www.reddit.com/user/jsmorley/about.json?feed=MYHASH=jsmorley&mark=false"
UpdateRate=1000
FinishAction=!EnableMeasure MeasureLuaAge

...

[MeasureAge]
Measure=Plugin
Plugin=Plugins\WebParser.dll
URL=[measureFeed]
StringIndex=3

...

[MeterAge]
Meter=String
MeterStyle=styleRightText
ClipString=0
X=200
Y=100
W=100
Text=Years: #Years# Months: #Months# Days: #Days#
DynamicVariables=1

...

[MeasureLuaAge]
Measure=Script
ScriptFile=#CURRENTPATH#MeasureLuaAge.lua
Disabled=1
Be cautious Jonno, I tweaked your RegExp= just a tad, and you will need to put in your URL string with your name and hash code

And MeasureLuaAge.lua in the same folder with the skin:

Code: Select all

PROPERTIES =
{

}

function Initialize()

-- Set pointers to the two measures we want to manipulate in the script.
-- Only need to do this once, so we do it in Initialize().
msAge = SKIN:GetMeasure("MeasureAge")
msScript = SKIN:GetMeasure("MeasureLuaAge")

end -- function Initialize

function Update()

	-- Temporary function to test different "starting" dates
	-- s="Sun, 07 Aug 2011 22:46:00 EST"
	-- p="%a+, (%d+) (%a+) (%d+) (%d+):(%d+):(%d+) (%a+)"
	-- day,month,year,hour,min,sec,tz=s:match(p)
	-- MON={Jan=1,Feb=2,Mar=3,Apr=4,May=5,Jun=6,Jul=7,Aug=8,Sep=9,Oct=10,Nov=11,Dec=12}
	-- month=MON[month]
	-- sAge = os.time({tz=tz,day=day,month=month,year=year,hour=hour,min=min,sec=sec})

	-- Get the current value of the measure [MeasureAge].
	sAge = msAge:GetStringValue()
	-- Get the current date / time as a timestamp.
	sNow = os.time()
	-- Get the difference in seconds.
	iAgeDiff = os.difftime(sNow, sAge)

	-- Do some date math.
	iAgeYears = math.floor(iAgeDiff / 31536000)
	iYearsRemainder = math.floor(iAgeDiff % 31536000)
	iAgeMonths = math.floor(iYearsRemainder / 2440800)
	iMonthsRemainder = math.floor(iAgeDiff % 2440800)
	iAgeDays = math.floor(iMonthsRemainder / 86400)
	iDaysRemainder = math.floor(iAgeDiff % 86400)
	iAgeHours = math.floor(iDaysRemainder / 3600)
	iHoursRemainder = math.floor(iAgeDiff % 3600)
	iAgeMinutes = math.floor(iHoursRemainder / 60)
	iAgeSeconds = math.floor(iAgeDiff % 60)

	-- Uncomment to debug...
	-- print(os.date("%c",sAge))
	-- print(os.date("%c",sNow))
	-- print(iAgeYears)
	-- print(iAgeMonths)
	-- print(iAgeDays)
	-- print(iAgeHours)
	-- print(iAgeMinutes)
	-- print(iAgeSeconds)
	
	-- Set some variables in the skin with the values we have created.
	SKIN:Bang("!SetVariable Years "..iAgeYears)
	SKIN:Bang("!SetVariable Months "..iAgeMonths)
	SKIN:Bang("!SetVariable Days "..iAgeDays)
	SKIN:Bang("!SetVariable Hours "..iAgeHours)
	SKIN:Bang("!SetVariable Minutes "..iAgeMinutes)
	SKIN:Bang("!SetVariable Seconds "..iAgeSeconds)

	-- Disable the script on the way out.
	msScript:Disable()

	return tostring(iAgeDiff)

end -- function Update
Notes:

I am setting variables for all the values, years, months, days, hours, minutes, seconds, so this can be somewhat "generic", but I think he only wants to display years, months and days.

I am setting the Lua measure as "disabled" in the skin and having the WebParser measure "enable" it. On the first execution, when the skin is loaded or refreshed, the Webparser measure will not yet have "returned" any data, and the values from the Lua will all be "0" until the next UpdateRate cycle is reached. With my approach it will wait until WebParser is done getting the site and parsing it before calling the Lua. Since I disable the lua measure on the way out of the .lua script, in effect the script is only run when the WebParser measure asks it to run, thus saving some cycles.