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
  • Skyhighatrist@lemmy.ca
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    4 months ago
    #!/bin/bash
    
    w=$(($(i3-msg -t get_workspaces | tr , '\n' | grep '"num":' | cut -d : -f 2 | sort -rn | head -1)+ 1))
    
    i3-msg move container to workspace $w
    
    i3-msg workspace $w
    
    i3-msg move workspace to output $1
    

    Here’s my version. It’s a little different than yours and is obviously for i3. I have it bound to display specific shortcuts, so I can send the focused window to a new empty workspace on the display of my choosing.

    I also don’t use a set number of workspaces, I have 4 primary, one for each display. And I have shortcuts to switch to the primary on a given display, and then other shortcuts to cycle the active workspace on that display. So the above may not work for everyone, but it is another option. I assume it would require only minor modification to work with Sway.

    Edit: I suppose I should provide an explanation. That first line just gets the highest workspace number currently in use and adds 1 to it. Then the rest is pretty straightforward. It moves the container to the the workspace with the new number (i3 creates a new workspace for this) and then brings it into focus, and finally moves it to the display requested.