4

Many apps need the functionality to retrieve the title and app of the active window. With XProp this was not problem.

xprop -root _NET_ACTIVE_WINDOW

Even with wayland it used to work by using the following script

!/usr/bin/env python3

import subprocess

if name == "main": while 1 == 1:

    print('---------------')
    print('global.get_window_actors...')
    app_command = ("gdbus call -e -d org.gnome.Shell -o /org/gnome/Shell -m org.gnome.Shell.Eval global.get_window_actors\(\)[`gdbus call -e -d org.gnome.Shell -o /org/gnome/Shell -m org.gnome.Shell.Eval global.get_window_actors\(\).findIndex\(a\=\>a.meta_window.has_focus\(\)===true\) | cut -d\"'\" -f 2`].get_meta_window\(\).get_wm_class\(\)")
    app_process = subprocess.Popen(app_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    app_retval = app_process.stdout.read()
    app_retcode = app_process.wait()
    actorsApp = app_retval.decode('utf-8').strip()
    print(actorsApp)

    time.sleep(1)

However the gnome solution has become obsolete as they removed the possiblity in its newer version.

So at the moment (25.04.2022) it is not possible to retrieve the active window name with the latest LTS version of Ubuntu.

So my question is: What is the correct way get the current window and retrieve app name und window title?

Silve2611
  • 141

1 Answers1

2

I believe you have to create a Shell extension that communicates with your application over Dbus.

This is a major flaw in Wayland that several applications are still trying to work out. You can see one such major discussion here.

Glyph
  • 233
  • 1
    I noticed an issue while using Remmina (remote desktop software) when I had upgraded to 22.04. I posted about it here: https://askubuntu.com/questions/1423282/why-doesnt-remmina-handle-sending-alt-tab-to-remote-computer-on-22-04-jammy-jel/1423761#1423761 After I disabled wayland the functionality worked as expected. – raddevus Oct 25 '22 at 18:34
  • 1
    Thx for the answer but the link you have sent is already 3 years old. My question is how can I communicate over dbus. I followed the steps here but it does not work anymore https://askubuntu.com/questions/1402702/how-to-get-the-current-active-window-in-ubuntu-22-04 – Silve2611 Nov 02 '22 at 16:17
  • My point is that you will need to define your own dbus communication protocol in your application, because the answer provided in that other solution is how to talk to Shell over dbus, which no longer works. You need to communicate with Shell from within Shell, as a JavaScript extension, exfiltrate the information you need, then send that to your application somehow (not necessarily over dbus). The docs are quite thin, but what you need should be in here somewhere: https://gjs-docs.gnome.org/st10/ – Glyph Nov 06 '22 at 20:49
  • @Glyph How would this work. I can't seem to access the information of another application and get data from it. What I want is only the title of the window. – Silve2611 Nov 09 '22 at 10:40
  • In your extension I think you need to subscribe to window-added / window-removed events and maintain your own internal list, like this one: https://github.com/siefkenj/gnome-shell-windowlist/blob/master/windowlist%40o2net.cl/extension.js#L375-L376 and then you can find the focused one by sorting like this https://github.com/siefkenj/gnome-shell-windowlist/blob/3b593b48dd3885a53de83dc3eb27799027a51bb3/windowlist%40o2net.cl/extension.js#L455-L463 . My guess is that this is a Meta.Window, which has a get_title: https://gjs-docs.gnome.org/meta11~11_api/meta.window – Glyph Nov 10 '22 at 18:10
  • Sorry that this is all so vague, but actually building one of these things is a major project on a platform I hardly ever use, so it may be years before I get to it :-( – Glyph Nov 10 '22 at 18:11