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.

#!/bin/bash

# this script moves a container to an empty workspace
#  and switches to that workspace

# Define list of available workspaces
all_workspaces_list=(1 2 3 4 5 6 7 8 9 10)

# get workspaces currently being used
used_workspaces=$(swaymsg -t get_workspaces | grep -oP '"name": "\K\d+')
# create a list from used workspaces
used_workspaces_list=($used_workspaces)

# Check for first of all_workspaces_list not in used_workspaces_list
for workspace in "${all_workspaces_list[@]}"; do
    if [[ ! "${used_workspaces_list[*]}" =~ "$workspace" ]]; then
        free_workspace=$workspace
        break # stop loop after finding first available workspace
    fi
done

swaymsg move container to workspace number $free_workspace
swaymsg workspace number $free_workspace