Tray icon interaction from the terminal?

Hello again. Is it possible to interact with system tray icons in a programmatic way? For example, I want to be able to “click on the Vesktop icon” from a terminal or in some other way without needing to manually open the tray myself.

My use case for this is that a lot of applications struggle with their in-tray behavior. It’s very common for them to boot up a second instance of themselves which I do not want. It’d be easier for me to write a script that checks if the tray icon exists otherwise launch app. I have no idea if this is feasible but I figured it might as well be worth asking around.

Yes. Plasma scripting:

1 Like

Thanks for the good pointer! If you don’t mind humoring me further, would you know how to get the actual system tray items themselves? Poking around with the JS and I’m only able to interact with the widget in the system tray as opposed to other items like Telegram, Vesktop, Kleopatra, etc (basically any other application).

Here’s what I’ve written so far in an attempt to try to understand the Plasma scripting interface:

function getMethods(obj)
{
    var res = [];
    for(var m in obj) {
        if(typeof obj[m] == "function") {
            res.push(m)
        }
    }
    return res;
}

for (i = 0; i < panelIds.length; ++i) { //search through the panels
    panel = panelById(panelIds[i]);
    if (!panel) continue;

    for (tmpIndex = 0; tmpIndex < panel.widgetIds.length; tmpIndex++) {
        appletWidget = panel.widgetById(panel.widgetIds[tmpIndex]);

        if (appletWidget.type == "org.kde.plasma.systemtray") {
            systemtrayId = appletWidget.readConfig("SystrayContainmentId");
            if (systemtrayId) {
                print("systemtray id: " + systemtrayId + "\n\n")
                var systray = desktopById(systemtrayId);
                systray.currentConfigGroup = ["General"];
                print("config keys: " + systray.configKeys + "\n");
                print("extra items: " + systray.readConfig("extraItems").split(",") + "\n\n");
                print("hidden items: " + systray.readConfig("hiddenItems").split(",") + "\n\n");
                print("known items: " + systray.readConfig("knownItems").split(",") + "\n\n");
                print("scaleIconsToFit: " + systray.readConfig("scaleIconsToFit").split(",") + "\n\n");
                print("shown items: " + systray.readConfig("shownItems").split(",") + "\n\n");

                print("ATTEMPTING TO OPEN TELEGRAM");

                print(systray.widgetIds);

                for (j = 0; j < systray.widgetIds.length; ++j) {
                    var widget = systray.widgetById(systray.widgetIds[j]);
                    print (widget.type + "\n")
                }

                print("\nsystray functions: " + getMethods(systray) + "\n\n");
            }
        }
    }
}

Or another pointer towards better documentation would be helpful, since the documentation pages on the scripting page aren’t very helpful towards what I’m trying to accomplish.

No, that seems to be the only document.

If it’s an application icon, then you can try activating it through dbus:



GitHub - andrewerf/tray-control: Simple CLI tool to show items in systray and activate them. Build on top of DBus. works well. If you want to do the same in your script, you can copy their dbus use.