Title.sh
· 1.3 KiB · Bash
Ham
#!/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
| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | get_active_window_title() { |
| 4 | # 1. Check if running under X11 (or XWayland fallback context) |
| 5 | if [ "$XDG_SESSION_TYPE" = "x11" ]; then |
| 6 | xdotool getwindowfocus getwindowname 2>/dev/null |
| 7 | return |
| 8 | fi |
| 9 | |
| 10 | # 2. Check Wayland Compositors |
| 11 | case "${XDG_CURRENT_DESKTOP,,}" in |
| 12 | *gnome*) |
| 13 | # GNOME doesn't expose a straightforward CLI tool for security reasons. |
| 14 | # If you have the 'unrestricted' or 'unsafe-mode' enabled for DBus Eval: |
| 15 | 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/',)//" |
| 16 | ;; |
| 17 | *sway*) |
| 18 | # Sway/i3 native IPC |
| 19 | swaymsg -t get_tree | jq -r '.. | try select(.focused == true).name' 2>/dev/null |
| 20 | ;; |
| 21 | *hyprland*) |
| 22 | # Hyprland native IPC |
| 23 | hyprctl activewindow -j | jq -r '.title' 2>/dev/null |
| 24 | ;; |
| 25 | *) |
| 26 | echo "Unsupported or strictly sandboxed environment." |
| 27 | return 1 |
| 28 | ;; |
| 29 | case |
| 30 | } |
| 31 | |
| 32 | # Stream/Watch mode example (Uncomment if you want to poll it) |
| 33 | # while true; do get_active_window_title; sleep 1; done |
| 34 | |
| 35 | get_active_window_title |
| 36 |