#!/usr/bin/env bash get_active_window_title() { # 1. Check if running under X11 (or XWayland fallback context) if [ "$XDG_SESSION_TYPE" = "x11" ]; then xdotool getwindowfocus getwindowname 2>/dev/null return fi # 2. Check Wayland Compositors case "${XDG_CURRENT_DESKTOP,,}" in *gnome*) # GNOME doesn't expose a straightforward CLI tool for security reasons. # If you have the 'unrestricted' or 'unsafe-mode' enabled for DBus Eval: 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_title()" 2>/dev/null | sed "s/('//;s/',)//" ;; *sway*) # Sway/i3 native IPC swaymsg -t get_tree | jq -r '.. | try select(.focused == true).name' 2>/dev/null ;; *hyprland*) # Hyprland native IPC hyprctl activewindow -j | jq -r '.title' 2>/dev/null ;; *) echo "Unsupported or strictly sandboxed environment." return 1 ;; case } # Stream/Watch mode example (Uncomment if you want to poll it) # while true; do get_active_window_title; sleep 1; done get_active_window_title