For my very personnal use, I created a very simple plasmoid (for plasma 6).
When I clic, it launches a toggleState() function.
All works nice, but now, I would like to start this function from elsewhere, like a script.
What path should I follow?
Thereâs no direct way to dot this as far as I am aware.
I donât kow if thereâs a better way, but one option would be to create a Python Script or a standalone executable which you can execute from the Plasmoid using a DataSource. Then you can make the script register a DBus Srvice with a method which terminate the script with a message like âTriggeredâ. You can make the plasmoid trigger the related function when it recieve the Triggered text.
Add this on QML side
Plasma5Support.DataSource {
id: service
engine: "executable"
onNewData: function(source, data) {
disconnectSource(source)
if (data.stdout.includes("Triggered"))
toggleState()
}
function start() { executable.connectSource("python service.py") }
}
After many hours trying to make it work, I realize that it is impossible to retrieve the output of a script that loops in this way.
I have indeed removed the self._loop.quit() from the python script, because it closes immediately, how could a bash script send it a command?
I am not sure that @Jack_White understood my need, or I expressed myself very badly.
I need to be able to send a command to my plasmoid through a bash command, such as a dbus-send here. So here the python script launched by my plasmoid would have to run permanently to wait for this command, and my plasmoid would have to receive the notification, and that, as presented, is not possible.
The way I implemented is similar to what @Jack_White suggests, though I have been using it to run commands and retrieve the output when they exit and havenât tried reading from the constant stdout stream.
So if you only want to run a simple command you simply call it and read what it returns if you want to display it, then you could use a timer and have it run at your desired interval, for example have a common file that your widget reads and you script writes.
To interact with the widget I register a D-Bus service with a python script but exposed dbus methods to get-set the data I needed. Then have the plasmoid polling from it using the get method at a fixed interval.
Excuse me but either Iâm becoming an idiot or what youâre talking about doesnât correspond at all to what Iâm trying to do.
Because, if the python script stops as soon as the service has been queried with its :
print (âTriggeredâ)
self._loop.quit()
I donât see at all how it will be able to answer the next time it is queried again. Itâs a one-time use and I donât understand its purpose or interest.
And if itâs the plasmoid that launches the python script, this python script must remain alive all the time the plasmoid is loaded to be able to send it commands.
In @Jack_White QML I donât even know what launches the start().
So I explain once again what I am trying to do:
I load my plasmoid
I trigger an event by any means like a dbus-send command, or a write to a file, or a notification, nevermind
the plasmoid detects this event and launches the toggleState() function
In no case do I want the plasmoid to launch an event retrieved by something else. Itâs the opposite!
Sorry @luisbocanegra but being a beginner, I donât understand much of what you suggested to me.
Ah, Sorry. I was in a hurry and left a few key parts from the code. You have figured it out anyway, so I guess allâs well that ends well
PS: Wouldnât the code look a bit cleaner if the DBus service is started from itâs own DataSource? Dont know about possible performance issues though
Ah, I was already wondering if I was simply missing something
I guess this would need a bit more documentation from a QML point of view so that it doesnât require understanding on how the C++/QML type system works.