Is there a method to get the PID of the currently focused window in wayland?

I am using the sway window manager. Thank your for your help!


swaymsg -t get_tree returns a JSON-encoded layout tree of all open windows. Generated JSON is quite large but it is easy to parse it with jq tool.

You can run this script from demnu to display focused window info:

swaymsg -t get_tree \                                                                                     
  | jq '.. | (.nodes? // empty)[] | select(.focused==true) | {name, pid}' \
  | swaynag -m "Window Info" -l
  • 1
    Thank you for the answer. I should habe mentioned that swaymsg -t get_tree does not output the PID at the time of writing. Now this feature thankfully was implementet. – Marton Dec 14 '18 at 11:36

This works with floating or tiled windows:

swaymsg -t get_tree | jq '.. | select(.type?) | select(.focused==true).pid'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.