It is currently March 29th, 2024, 11:02 am

Pro techniques with Variables (and MeterStyles)

Tips and Tricks from the Rainmeter Community
User avatar
Active Colors
Moderator
Posts: 1251
Joined: February 16th, 2012, 3:32 am
Location: Berlin, Germany

Pro techniques with Variables (and MeterStyles)

Post by Active Colors »

Depending on various case scenarios this can come in handy.
You can find attached skins with examples at the end of this post.
Post your workarounds in the comments as well.

1. #CURRENTSECTION# as a variable

Code: Select all

[Variables]
Text1=First
Text2=Second

[TextStyle]
Text=[#[#CURRENTSECTION]]
DynamicVariables=1

[Text1]
Meter=String
MeterStyle=TextStyle

[Text2]
Meter=String
MeterStyle=TextStyle
1.PNG

2. #CURRENTSECTION# as a part of variable

Code: Select all

[Variables]
Text1Name=Firefox
Text2Name=Notepad
Text1Link=C:\Program Files\Mozilla Firefox\firefox.exe
Text2Link=C:\Windows\System32\notepad.exe

[TextStyle]
Text=[#[#CURRENTSECTION]Name]
LeftMouseUpAction=["[#[#CURRENTSECTION]Link]"]
DynamicVariables=1

[Text1]
Meter=String
MeterStyle=TextStyle

[Text2]
Meter=String
MeterStyle=TextStyle
2.PNG

3. #CURRENTSECTION# and variable as a variable
(Also, #CURRENTSECTION# and variable as a part of a variable)
Note: In the code:
LeftMouseUpAction= uses #CURRENTSECTION# and variable as a variable
Text= uses #CURRENTSECTION# and variable as a part of a variable

Code: Select all

[Variables]
Option=Default
DefaultApp1=C:\Program Files\Mozilla Firefox\firefox.exe
DefaultApp2=C:\Windows\System32\notepad.exe
DefaultApp1Name=Firefox
DefaultApp2Name=Rainmeter

[StyleApp]
LeftMouseUpAction=["[#[#Option][#CURRENTSECTION]]"]
Text=[#[#Option][#CURRENTSECTION]Name]
DynamicVariables=1

[App1]
Meter=String
MeterStyle=StyleApp

[App2]
Meter=String
MeterStyle=StyleApp
3.PNG

4. Mixed multi-level variables with #CURRENTSECTION#

Code: Select all

[Variables]
Region=Berlin
BerlinPrice1=4.50
BerlinPrice2=2.99
Berlin=Discount
DiscountPrice1=4.25
DiscountPrice2=2.49

[StylePrice]
Text=[#CURRENTSECTION]: [#[#Region][#CURRENTSECTION]] #CRLF#Discount: [#[#[#Region]][#CURRENTSECTION]]
DynamicVariables=1

[Region]
Meter=String
Text=Region: [#[#CURRENTSECTION]] #CRLF#Mode: [#[#Region]] 

[Price1]
Meter=String
MeterStyle=StylePrice

[Price2]
Meter=String
MeterStyle=StylePrice
4.PNG

5. Escaped variable as a variable
If you ever need to used escaped variable as a variable, note that [#[#*Variable*]] will not work since the inner brackets are not being dropped when escaping asterisks are placed.
In order to make it work this form should be used:
[##*Variable*#]

6. Key name of a variable is a variable itself

Code: Select all

[Variables]
Location=Berlin
#Location#Price1=4.50
#Location#Price2=2.99

[StylePrice]
Text=[#CURRENTSECTION]: [##*Location*#[#CURRENTSECTION]]
DynamicVariables=1

[Price1]
Meter=String
MeterStyle=StyleText

[Price2]
Meter=String
MeterStyle=StylePrice
6.PNG
Note 1: Without second nested variable the variable can be written simply [##*Location*#Price1] as well.
Note 2: If you need to declare such a #key#=value as a variable you can use this bang (e.g. in [Rainmeter] OnRefreshAction):

Code: Select all

[!SetVariable "[#Location]Price1" "[##*Location*#Price1]"][!Update *]
6.1.png

7. #CURRENTSECTION# is an index of the current section
Note: you need to declare the desired part of a desired section name in the Variables section.

Code: Select all

[Variables]
; Indexed Sections
Text=
Icon=

; Options
#Text#1=Firefox
#Text#2=Photoshop
#Text#3=iTunes

#Icon#1=#@#Firefox.png
#Icon#2=#@#Photoshop.png
#Icon#3=#@#iTunes.png

[StyleIcon]
X=r
Y=60R
W=32
H=32
ImageName=[##*Icon*#[#CURRENTSECTION]]
DynamicVariables=1

[StyleText]
X=([#*Icon*#[#CURRENTSECTION]:XW]+20)
Y=(([#*Icon*#[#CURRENTSECTION]:Y]+([#*Icon*#[#CURRENTSECTION]:H]/2))-([#*Text*#[#CURRENTSECTION]:H]/2))
Text=Label: [##*Text*#[#CURRENTSECTION]] #CRLF#Current section index: [#CURRENTSECTION] #CRLF#Y value of the current section: [#*Text*#[#CURRENTSECTION]:Y] #CRLF#E.g: Y value of text meter with current section index: [#*Icon*#[#CURRENTSECTION]:Y]
DynamicVariables=1

[#Icon#1]
Meter=Image
MeterStyle=StyleIcon
X=25
Y=50

[#Icon#2]
Meter=Image
MeterStyle=StyleIcon

[#Icon#3]
Meter=Image
MeterStyle=StyleIcon


[#Text#1]
Meter=String
MeterStyle=StyleText

[#Text#2]
Meter=String
MeterStyle=StyleText

[#Text#3]
Meter=String
MeterStyle=StyleText
7.PNG

Other cases from the forum
You do not have the required permissions to view the files attached to this post.
User avatar
death.crafter
Rainmeter Sage
Posts: 1399
Joined: April 24th, 2021, 8:13 pm

Re: Pro techniques with Variables (and MeterStyles)

Post by death.crafter »

Active Colors wrote: June 28th, 2021, 7:19 pm
I have been using the first and second techniques, and they are quite handy, but I never thought of sharing it.

Anyways, I don't understand the fourth and sixth use case. We could have as well as used literal strings. Why go the extra length🤔
from the Realm of Death
User avatar
Active Colors
Moderator
Posts: 1251
Joined: February 16th, 2012, 3:32 am
Location: Berlin, Germany

Re: Pro techniques with Variables (and MeterStyles)

Post by Active Colors »

death.crafter wrote: June 28th, 2021, 8:24 pm I have been using the first and second techniques, and they are quite handy, but I never thought of sharing it.
I wanted to put all useful techniques that go beyond the documentation and I didn't want to complicate it by grouping them according to some subjective difficulty. For an experienced user it can be simple while for a beginner it could be an enlightenment.

death.crafter wrote: June 28th, 2021, 8:24 pm Anyways, I don't understand the fourth and sixth use case. We could have as well as used literal strings. Why go the extra length🤔
In order to get rid of possible repetitions.
I don't remember about the case of number 6 as I made it a while ago while helping somebody.
The number 4 is pretty straightforward? It is the same as number 2 but with an extra level. Speaking of this example, imagine if there are more conditions to the "Region", for instance instead of "Berlin=Discount" it can be "Berlin=Discount2" or something completely different. You wouldn't want to rewrite each condition explicitly throughout the skin but keep it in just one core hub aka Variables where it is supposed to be and let MeterStyles do the job ;-)
User avatar
death.crafter
Rainmeter Sage
Posts: 1399
Joined: April 24th, 2021, 8:13 pm

Re: Pro techniques with Variables (and MeterStyles)

Post by death.crafter »

Active Colors wrote: June 28th, 2021, 9:27 pm In order to get rid of possible repetitions.
I don't remember about the case of number 6 as I made it a while ago while helping somebody.
The number 4 is pretty straightforward? It is the same as number 2 but with an extra level. Speaking of this example, imagine if there are more conditions to a region, for instance instead of "Berlin=Discount" it can be "Berlin=Discount2" or something completely different. You wouldn't want to rewrite each condition explicitly throughout the skin but keep it in just one core hub aka Variables where it is supposed to be ;-)
Ahh I see. That's a good way to put it. Thanks for explaining.
from the Realm of Death
User avatar
nek
Posts: 105
Joined: November 3rd, 2019, 12:00 am

Re: Pro techniques with Variables (and MeterStyles)

Post by nek »

Thanks! for sharing your knowledge.
I have created a Color Swatch skin using technique 1. #CURRENTSECTION# as a variable to change color swatches (Fluent, Material, Grayscale, Tailwind...) with mouse scroll wheel.
smallsize.png
Animated preview
Image

Image
📦 Demo.ColorSwatch.rmskin (150 KB, github.com)
You do not have the required permissions to view the files attached to this post.
User avatar
Active Colors
Moderator
Posts: 1251
Joined: February 16th, 2012, 3:32 am
Location: Berlin, Germany

Re: Pro techniques with Variables (and MeterStyles)

Post by Active Colors »

nek wrote: August 6th, 2022, 9:18 pm Thanks! for sharing your knowledge.
I have created a Color Swatch skin using technique 1. #CURRENTSECTION# as a variable to change color swatches (Fluent, Material, Grayscale, Tailwind...) with mouse scroll wheel.
smallsize.png

Animated preview
Image

Image
📦 Demo.ColorSwatch.rmskin (150 KB, github.com)
Wow. Fantastic work, nek!