Couldn't help it, so I tested this on an actual usage scenario, like parsing a hypothetical JSON using WebParser measures...
Source.json, placed in the
@Resources subfolder of the current skin (I parsed a local file, as it was easier, taking this sample JSON from Wikipedia):
Code: Select all
{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 27,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
}
],
"children": [],
"spouse": null
}
Script.lua, placed in the
Scripts subfolder from the
@Resources folder of the current skin:
Code: Select all
function SectionIndex(section, occurrence)
local indexes = {}
for index in section:gmatch('%d+') do table.insert(indexes, index) end
if occurrence == 'first' then occurrence = 1 elseif occurrence == 'last' then occurrence = #indexes end
return indexes[tonumber(occurrence)] or '0'
end
SectionIndex.ini, i.e. the skin:
Code: Select all
[Variables]
; Inline Lua Usage: [&Script:SectionIndex(SomeSection,SomeOccurrence)]
; SomeSection = the name of any section, enclosed by ' (apostrophes), like: 'Measure1' or 'Meter1'
; SomeOccurrence = the desired occurrence of an index, like: 3 or 5 or 'first' or 'last'
; Section variables need to use their nested form, like: [&Script:SectionIndex('[#Section]',[#Occurrence])]
; OR
; Simulation of a CURRENTSECTIONINDEX "built-in" variable, "last" index scenario
CURRENTSECTIONINDEX=[&Script:SectionIndex('[#*CURRENTSECTION*]','last')]
; Alternative using inline Lua: [&Script:SectionIndex('[#CURRENTSECTION]','last')]
; Variables specific to this test skin:
ElementNumber=1
[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1
BackgroundMode=2
SolidColor=47,47,47,255
---Measures---
[Script]
Measure=Script
ScriptFile=#@#Scripts\Script.lua
UpdateDivider=-1
[Source]
Measure=WebParser
Url=file://#@#Source.json
RegExp=(?siU)^.*\{.*"firstName":(.*),.*"lastName":(.*),.*"isAlive":(.*),.*"age":(.*),.*"address":.*(\{.*\}),.*"phoneNumbers":.*(\[.*\]),.*"children":.*(\[.*\]),.*"spouse":(.*)\}$
UpdateRate=3600
FinishAction=[!UpdateMeasureGroup "SourceGroup"][!UpdateMeter *][!Redraw]
OnConnectErrorAction=[!UpdateMeasureGroup "SourceGroup"][!UpdateMeter *][!Redraw]
OnRegExpErrorAction=[!UpdateMeasureGroup "SourceGroup"][!UpdateMeter *][!Redraw]
[Element1]
Measure=WebParser
Url=[Source]
StringIndex=#CURRENTSECTIONINDEX#
UpdateDivider=-1
DynamicVariables=1
[Element2]
Measure=WebParser
Url=[Source]
StringIndex=#CURRENTSECTIONINDEX#
UpdateDivider=-1
DynamicVariables=1
[Element3]
Measure=WebParser
Url=[Source]
StringIndex=#CURRENTSECTIONINDEX#
UpdateDivider=-1
DynamicVariables=1
[Element4]
Measure=WebParser
Url=[Source]
StringIndex=#CURRENTSECTIONINDEX#
UpdateDivider=-1
DynamicVariables=1
[Element5]
Measure=WebParser
Url=[Source]
StringIndex=#CURRENTSECTIONINDEX#
UpdateDivider=-1
DynamicVariables=1
[Element6]
Measure=WebParser
Url=[Source]
StringIndex=#CURRENTSECTIONINDEX#
UpdateDivider=-1
DynamicVariables=1
[Element7]
Measure=WebParser
Url=[Source]
StringIndex=#CURRENTSECTIONINDEX#
UpdateDivider=-1
DynamicVariables=1
[Element8]
Measure=WebParser
Url=[Source]
StringIndex=#CURRENTSECTIONINDEX#
UpdateDivider=-1
DynamicVariables=1
---Meters---
[MeterElement]
Meter=String
FontFace=Consolas
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
FontSize=16
AntiAlias=1
MeasureName=Element#ElementNumber#
Text="Element No. #ElementNumber# =#CRLF##CRLF#%1"
MouseScrollUpAction=[!SetVariable ElementNumber (Clamp((#ElementNumber#-1),1,8))][!UpdateMeter MeterElement][!Redraw]
MouseScrollDownAction=[!SetVariable ElementNumber (Clamp((#ElementNumber#+1),1,8))][!UpdateMeter MeterElement][!Redraw]
DynamicVariables=1
Packed version:
SectionIndex_1.0.0.rmskin
The result is according to the expectations: each
[ElementN] measure is grabbing the
N-th element in the JSON (didn't bother with further splitting the elements, as it plays no role in what I wanted to see). Not sure if I should stringify the function result for consistency with the '0' fail case, but one could do this by changing the last line of the function to
return tostring(indexes[tonumber(occurrence)]) or '0'. Probably not needed, as generally Rainmeter doesn't need this kind of strict format, plus there might be some places like IfConditions or
Conditional Operations that would need numbers to work, but just in case.
You do not have the required permissions to view the files attached to this post.