It is currently March 29th, 2024, 12:03 pm

Control the execution of an "addon" for Rainmeter

Tips and Tricks from the Rainmeter Community
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Control the execution of an "addon" for Rainmeter

Post by jsmorley »

A lot of folks use "addon" executables to help do things in Rainmeter. One of the challenges is how and when to "!Execute" the external addon on some regular basis. There are LOTS of ways to accomplish this, depending on how the skin works and what the addon is being asked to do. However, there are a couple of simple ways to have an addon execute on a "timed" basis without a lot of complicated code.

The first two below show how to execute an addon "once a minute" and "once an hour" This is independent of the Update= setting of the skin and is based on the system clock. The method used in the first is to get the "seconds" in the clock and when they are at "0", it is a new minute. In the second, we get the "minutes" in the clock, and when they are at "0", it is a new hour.

[MeasureExecuteMinute]
Measure=Time
Format="%S"
IfEqualValue=0
IfEqualAction=!Execute [#ADDONS#AppToRun.exe]

[MeasureExecuteHour]
Measure=Time
Format="%M"
IfEqualValue=0
IfEqualAction=!Execute [#ADDONS#AppToRun.exe]

A more robust solution, allowing for much more control of how often the addon is executed, is to use a CALC measure and the "COUNTER" function of Rainmeter.

A built-in "counter" variable is incremented by Rainmeter on every "Update=" cycle. This counter is automatically reset on a skin "refresh", but has a nice little trick. It can also be reset manually, by using "% x" after the "Counter" keyword in the "Formula=" key in a CALC statement. The "x" is the number of "Update=" cycles (not seconds necessarily, but can be if Update= is set to "1000") you wish to have the counter reach before resetting.

So in the example below, since the "Update=" is set to 1000 milliseconds, or one second, and the "Counter" variable will reset to "0" every time it reaches "2", this addon will run once a second.

[Rainmeter]
Update=1000

[MeasureExecuteUpdate]
Measure=Calc
Formula=Counter % 2
IfAboveAction=!execute [#ADDONS#AppToRun.exe]
IfAboveValue=0

Counter is at "0". Nothing happens as "IfAboveValue=0" is not true. Counter changes to "1", and the addon is executed as "IfAboveValue=0" is now true. When Counter reaches "2" on the next update, it is reset to "0" and the addon is not executed. It might look like this would cause the addon to execute twice in this set of cycles, or that "Formula=Counter % 1" should work as well, but it should be remembered that Rainmeter updates this counter variable BEFORE it begins executing the measures in a skin, (so "% 1" will NEVER allow "IfAboveValue=0" to be true) and that Rainmeter will only "fire" an action based on IfAboveValue/IfBelowValue/IfEqualValue one time when the comparison becomes "true" and it must go back to "false" and then "true" again for the action to again fire.

Obviously you will not want to execute all addons once a second. Simply change the "% 2" to some other number in the example above to control how often the IfAboveAction statement is true and the action fires. For instance, changing the above to "Formula=Counter % 600" would cause the action to fire every ten minutes.
User avatar
Varelse
Posts: 61
Joined: April 22nd, 2009, 7:46 pm

Re: Control the execution of an "addon" for Rainmeter

Post by Varelse »

I've been looking for a piece of code that would set off an action every update or so. :-) This is so much more versatile than for just addons. I'm using it in conjuction with another clever piece of code written by kenz0 to scroll text in my itunes player. I've also integrated it into my RSS reader to cycle between the feeds.

Which is kinda working...

I've only crashed rainmeter a dozen or so times so far trying to get the text to scroll :? . But I think I finally have it working. Here's the basis of the code I'm using:

Code: Select all

[Variables]
MAX=some max
NUMBER2=some increment

[FORWARD]
Measure=Calc
Formula=((#NUMBER1#-3))%#MAX#=0?#MAX#:((#NUMBER1#-3))%#MAX#
DynamicVariables=1

[Calc1]
Measure=Calc
Formula=Counter % 2
IfEqualValue=1
IfEqualAction=!Execute [!RainmeterSetVariable NUMBER1 [FORWARD]][!RainmeterMoveMeter 125 #NUMBER1# words][!RainmeterRedraw]
DynamicVariables=1

[words]
Meter=String
MeasureName=LYRICS
MeasureName2=Title
X=125
Y=(#NUMBER1#)
W=240
H=700
FontColor=#FontColor2#
FontFace=#FontName1#
FontSize=#FontSize2#
StringAlign=Center
StringStyle=Bold
AntiAlias=1
ClipString=1
The downside is that it's cpu intensive. Using an update of 500 it taking about 2-4% with just the one skin open.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Control the execution of an "addon" for Rainmeter

Post by jsmorley »

This trick of using "Forumula=Counter % n" in a Calc statement is certainly useful for more than running addons. It can be use with any "!bang" statement to allow fine control of when things happen in your skin by hiding and unhiding meters, enabling or disabling measures, toggling skins, etc...

Do take note if you are going to get REALLY geeky with "Counter" that when I said "Rainmeter updates the counter before executing the measures" above I was taking some liberties with the underlying programming for clarity and simplicity's sake. What is really going on, and why you need "% 2" instead of "% 1" in the example above is due to the fact that, for the same reason that January 1, 2000 was NOT the beginning of a new millennium, and your "first" birthday isn't the day you are born, the counter is "zero" based. So the first cycle is cycle "0", not "1", and in effect the counter is one cycle "behind" at all times. This is only important if you are going to use the "total cycles" in some really clever way other than just as a timer.