Same concept as airblader's answer, but in bash.
Requires jq -
apt-get install -y jq or equivalent. This is a lightweight JSON parser.
Put the script in your path, then bind as:
bindsym $mod+Shift+exclam move workspace 1; exec i3-switch-if-workspace-empty.sh 1
To run it manually, just pass it the number to switch to, if the current workspace is empty.
if [ "" == "$new_workspace" ]; then
echo expecting workspace name/number
active_workspace=$( i3-msg -t get_workspaces | jq '. | select(.focused).num' )
toplevel_containers=$( i3-msg -t get_tree | jq '(.nodes.nodes.nodes | select(.type=="workspace") | select(.num=='"$active_workspace"') | .nodes | length) // 0' )
if [ "$toplevel_containers" -eq "0" ]; then
i3-msg workspace $new_workspace
Tested with multiple monitors as well. Hope this helps.
Again, credit to airblader for the original solution and pointing me to i3 tree!
(it is also on github, but not enough karma to post links)
edit: fixed a bug - was looking at the second-top level rather than the top-level. should work now.