DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Swap Windows Between Workspaces In X-Window
#!/bin/bash
# Swap windows between two worspaces (indexes: 0..N-1)
#
# Author: Arnau Sanchez <tokland@gmail.com>
# License: GNU/GPL v3
# Depends: bash, xargs, wnckprop (libwnck)
#
# Example:
#
# $ swap_workspaces 1 2
# workspace 1 windows: 20971523 27263001 35677298
# workspace 2 windows: 20971523 27263001 58720259 73400323
# common windows (ignore them): 20971523 27263001
# 35677298: moved to workspace 2
# 58720259: moved to workspace 1
# 73400323: moved to workspace 1
#
set -e
# Echo text to standard error
debug() {
echo "$@" >&2
}
# Return geometry (x y width height) for window
get_window_geometry() {
local WID=$1
LC_ALL=C wnckprop --window=$WID | grep "^Geometry" | \
cut -d":" -f2 | sed "s/,/ /g" | xargs
}
# Get windows IDs for a given workspace
get_windows_for_workspace() {
local WORKSPACE=$1
wnckprop --list --workspace=$WORKSPACE | cut -d":" -f1 | xargs
}
# Move windows to workspace (accepts list of windows to ignore)
move_windows_to_workspace() {
local WORKSPACE=$1
local WIDS=$2
local IGNORE_WIDS=$3
for WID in $WIDS; do
if ! grep -q "\<$WID\>" <<< "$IGNORE_WIDS"; then
wnckprop --set-workspace=$WORKSPACE --window=$WID
debug "$WID: moved to workspace $WORKSPACE"
fi
done
}
### Main
[ $# -eq 2 ] || { debug "Usage: $(basename $0) WORKSPACE1 WORKSPACE2"; exit 1; }
WS1N=$1
WS2N=$2
WS1=$(get_windows_for_workspace "$WS1N")
WS2=$(get_windows_for_workspace "$WS2N")
COMMON=$(echo "$WS1 $WS2" | xargs -n1 | sort | uniq -d | xargs)
debug "workspace $WS1N windows: $WS1"
debug "workspace $WS2N windows: $WS2"
debug "common windows (will be ignored): $COMMON"
move_windows_to_workspace "$WS2N" "$WS1" "$COMMON"
move_windows_to_workspace "$WS1N" "$WS2" "$COMMON"





