It is currently March 29th, 2024, 2:59 pm

How do I use RunCommand with Python?

Get help with creating, editing & fixing problems with skins
Rich_Kat
Posts: 3
Joined: July 27th, 2020, 4:38 pm

How do I use RunCommand with Python?

Post by Rich_Kat »

This seems really basic, but I can't seem to understand it. I'm just trying to get an output from a python script to display as text.

Here's my rainmeter script atm, which is probably wrong:

Code: Select all

[Rainmeter]
Update = 16
DynamicWindowSize = 1


[String_meter]
Meter = String

MeasureName = SomeMeasure

Text = The value is %1 !

DynamicVariables = 1


[SomeMeasure]
Measure=Plugin
Plugin=RunCommand
Parameter= python rainmeter_get.py
State=Hide
OutputType=ANSI
OutputFile=#CURRENTPATH#example.txt
And here's my python script, which I'm even more sure is wrong:

Code: Select all

import random

value = random.randrange(1, 10)

print(value)
  
file = open("example.txt", "w")
file.write(str(value))
file.close()
At the moment it writes to an example txt document, which is the OutputFile parameter of the RunCommand measure, which I'm now pretty sure is not meant for that, but I really don't know what to do. Any help is much appreciated.
User avatar
Yincognito
Rainmeter Sage
Posts: 7031
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: How do I use RunCommand with Python?

Post by Yincognito »

Rich_Kat wrote: July 27th, 2020, 4:47 pm This seems really basic, but I can't seem to understand it. I'm just trying to get an output from a python script to display as text.

Here's my rainmeter script atm, which is probably wrong:

Code: Select all

[Rainmeter]
Update = 16
DynamicWindowSize = 1


[String_meter]
Meter = String

MeasureName = SomeMeasure

Text = The value is %1 !

DynamicVariables = 1


[SomeMeasure]
Measure=Plugin
Plugin=RunCommand
Parameter= python rainmeter_get.py
State=Hide
OutputType=ANSI
OutputFile=#CURRENTPATH#example.txt
And here's my python script, which I'm even more sure is wrong:

Code: Select all

import random

value = random.randrange(1, 10)

print(value)
  
file = open("example.txt", "w")
file.write(str(value))
file.close()
At the moment it writes to an example txt document, which is the OutputFile parameter of the RunCommand measure, which I'm now pretty sure is not meant for that, but I really don't know what to do. Any help is much appreciated.
I'm pretty sure this is possible. Your approach is correct (apart from outputing to a file and not to the measure itself, the latter being required if you want to display the output in Rainmeter), but you need to get the Python script right first. I'm not sure if there are folks here who know Python (I don't, but I can partially compensate through intuition), so as long as you get the non-Rainmeter part the way you want, there shouldn't be a problem getting the Rainmeter part done.

EDIT: This link might also help, as apparently there are Rainmeter plugins that integrate Python scripts into Rainmeter measures...
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
Rich_Kat
Posts: 3
Joined: July 27th, 2020, 4:38 pm

Re: How do I use RunCommand with Python?

Post by Rich_Kat »

Your approach is correct (apart from outputing to a file and not to the measure itself, the latter being required if you want to display the output in Rainmeter)
But how do I do that? :( That's the thing I can't seem to get, and I have no idea how to write my python code for it to work. I've also tried this, but it doesn't seem to work:

Code: Select all

import random

class Measure:
    def Reload(self, rm, maxValue):
        rm.RmLog(rm.LOG_NOTICE, "Reload called")
    
    def Update(self):
      return 1.0
  
    def GetString(self):
      return str(random.randrange(1, 10))
  
    def ExecuteBang(self, args):
      pass
  
    def Finalize(self):
      pass
The rainmeter-python plugin stopped being updated 3 years ago, and I would prefer to get this working with the built-in function rather than one which might stop working at any time. The plugin might already not work, it didn't work first try for me, I might come back to it if I can't get RunCommand to work.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: How do I use RunCommand with Python?

Post by jsmorley »

I know absolutely nothing about Python, but the way the RunCommand plugin works is that it will return whatever is displayed by a program using STDOUT (console). So whatever program, Python or otherwise that you want to access should have the ability to output your desired result in STDOUT. An example of this are cmd.exe (DOS) commands, that output to a console window using STDOUT.

Any program you write should NOT create a GUI (graphical user interface) window, but should be designed and / or compiled to use a CUI (character user interface) output. Basically the program should run in a cmd.exe console window, which RunCommand can hide.

You will need to be careful that you match up the OutputType option on the measure to the encoding that your program uses for output.

I would stay away from any purported Python plugins for Rainmeter at this time, I don't know of any that are currently supported and that I would really trust to work reliably.

I wouldn't be opposed in any way to an effort to integrate Python libraries with Rainmeter in a manner much as Lua is currently integrated. I think Python is a pretty good, very popular, and reasonably accessible language, and it has the additional charm of being a bit more robust and modern than Lua, for instance in the support for Unicode. This would not be a trivial effort, and I'm not suggesting it is in any way in the works, but I'd certainly be in favor of it.
User avatar
Yincognito
Rainmeter Sage
Posts: 7031
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: How do I use RunCommand with Python?

Post by Yincognito »

Rich_Kat wrote: July 28th, 2020, 12:43 pmBut how do I do that? :( That's the thing I can't seem to get, and I have no idea how to write my python code for it to work.
Again, I have no idea what you'd like your Python code to do, and how to write it - that's your job. Having said that, here is a basic code for a "Hello World!" output from the measure, which will then be displayed in the skin.

The Python script, which I called HelloWorld.py, placed in the Resources folder of the skin (adjustable):

Code: Select all

print("Hello, World!")
The skin .INI code (basically a slightly modified version of the sample code in the manual):

Code: Select all

[Variables]

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

[MeasureRun]
Measure=Plugin
Plugin=RunCommand
Parameter="#@#HelloWorld.py"
State=Hide
OutputType=ANSI
Timeout=5000
RegExpSubstitute=1
Substitute="^$":"None"

[MeterRun]
Meter=String
FontSize=11
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=Click to Run
LeftMouseUpAction=[!CommandMeasure MeasureRun "Run"]

[MeterResult]
Meter=String
MeasureName=MeasureRun
Y=10R
FontSize=14
FontColor=255,255,255,255
Text="Python Output: %1"
AntiAlias=1
Result:
Python - RunCommand.jpg
You can use this as a basis for building whatever you want in your Python script.
You do not have the required permissions to view the files attached to this post.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: How do I use RunCommand with Python?

Post by jsmorley »

Do be aware that any skin you distribute that uses Python will require that the end-user of the skin have the Python libraries installed on their system, or that you use an approach where you use py2exe to "compile" the Python .py file to an executable .exe file.
Rich_Kat
Posts: 3
Joined: July 27th, 2020, 4:38 pm

Re: How do I use RunCommand with Python?

Post by Rich_Kat »

You can use this as a basis for building whatever you want in your Python script.
Thank you so much! Having a working example script to work with has really helped me understand this and get it working.
User avatar
Yincognito
Rainmeter Sage
Posts: 7031
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: How do I use RunCommand with Python?

Post by Yincognito »

Rich_Kat wrote: July 28th, 2020, 2:40 pm Thank you so much! Having a working example script to work with has really helped me understand this and get it working.
You're welcome. Make sure that you take jsmorley's advice into consideration as well. ;-)
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth