It is currently March 28th, 2024, 11:02 pm

String Split Problem

Discuss the use of Lua in Script measures.
Proxy
Posts: 3
Joined: July 6th, 2013, 5:14 pm

String Split Problem

Post by Proxy »

Hey, basically I'm attempting to split the returned string of a plugin.

Here is an example of what im trying to do:

.ini:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
SolidColor=150,0,0,255

[ScriptMeasure]
Measure=Script
ScriptFile="#CURRENTPATH#splitstr.lua"
UpdateDivider=1

[TextStyle]
X=0
Y=2R
FontFace=Arial
FontSize=11
FontColor=255,255,255,255
SolidColor=0,0,0,1
AntiAlias=1

[Text1]
Meter=String
MeterStyle=TextStyle

[Text2]
Meter=String
MeterStyle=TextStyle

[MeasureOSVersion]
Measure=Plugin
Plugin=SysInfo
SysInfoType=OS_VERSION
UpdateDivider=-1
.lua:

Code: Select all

function Update()
	osv="[MeasureOSVersion]"
	strSplit=split(osv)
        --SKIN:Bang('!SetOption', 'Text2', 'Text', osv)
	SKIN:Bang('!SetOption', 'Text1', 'Text', strSplit[1])
	SKIN:Bang('!SetOption', 'Text2', 'Text', strSplit[2])
end

function split(str)
    local tbl = {}
    for w in str:gmatch("%w+") do
       table.insert(tbl,w)
    end
    return tbl
end
So if my OS is "Windows 7" Text1 should show "Windows" and Text2 should show "7".

PS:started doing stuff with rainmeter tday, so pls be kind :p
Last edited by Proxy on July 6th, 2013, 8:08 pm, edited 2 times in total.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: String Split Problem

Post by jsmorley »

Code: Select all

function Initialize()

   measureOSVersion=SKIN:GetMeasure('MeasureOSVersion')
   
end

function Update()

   osv=measureOSVersion:GetStringValue()
   
   strSplit=split(osv)
   SKIN:Bang('!SetOption', 'Text1', 'Text', strSplit[1])
   SKIN:Bang('!SetOption', 'Text2', 'Text', strSplit[2])
   
end

function split(str)

    local tbl = {}
    for w in str:gmatch("%w+") do
       table.insert(tbl,w)
    end
    return tbl
	
end
The way Lua communicates directly with Measures and Meters in Rainmeter is by creating what we call "objects" that are in effect pointers to the desired measure or meter. Then you can take actions using any of the other measure and / or meter functions, by using them on this "object".

In your case, you need to create an object for your measure, which I did in Initialize() rather than in Update(), since you only need / want to create the object once.

So the Lua variable measureOSVersion is the handle for the object pointing to the skin measure MeasureOSVersion.

Then you use the "GetStringValue()" function in your Update() function to get the current string value of the measure. The rest of your code is and was fine.

http://docs.rainmeter.net/manual/lua-scripting#GetMeasure

http://docs.rainmeter.net/manual/lua-scripting#GetStringValue
Proxy
Posts: 3
Joined: July 6th, 2013, 5:14 pm

Re: String Split Problem

Post by Proxy »

ty very much, great answer.
Odd thing was that "[MeasureOSVersion]" was fetching the plugins return too, but splitting was impossible.
This is way more convenient.

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

Re: String Split Problem

Post by jsmorley »

Proxy wrote:ty very much, great answer.
Odd thing was that "[MeasureOSVersion]" was fetching the plugins return too, but splitting was impossible.
This is way more convenient.

solved :D
Good deal. I do think you might have been seeing something incorrectly though,

osv="[MeasureOSVersion]"

Will just return the literal string "[MeasureOSVersion]", not any value from the plugin.
Proxy
Posts: 3
Joined: July 6th, 2013, 5:14 pm

Re: String Split Problem

Post by Proxy »

jsmorley wrote: osv="[MeasureOSVersion]"

Will just return the literal string "[MeasureOSVersion]", not any value from the plugin.
*facepalm* just noticed that i was using the wrong (and partly working) lua file. Thats why i thought "[MeasureOSVersion]" was giving me real values. :rofl:
jee, still gotta get used to code without IDE.

One quick other question: How do i pass along Text variables of String Meters to a lua function?
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: String Split Problem

Post by jsmorley »

Proxy wrote:
One quick other question: How do i pass along Text variables of String Meters to a lua function?
Not clear exactly what you mean.

If you mean you want Lua to read a variable, that is just

Skin:
[Variables]
SomeVar="Cowabunga!"

Lua:
var1=SKIN:GetVariable('SomeVar')

If you want the current string display of a meter, you can't really do that. A meter has no "value" to ask for. You would have to get the same values (measures, variables, whatever) you are using in the string meter, and use them in Lua.

You have to think of a meter as purely a "display" object. It has no "value" as such. You tell Rainmeter the type of meter you want (String, Image, Bar, etc.) and then pass the meter values with MeasureName=, MeasureName2= etc. On String meters only, you can supplement or replace the MeasureName= value with additional string values using the Text= option along with %1/%2 etc., [MeasureName] and DynamicVariables=1 or #VarName#. Rainmeter uses all of this along with the type of meter, and builds the output display "on the fly" as the meter is updated.


You can use the GetOption meter function to get the Text= option on the meter, but it won't resolve any measure values set on it with MeasureName= and %1/%2 etc., or any #Variables# on the Text= option.