Skip to content

Instantly share code, notes, and snippets.

@dshoreman
Last active May 11, 2024 23:00
Show Gist options
  • Save dshoreman/278091a17c08e30c46c7e7988b7c2f7d to your computer and use it in GitHub Desktop.
Save dshoreman/278091a17c08e30c46c7e7988b7c2f7d to your computer and use it in GitHub Desktop.
Swaymsg commands for listing windows and outputs

i3

Get Active Window ID

xdotool getactivewindow

Get Current Desktop ID

xdotool get_desktop_for_window "$(xdotool getactivewindow)"

Sway

List output positions

$ swaymsg -t get_outputs | jq -r '.[].rect | "\(.x),\(.y) \(.width)x\(.height)"'
0,0 1280x800

With Names

$ swaymsg -t get_outputs | jq -r '.[] | {name} + .rect | "\(.x),\(.y) \(.width)x\(.height) \(.name)"'
0,0 1280x800 Virtual-1

Select windows

$ swaymsg -t get_tree | jq -r \
> '.. | (.nodes? // empty)[] | select(.pid and .visible) | .rect | "\(.x),\(.y) \(.width)x\(.height)"'
1280,47 1280x1553
0,47 1280x764
0,836 1280x764

With Names

$ swaymsg -t get_tree | jq -r \
> '.. | (.nodes? // empty)[] | select(.pid and .visible) | {name} + .rect
  | "\(.x),\(.y) \(.width)x\(.height) \(.name)"'

Misc (Untested, or for older Sway versions)

Select whole output

swaymsg -t get_outputs | jq -r '.[] | select(.active) | .rect | "\(.x),\(.y) \(.width)x\(.height)"' | slurp

Windows from workspace where id = 4

swaymsg -t get_tree | jq '.. | (.nodes? // empty)[] | select(.type == "workspace" and .id == 4) | (.nodes? // empty)[] | select(.pid) | .rect | "\(.x),\(.y) \(.width)x\(.height)"' | slurp

All windows from all visible workspaces

swaymsg -t get_tree | jq --argjson visible "$(swaymsg -t get_workspaces | jq -c '[.[] | select(.visible) | .id]')" '.. | (.nodes? // empty)[] | select(.type == "workspace" and .id == $visible[]) | .. | (.nodes? // empty)[] | select(.pid) | .rect | "\(.x),\(.y) \(.width)x\(.height)"' | slurp

Windows and outputs

swaymsg -t get_tree | jq -r 'recurse(.nodes[]?, .floating_nodes[]?) | select(.visible or (.type == "output" and .active)) | .rect | "\(.x),\(.y) \(.width)x\(.height)"'
@tleb
Copy link

tleb commented May 22, 2023

Hi. Thanks for the examples. There are two typos in the "List output positions" examples. Notice \(.height) rather than \(height) and the double quote in the second example that was moved to the end of the template string.

@@ -15,12 +15,12 @@ xdotool get_desktop_for_window "$(xdotool getactivewindow)"

 ## List output positions
 ```sh
-$ swaymsg -t get_outputs | jq -r '.[].rect | "\(.x),\(.y) \(.width)x\(height)"'
+$ swaymsg -t get_outputs | jq -r '.[].rect | "\(.x),\(.y) \(.width)x\(.height)"'
 0,0 1280x800
 ```
 ### With Names
 ```sh
-$ swaymsg -t get_outputs | jq -r '.[] | {name} + .rect | "\(.x),\(.y) \(.width)x\(height)" \(.name)'
+$ swaymsg -t get_outputs | jq -r '.[] | {name} + .rect | "\(.x),\(.y) \(.width)x\(.height) \(.name)"'
 0,0 1280x800 Virtual-1
 ```

@dshoreman
Copy link
Author

@tleb Thanks, well spotted! I'll be honest, I forgot I even made this Gist... Fixed now :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment