bashqmlkde-plasma

Execute bash command from QLM KDE-Plasma Widget


I am trying to create a widget within KDE Plasma that enables me to quickly mount an drive via rClone. Meaning I have an icon in my task bar from which I can select what drives to mount. However I did not find a way to execute a bash command from an QLM file.

What i have: I have a bash command that mounts a OneDrive folder with rClone.

// Example:
rclone mount remote:path/to/files /path/to/local/mount

What i want: I want to create a shortcut (preferably in the taskbar, like an KDE Widget) on which I can click that fires the mentioned command (mount).

What I tried: I created a Widget which I added to the Taskbar. It has a button that should fire mentioned command (like the following).

PlasmaComponents.Button {
   text: i18n("Mount")
   onClicked: ???
}

The problem: That's where I am stuck. How can I execute a bash command on button click in an Plasma widget (written in QLM)? Is there some library/plugin/api to do this? Or is there a completely different way of achieving this or something similar?


Solution

  • DavidC.Rankin pointed me towards kmenuedit (and kdialog), which is an easier way of achiving this. However, I also found an answer to my original question:

    On another site a user provided me with an answer that i want to share here. It is indeed possible to run bash commands from QLM Widgets:

    Assuming you're using Plasma 6, you should be able to do this via a "DataSource".

    If you look at the code for the Toggle Overview Widget for Plasma 6, when you go to the code, you can see how it's being used. I've actually utilized the relevant code myself for one of my own modified widgets.

    What you need to do is import org.kde.plasma.plasma5support as Plasma5Support and then setup the Plasma5Support.DataSource from there (check the code, should be from line 81 - 92 as of the time of writing this). If you use the code from there then you should be able to use the following:

    executable.exec('bash command goes here');
    

    You could also put the exec in a function and then call that function when the button is clicked.

    (From Reddit)

    To summarize: you need to

    import org.kde.plasma.plasma5support as Plasma5Support
    

    and then add the button and the DataSource to the Widget:

    PlasmaComponents.Button {
        text: i18n("Execute")
        onClicked: executable.exec('bash_code_here')
    }
    
    Plasma5Support.DataSource {
        id: executable
        engine: "executable"
        connectedSources: []
        onNewData: function(source, data) {
            disconnectSource(source)
        }
    
        function exec(cmd) {
            executable.connectSource(cmd)
        }
    }
    

    Alternatively, you can also execute a script file:

    onClicked: executable.exec('bash /path/to/yourscript')