summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorch1p <me@ch1p.com>2017-08-29 02:41:32 +0300
committerch1p <me@ch1p.com>2017-08-29 02:41:32 +0300
commit418727e513a9a01f5e62e2299b10f7c0aedbd7ba (patch)
tree2f264c1306fb6b0f3d180a5fec0498b421e59f5f
parent16af2713de8ce9b6a1cd6163022af1b91f8ac160 (diff)
add script, update readme
-rw-r--r--README.md4
-rwxr-xr-xswap_workspaces71
2 files changed, 75 insertions, 0 deletions
diff --git a/README.md b/README.md
index 5433e23..f101541 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,5 @@
# swap_workspaces
+
+This is a simple bash script to swap all windows between two workspaces on Linux.
+
+This may be useful when you want to change the order of the workspaces but your DE doesn't have such feature (XFCE as an example).
diff --git a/swap_workspaces b/swap_workspaces
new file mode 100755
index 0000000..f7c8185
--- /dev/null
+++ b/swap_workspaces
@@ -0,0 +1,71 @@
+#!/bin/bash
+
+workspace_exists() {
+ wmctrl -d | awk '{print $1}' | grep -w -- "${1}"
+}
+
+workspace_windows() {
+ wmctrl -l | {
+ while IFS= read -r line; do
+ local parts=($line)
+ local id="${parts[@]:0:1}"
+ local workspace=${parts[@]:1:1}
+ if [[ "$workspace" != "$1" ]]; then
+ continue
+ fi
+ echo "$id"
+ done
+ }
+}
+
+move_window() {
+ wmctrl -i -r "$1" -t "$2"
+}
+
+USAGE="A program to move all windows from workspace <1> to workspace <2> and
+vice versa.
+
+This is useful when you want to reorder workspaces but your DE doesn't have such
+feature (XFCE as an example).
+
+Usage:
+ $(basename "$0") <1> <2>
+
+Dependencies:
+ wmctrl"
+
+FROM=$1
+TO=$2
+
+if [ ! -x "$(command -v wmctrl)" ]; then
+ echo "Please make sure that wmctrl is installed."
+ exit 1
+fi
+
+if [ -z "$FROM" ] || [ -z "$TO" ]; then
+ echo "$USAGE"
+ exit 1
+fi
+
+if [ "$FROM" == "$TO" ]; then
+ exit
+fi
+
+if [[ ! $(workspace_exists "$FROM") ]]; then
+ echo "workspace $FROM not found"
+ exit 1
+elif [[ ! $(workspace_exists "$TO") ]]; then
+ echo "workspace $TO not found"
+ exit 1
+fi
+
+WINDOWS_FROM=($(workspace_windows "$FROM"))
+WINDOWS_TO=($(workspace_windows "$TO"))
+
+for id in "${WINDOWS_FROM[@]}"; do
+ move_window "$id" "$TO"
+done
+
+for id in "${WINDOWS_TO[@]}"; do
+ move_window "$id" "$FROM"
+done