It is currently March 28th, 2024, 1:39 pm

Calculating the letters in an output string

Get help with creating, editing & fixing problems with skins
Post Reply
User avatar
Harkeerat16
Posts: 15
Joined: June 10th, 2017, 10:54 am
Location: India

Calculating the letters in an output string

Post 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
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Calculating the letters in an output string

Post 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.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Calculating the letters in an output string

Post 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
User avatar
Harkeerat16
Posts: 15
Joined: June 10th, 2017, 10:54 am
Location: India

Re: Calculating the letters in an output string

Post 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
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Calculating the letters in an output string

Post by jsmorley »

Great. Glad to help.
Post Reply