Bash script to move focused container to new empty workspace
So I migrated from i3 to sway. Had a python script that I found on the internets that did this, and really like the functionality. Figured I’d give an attempt at making my own script in bash. My programming skills and bash scripting aren’t great, so I had chatGPT help me with some syntax. Thought others might be interested so am sharing here.
<span style="color:#323232;">#!/bin/bash
</span><span style="color:#323232;">
</span><span style="color:#323232;"># this script moves a container to an empty workspace
</span><span style="color:#323232;"># and switches to that workspace
</span><span style="color:#323232;">
</span><span style="color:#323232;"># Define list of available workspaces
</span><span style="color:#323232;">all_workspaces_list=(1 2 3 4 5 6 7 8 9 10)
</span><span style="color:#323232;">
</span><span style="color:#323232;"># get workspaces currently being used
</span><span style="color:#323232;">used_workspaces=$(swaymsg -t get_workspaces | grep -oP '"name": "Kd+')
</span><span style="color:#323232;"># create a list from used workspaces
</span><span style="color:#323232;">used_workspaces_list=($used_workspaces)
</span><span style="color:#323232;">
</span><span style="color:#323232;"># Check for first of all_workspaces_list not in used_workspaces_list
</span><span style="color:#323232;">for workspace in "${all_workspaces_list[@]}"; do
</span><span style="color:#323232;"> if [[ ! "${used_workspaces_list[*]}" =~ "$workspace" ]]; then
</span><span style="color:#323232;"> free_workspace=$workspace
</span><span style="color:#323232;"> break # stop loop after finding first available workspace
</span><span style="color:#323232;"> fi
</span><span style="color:#323232;">done
</span><span style="color:#323232;">
</span><span style="color:#323232;">swaymsg move container to workspace number $free_workspace
</span><span style="color:#323232;">swaymsg workspace number $free_workspace
</span>
Add comment