It is currently March 28th, 2024, 2:28 pm

A primer on "quotes" in Rainmeter

Our most popular Tips and Tricks from the Rainmeter Team and others
Post Reply
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

A primer on "quotes" in Rainmeter

Post by jsmorley »

One of the things that can be a bit confusing to a new Rainmeter user is when to "quote" strings that you use. Let's touch on that and see if we can simplify it.

Rainmeter never needs "quotes" around option values

The first thing to be aware of is that Rainmeter doesn't pay the slightest bit of attention to starting and ending "quotes" around option values. By that I mean things like:

FontColor="255,255,255,255"
ImageName="#@#Images\My Image.png"
Text="Hello World"
Drive="C:"
Interface="Qualcomm Atheros AR938x Wireless Network Adapter"
RegExp="(?siU)<title>(.*)</title>"

Doesn't matter a bit if there is "white space" (spaces or tabs) in the value, those beginning and ending "quotes" are just not needed. In fact, the very first thing Rainmeter will do is strip them off and throw them away. They are just ignored. The above can and should be defined as:

FontColor=255,255,255,255
ImageName=#@#Images\My Image.png
Text=Hello World
Drive=C:
Interface=Qualcomm Atheros AR938x Wireless Network Adapter
RegExp=(?siU)<title>(.*)</title>

Why is this? It's because an option value is always a single thing. It is always a single string value to Rainmeter, and the "quotes" are not needed because they are assumed by the program. In other words, Rainmeter knows the values are strings, and you don't have to tell it so like you would in a programming language where you might say myVar="Hello World". In that case, you have to tell the language that the variable myVar is a string. In Rainmeter, you never do. They are all strings, even FontSize=12, or W=100. Rainmeter knows when an option requires a number, and will itself turn the string "12" into the number 12 as needed. You don't have to worry about it.

So not to get too far into the weeds, just keep this simple rule in mind. Rainmeter never, ever needs starting and ending quotes around option values.

What about [Variable] values?

There is no difference. The things defined in [Variables] are just Option=Value like anything on a measure or meter. Never use starting and ending quotes on variable definitions. They are just stripped off and thrown away.

Won't white space in values like paths to a file cause problems?

NO. Rainmeter is perfectly fine with ImageName=#@#Images\My Image.png. By the definition of the option, it knows that a path to a file is expected, and it will deal with it properly when it asks Windows for the file behind the scenes. Starting and ending "quotes" are just not needed. Again, they are just thrown away.

So when do I actually need "quotes" around a string in Rainmeter?

The only time you will in fact need "quotes" around a string in Rainmeter is when the string value is a parameter to a bang, and the string value contains white space (spaces or tabs).

Bad:
LeftMouseUpAction=[!SetOption MyMeter ImageName #@#Images\My Image.png]

Good:
LeftMouseUpAction=[!SetOption MyMeter ImageName "#@#Images\My Image.png"]

So what is the difference? It's the fact that there are multiple "parameters" supported in bangs, and white space will confuse Rainmeter about where a parameter starts and ends. In that example above, the !SetOption bang, as most do, supports a final optional parameter of "config name", which tells it which skin / config you want the bang to operate on. With the white space in the path above, Rainmeter can't know if Image.png is still part of the path, or the name of the config to act upon. The "quotes" around the value resolves this ambiguity.

When the !SetOption bang actually sets the value for the option ImageName on the meter, it will be set without the quotes. Again, they are just not needed on option values. The quotes are there to help Rainmeter "parse" the bang parameters. Nothing else.

So the rule here is that you should always enclose string parameters in bangs in "quotes". Even if there is no white space in the value, it's a good habit to get in. This habit will be particularly useful, and will save a lot of head-scratching, when you dynamically use [SectionVariables] or #Variables# in the parameters in a bang. Are you SURE there aren't, or won't ever be, spaces in that value?

That's it really. Just keep these two rules in mind:

1) Rainmeter never needs starting and ending "quotes" around option values

2) Always enclose string parameters in bangs in "quotes"
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: A primer on "quotes" in Rainmeter

Post by jsmorley »

Extra credit:

What if I WANT "quotes" on my string value?

As noted above, Rainmeter will strip off and throw away any starting and ending "quotes" around an option value. So what if I wanted an option like:

Text="This is a quoted string"

To actually display "This is a quoted string" and not just This is a quoted string?

In that case, you would just use two ""quotes"" around the value.

Text=""This is a quoted string""

The first and last "quotes" will be stripped off and thrown away, and the inner "quotes" that are left will be treated as part of the sting and displayed.

Note that this is not an issue if the "quotes" you want to display are not at the start and the end..

Text=This is "a quoted string"

Is perfect fine as it is, as only when there are starting and ending "quotes" are they stripped off and thrown away.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: A primer on "quotes" in Rainmeter

Post by jsmorley »

Advanced placement:

What if my string parameter to a bang has quotes embedded in the value itself?

This can come up when you are using !SetOption or !SetVariable to set something that has embedded "quotes" in the value.

You might see this when you are using the !SetOption bang on some action to set an action on a measure or meter. Using an action to set an action...

If we look at an example:

IfTrueAction=[!SetOption myMeter LeftMouseUpAction "[!SetOption myMeter ImageName "#@#Images\MyImage.png"]"]

Here we have a "parsing" problem again. The way "quotes" are handled in Rainmeter is that it sees a "first quote, and then waits for the next quote" to define the end of the string value. In this case, the quotes around "#@#Images\MyImage.png" are going to cause problems. We need "quotes" around the parameter of the initial !SetOption, since it has white space in it, but we also need "quotes" around the !SetOption value we are setting, as it also has white space in it.

What will come to the rescue here are Magic Quotes. Using a set of three """quotes""" will tell Rainmeter that what is being defined is a string that itself has "quotes" in it, and it will then look for the """first magic quote and last magic quote""" to define that string value.

So we would solve the example above as:

IfTrueAction=[!SetOption myMeter LeftMouseUpAction """[!SetOption myMeter ImageName "#@#Images\MyImage.png"]"""]

An example that might be easier to follow is:

Code: Select all

[Variables]
MyVar=One "Two" Three Four

[SomeMeter]
...
LeftMouseUpAction=[!SetVariable MyVar """Five "Six" Seven Eight"""]
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: A primer on "quotes" in Rainmeter

Post by jsmorley »

Post-grad studies:

The Substitute option is the exception

So now that you know all the rules and tricks of when to use "quotes" in your values, let's end up with the inevitable exception. This is the Substitute option on measures, which handles "quotes" in a different way.

Unlike other option values "quotes" are an integral part of the actual functionality of Substitute. The way it works is:

Substitute="value to find":"replacement value"

And the "quotes" are used to define the beginning and end of the components of the substitute functionality. Even though you end up with starting and ending "quotes" on the option, in this case they are not stripped off and thrown away.

Ok, that's simple enough. Why is this "Post-grad studies"?

There is one other exception to how things work with Substitute. What if there are "quotes" embedded in the "value to find" and or "replacement value" string themselves?

In this case, 'single quotes' can be used. It is the ONLY place in Rainmeter, outside of Lua scripting, where 'single quotes' are treated as a quote character. Anywhere else, it will just be seen as a literal text character.

So if we had a string value being returned by a measure like:

This is a "quoted string" value

And you wanted to replace all occurrences of a "quote" with something else, say to remove them, you can do:

Substitute='"':""

If you look carefully, what we are doing is enclosing the quote " character in single quote ' characters in the first, or "value to find" component of the Substitute. That allows the function to search for "quotes", without having that just totally confuse how Rainmeter "parses" the option. Then the "replacement value" string is just "" or an empty string, removing all "quotes" from the value.

Maybe easier to read is the same example, but let's replace the literal string "quoted string" with the literal 'single quoted string'.

Substitute='"quoted string"':"'single quoted string'"

The single quote ' character can be used EITHER to delineate the "value to find" OR "replacement value" component of the Substitute, but not both. The reverse of the example above would also work:

If we had a string value being returned by a measure like:

This is a 'quoted string' value

And you wanted to replace all occurrences of a 'single quote' with something else, say a "double quote", you can do:

Substitute="'":'"'

In this case we use the 'single quotes' to delineate the "replacement value" string.

Again, keep in mind that this is the ONLY place in Rainmeter code where a 'single quote' is treated as a quote character. Don't use it anywhere else in a skin .ini file unless you mean it to be a literal part of the string value. Single quotes are extensively used in Lua code, but that is an entirely different discussion.
User avatar
Virginityrocks
Posts: 478
Joined: February 26th, 2011, 10:22 pm

Re: A primer on "quotes" in Rainmeter

Post by Virginityrocks »

Thank you js. What if someone wanted to put """magic quotes""" inside magic quotes?

For example

Code: Select all

[WriteKeyValue Meter LeftMouseUpAction """[!WriteKeyValue Variables VAR1 """#*SOMEVAR1*#+#*SOMEVAR2*#""" "#@#Settings.inc"]"""]
Last edited by Virginityrocks on March 18th, 2019, 5:04 pm, edited 1 time in total.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: A primer on "quotes" in Rainmeter

Post by jsmorley »

Virginityrocks wrote: March 18th, 2019, 4:47 pm Thank you js. What if someone wanted to put """magic quotes""" inside magic quotes?

For example

Code: Select all

[WriteKeyValue Meter LeftMouseUpAction """[!WriteKeyValue Variables VAR1 """#*SOMEVAR1*#+#*SOMEVAR2*#""" "#@#Settings.inc"]"""]
Without the inner """#*SOMEVAR1*#+#*SOMEVAR2*#""" when Meter's LeftMouseUpAction is used, #@#Settings.inc won't write the variables themselves, just their values.
I don't see a way to do that.
User avatar
Virginityrocks
Posts: 478
Joined: February 26th, 2011, 10:22 pm

Re: A primer on "quotes" in Rainmeter

Post by Virginityrocks »

When there's a will there's a way!

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

Re: A primer on "quotes" in Rainmeter

Post by jsmorley »

Code: Select all

LeftMouseUpAction=[!WriteKeyValue SomeMeter LeftMouseUpAction """[!WriteKeyValue Variables VAR1 "#*SOMEVAR1*#+#*SOMEVAR2*#"]""" """#@#Settings.inc"""]
User avatar
Virginityrocks
Posts: 478
Joined: February 26th, 2011, 10:22 pm

Re: A primer on "quotes" in Rainmeter

Post by Virginityrocks »

Unfortunately using "quotes" instead of """quotes""" on the inside will move the variables over, yes, but I'm not done with them at that point. They need to be moved again from that new location to another location that they've been written to, so writing the """ """ is necessary. Otherwise the end result will still be values rather than variables. But I'm working my scotch tape and glue magic to make something work.
User avatar
StArL0rd84
Posts: 424
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Re: A primer on "quotes" in Rainmeter

Post by StArL0rd84 »

Thank you for this thorough explanation.
I always kinda winged it with the number of quotes required.
The """magic quote""" part really came in handy for the DeskTools 1.1 Update.
https://forum.rainmeter.net/viewtopic.php?f=27&t=31759&p=160438#p160438
Post Reply