I recently installed Ubuntu 17.10 which uses Wayland instead of (or in some sort of combination?) X11. Before I could use xprop -root|grep ^_NET_CLIENT_LIST or wmctrl (wmctrl -lpGxu) to get a list of all active windows. This doesn't work any more with all gnome applications like the terminal and some others like nautlius. Is there any way to list those?
-
You can't. This stackoverflow question has more details: stackoverflow.com/questions/45465016/…Glyph– Glyph2018-01-25 09:56:14 +00:00Commented Jan 25, 2018 at 9:56
1 Answer
Update
Sadly, this no longer works on Gnome 41 for security reasons
Running global.context.unsafe_mode = true in Looking Glass re-enables the functionality, but only temporarily.
Original answer
Yeah, on Wayland, sadly Xorg utilities like wmctrl and xdotool do not function. Instead we can talk to the window manager.
For Gnome, we can run gdbus to send a DBUS message to execute some GJS (JavaScript bindings for the GNOME C APIs).
To get a list of windows, with their class and title (using sed and jq to prettify):
$ gdbus call \
--session \
--dest org.gnome.Shell \
--object-path /org/gnome/Shell \
--method org.gnome.Shell.Eval "
global
.get_window_actors()
.map(a=>a.meta_window)
.map(w=>({class: w.get_wm_class(), title: w.get_title()}))" \
| sed -E -e "s/^\(\S+, '//" -e "s/'\)$//" \
| jq .
Example output:
[
{
"class": "firefox",
"title": "Mozilla Firefox"
},
{
"class": "org.gnome.Nautilus",
"title": "Downloads"
},
{
"class": "Google-chrome",
"title": "ubuntu - Bash command to focus a specific window - Super User - Google Chrome"
},
{
"class": "sublime_text",
"title": "untitled (dotfiles) - Sublime Text"
},
{
"class": "gnome-terminal-server",
"title": "Projects"
},
{
"class": "Gnome-shell",
"title": "gnome-shell"
}
]
To get the class of the currently focused window:
$ gdbus call \
--session \
--dest org.gnome.Shell \
--object-path /org/gnome/Shell \
--method org.gnome.Shell.Eval "
global
.get_window_actors()
.map(a=>a.meta_window)
.find(w=>w.has_focus())
.get_wm_class()" \
| cut -d'"' -f 2
gnome-terminal-server
You can play around with what's possible in GJS using Gnome's 'Looking Glass' debugger: Alt+F2, and run lg
-
The first
gdbus calldoesn't work for me on ubuntu 22.04, returns(false, '')smido– smido2024-02-21 17:48:35 +00:00Commented Feb 21, 2024 at 17:48 -
@smido Even after the
global.context.unsafe_mode = true?ZimbiX– ZimbiX2024-02-28 16:28:34 +00:00Commented Feb 28, 2024 at 16:28 -
Sorry it works with that call before. Thankssmido– smido2024-03-19 11:06:37 +00:00Commented Mar 19, 2024 at 11:06
-
How do we do this from another tty than where the desktop is running?trusktr– trusktr2025-04-09 03:06:21 +00:00Commented Apr 9 at 3:06