KWin Script to set Window Positions on Spawn in Plasma 6

I used this script in Plasma 5 mainly to stop windows from cascading when I open the same one, but it seems to have broken.

workspace.clientAdded.connect(window => {
    if (!window.normalWindow) return;
    area = workspace.clientArea(KWin.MaximizeArea, window);
    window.geometry.x = area.x + area.width / 2 - window.width / 2;
    window.geometry.y = area.y + area.height / 2 - window.height / 2;
});

I’m not very familiar with KWin scripting, but it has no errors when running it in plasma-interactiveconsole --kwin so I assume there weren’t any api name changes. Does anyone know how to fix this?

The API has changed. Either wait for the official document to update, or look there:

1 Like

Based on changes I’ve seen so far with modifications I’ve made to my own scripts, I think the following may work:

workspace.windowAdded.connect(window => {
    if (!window.normalWindow) return;
    area = workspace.clientArea(KWin.MaximizeArea, window);
    window.frameGeometry.x = area.x + area.width / 2 - window.width / 2;
    window.frameGeometry.y = area.y + area.height / 2 - window.height / 2;
});

The changes that I’m aware of here are:

  • workspace.clientAddedworkspace.windowAdded
  • window.geometrywindow.frameGeometry

Edit: I’ve just tested this and it seems to work, though it seems like it just centers windows.

If that’s what the script is meant to be doing, could you not go to System Settings and under Window ManagementWindow BehaviorAdvanced and set Window Placement to Centered?

Edit 2: Never mind, I see that still cascades windows in the center.

1 Like

Thanks! It seems like the line with if (!window.normalWindow) return; causes an error for me, but I’ll just look around in the code to see.

That’s rather odd as I’d expect any window to have that property and it’s still a valid window property based on the current code:

If you enable debug output for KWin Scripting and follow the journal when you open windows, do you get helpful errors?