Page 1 of 1

Calculating the letters in an output string

Posted: September 22nd, 2017, 1:20 pm
by Harkeerat16
Hi! Is there a way to calculate the letters in a random output string? I'm working on a quote skin that provides you with a new quote each day, and I need to know the number of letters in the string so that I can change the font size of the quote accordingly. Or does anyone know any other way for me to do it?
Thanks!! :D

Re: Calculating the letters in an output string

Posted: September 22nd, 2017, 1:30 pm
by jsmorley
You will need a very simple Lua script for this.

Skin:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Variables]

[MeasureQuote]
Measure=Plugin
Plugin=QuotePlugin
PathName=C:\Users\Jeffrey\Documents\Pithy\pithy.txt
UpdateDivider=5
OnUpdateAction=[!EnableMeasure MeasureScript][!UpdateMeasure MeasureScript]

[MeasureScript]
Measure=Script
ScriptFile=Test.lua
Disabled=1
UpdateDivider=1

[MeterQuote]
Meter=String
MeasureName=MeasureQuote
MeasureName2=MeasureScript
FontSize=13
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=%1#CRLF#%2 characters
Lua:

Code: Select all

function Initialize()

	myMeasure = SKIN:GetMeasure('MeasureQuote')
	
end

function Update()

	myQuote = myMeasure:GetStringValue()
	return string.len(myQuote)
		
end
1.png
NOTE: This will NOT work right with multi-byte Unicode characters in your quotes. This might include some pictographic languages, and some symbols. The number that string.len() returns is the number of "bytes" in the string, and that can be off when some Unicode characters are used.

Re: Calculating the letters in an output string

Posted: September 22nd, 2017, 1:51 pm
by jsmorley
This is a bit easier if you use the latest 4.1 beta of Rainmeter from https://www.rainmeter.net, since you can use Inline Lua to simplify things.

Skin:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Variables]

[MeasureQuote]
Measure=Plugin
Plugin=QuotePlugin
PathName=C:\Users\Jeffrey\Documents\Pithy\pithy.txt
UpdateDivider=5

[MeasureScript]
Measure=Script
ScriptFile=Test.lua

[MeterQuote]
Meter=String
MeasureName=MeasureQuote
FontSize=13
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
DynamicVariables=1
Text=%1#CRLF#That is [&MeasureScript:quoteChars] characters
Lua:

Code: Select all

function Initialize()

	myMeasure = SKIN:GetMeasure('MeasureQuote')
	
end

function Update()

	myQuote = myMeasure:GetStringValue()
	quoteChars = string.len(myQuote)
	
end
1.png

Re: Calculating the letters in an output string

Posted: September 23rd, 2017, 10:09 am
by Harkeerat16
Thanks, it works perfectly now! I didn't really know what Lua scripting was earlier, so thank you for introducing me to it too. I'm sure it would come in handy. :p

Re: Calculating the letters in an output string

Posted: September 23rd, 2017, 11:18 am
by jsmorley
Great. Glad to help.