What is the easiest way to create a Plasma script that will move my panel to a specific screen?

Hopefully this is the right section to put this in. Wasn’t sure if I should go with here or the help section.
I am trying to develop a Plasma script that can either delete and recreate or move my panel to a different screen. According to the documentation, there is a property of screen on the Panel object but it is only read only and can not be modified.

I was originally trying to use the following code to delete and recreate my Panel:

var allPanels = panels();

// Remove all existing panels
for (var panelIndex = 0; panelIndex < allPanels.length; panelIndex++) {
    var p = allPanels[panelIndex];
    p.remove();
}

// Define the widgets for the new panel
var widgets = [
"org.kde.plasma.kickoff",
"org.kde.plasma.pager",
"org.kde.plasma.marginsseparator",
"org.kde.plasma.icontasks",
"org.kde.plasma.systemtray",
"org.kde.plasma.digitalclock",
"org.kde.plasma.showdesktop"
];

// Create a new panel
var newPanel = new Panel;
newPanel.location = "top";
newPanel.hiding = "autohide";
newPanel.floating = false;
newPanel.height = 48;

// Add all widgets to the new panel
for (const widgetType of widgets) {
    newPanel.addWidget(widgetType);
}

// Apply the pinned apps as soon as the widgets are added
for (const widget of newPanel.widgets()) {
    if (widget.type === "org.kde.plasma.icontasks") {

        // Clear existing pinned apps by setting it to an empty string

        // Now apply the desired pinned apps
        var launchers = [
            "applications:org.kde.dolphin.desktop",
            "applications:firefox.desktop",
            "applications:org.kde.konsole.desktop"
        ]

        // Write the launchers list as a single string
        widget.writeConfig("launchers", launchers.join(";"));
        widget.reloadConfig();

    }
}

print("\nPanel setup complete.");

However the the problem I ran into is that when I tried to use the launchers string. It did not create multiple applications for reach launcher in the string. It created one application with the entire string. Like this:

When I only had one application it worked. but doing multiple applications in this string format doesn’t seem to work for me.

I am thinking this is a convoluted way of being able to move the panel, and there should be an easier way, but Im not 100% sure. It would be nice if I could just figure out why this code is not working and continue to use this script. That would be viable enough for me. I can just add this to my script that configures my monitors when I switch between using my laptop, and plugging into my desk setup. I would greatly appreciate any help or potential solutions

Looking at existing widget config from ~/.config/plasma-org.kde.plasma.desktop-appletsrc:

  • The currentConfigGroup needs to be set to ["General"]; before calling writeConfig
  • Also you used ; to separate the launchers but it should be ,
  • But even after doing it right it seems the launchers are not properly loaded unless the writeConfig is caleld twice??? Looks like a bug to me, it doesn’t seem to happen with other config options such as showOnlyCurrentScreen
// Apply the pinned apps as soon as the widgets are added
for (const widget of newPanel.widgets()) {
  if (widget.type === "org.kde.plasma.icontasks") {
    // Now apply the desired pinned apps
    var launchers = [
      "applications:org.kde.dolphin.desktop",
      "applications:firefox.desktop",
      "applications:org.kde.konsole.desktop",
    ];

    // Write the launchers list as a single string
    widget.currentConfigGroup = ["General"];
    // needs to be called twice for the launchers to show up properly but why? A bug?
    widget.writeConfig("launchers", launchers.join(","));
    widget.writeConfig("launchers", launchers.join(","));
    // doesn't happen if this one is set alone
    // widget.writeConfig("showOnlyCurrentScreen", true);
    widget.reloadConfig();
  }
}
print("\nPanel setup complete.");

Fortunately, soon you will be able to just set the screen property :slight_smile: It has been made writable in master and will be available in plasma 6.3 shell/scripting: Allow scripts to set the screen of a panel (!4802) · Merge requests · Plasma / Plasma Workspace · GitLab

3 Likes

This worked. I had to set the group. Switch ; to , and call writeConfig twice

I thought the list was semi colons as the seperator in the docs, so thats my bad on misreading that, but it still did require calling write config twice, which is weird.

Thanks for the help!