如何将所有窗口移动到另一个工作区

bet*_*r07 9 unity compiz workspaces viewports

是否可以将所有窗口(或所有未最小化的窗口)从一个工作区移动到另一个工作区?

我知道我可以在一个窗口移动到其他工作区与Shift+ Ctrl+ Alt+ arrow,但它将只移动一个重点窗口。

Ser*_*nyy 6

Unity:什么是视口?

Ubuntu Unity 使用视口——基本上是一个坐标系统(坐标 0,0 是左上角),其中一个巨大的桌面细分为适合您屏幕分辨率的块。当您向右和向下移动时,坐标值会增加。

在此处输入图片说明

坐标系是相对的。如果我当前的视口位于左上角,则与该视口相关的所有内容都将以宽度和高度的增量为正值。例如,如果我当前的视口位于最左上角,则您在上方中间工作区中看到的 Firefox 窗口位于相对于最左上角视口的 x 值 1366 和 y 值 0 处。如果我的活动视口是中上一个,则最左上视口中的终端窗口位于 x 值 -1327 60 处。 这是 的关键问题xdotool,因为xdotool不处理负数。

另请注意,当前视口的左上角将始终被 xdotool 假定为坐标 0 0 。这意味着我们只能左右移动东西。

使 xdotool 为 Unity 工作

现在我们知道xdotool只能相对于我们的左上角移动窗口(即,您始终可以向下和向右移动窗口,但不能向上和向左移动)。我们如何使它为统一工作。嗯,基本的想法是

  1. 找出当前视口上的所有窗口
  2. 暂时移动到请求的视口,使左上角在该视口处采用坐标 0 0
  3. 将所有窗口移动到用户定义的视口坐标
  4. 返回旧视口(可选,也可以跟随窗口)

脚本解决方案

下面的脚本完全执行上述过程。可以使用任一-v标志调用它来手动指定坐标,也可以使用-g标志来调出 GUI 对话框。-fflag 也会告诉脚本切换视口;如果未使用该标志 - 您将保留在当前视口上,并且只会移动窗口

获取脚本

可以使用以下步骤直接或通过 github 从这篇文章中复制源代码:

  1. sudo apt-get install git
  2. cd /opt ; sudo git clone https://github.com/SergKolo/sergrep.git
  3. sudo chmod -R +x sergrep

脚本文件将是 /opt/sergrep/move_viewport_windows.sh

要将脚本绑定到快捷方式,请参阅如何将 .sh 文件绑定到键盘组合?

请注意,wmctrlxdotool是此脚本正常工作所必需的。您可以通过 sudo apt-get install xdotool 和 wmctrl 安装它们

源代码

#!/usr/bin/env bash
#
###########################################################
# Author: Serg Kolo , contact: 1047481448@qq.com 
# Date: April 17 2016
# Purpose: Move all windows on the current viewport
#          to a user-defined one
# Written for:
# Tested on: Ubuntu 14.04 LTS , Unity 7.2.6
###########################################################
# Copyright: Serg Kolo , 2016
#    
#     Permission to use, copy, modify, and distribute this software is hereby granted
#     without fee, provided that  the copyright notice above and this permission statement
#     appear in all copies.
#
#     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
#     THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
#     DEALINGS IN THE SOFTWARE.

get_active_viewport()
{
  xprop -root -notype _NET_DESKTOP_VIEWPORT
}

get_screen_geometry()
{
 xwininfo -root | awk '/-geometry/{gsub(/+|x/," ");print $2,$3}'  
}

current_wins()
{  
   HEX="$(wmctrl -lG | \
   awk -v xlim="$XMAX" -v ylim="$YMAX" \
      'BEGIN{printf "ibase=16;"} $3>0 && $3<xlim  && $4>0 && $4<ylim \
      { gsub(/0x/,""); printf "%s;",toupper($1)  } ')"
   echo $HEX | bc | tr '\n' ' '
}

gui_selection()
{
  SCHEMA="org.compiz.core:/org/compiz/profiles/unity/plugins/core/"
  read swidth sdepth  <<< "$(get_screen_geometry)"
  vwidth=$(gsettings get $SCHEMA hsize)
  vheight=$(gsettings get $SCHEMA vsize)

 width=0
 for horizontal in $(seq 1 $vwidth); do
    height=0 
    for vertical in $(seq 1 $vheight);  do

      array+=( FALSE  )
      array+=( $(echo "$width"x"$height") )

    height=$(($height+$sdepth))
    done
 width=$(($width+$swidth))
 done

 zenity --list --radiolist --column="" --column "CHOICE" ${array[@]} --width 350 --height 350 2> /dev/null
}

print_usage()
{
cat << EOF
move_viewport_windows.sh [-v 'XPOS YPOS' ] [-g] [-f ] [-h]

Copyright Serg Kolo , 2016

The script gets list of all windows on the current Unity 
viewport and moves them to user-specified viewport. If
ran without flags specified, script prints this text

-g flag brings up GUI dialog with list of viewports

-v allows manually specifying viewoport. Argument must be
   quoted, X and Y position space separated

-f if set, the viewport will switch to the same one where
   windows were sent

-h prints this text

** NOTE ** 
wmctrl and xdotool are required for this script to work
properly. You can install them via sudo apt-get install
xdotool and wmctrl

EOF
}

parse_args()
{
  if [ $# -eq 0  ];then
    print_usage
    exit
  fi
  while getopts "v:ghf" opt
 do
   case ${opt} in
     v) NEWVP=${OPTARG}
        ;;
     g) NEWVP="$(gui_selection | tr 'x' ' ' )"
        [ -z "$NEWVP" ] && exit 1
        ;;
     f) FOLLOW=true
        ;; 
     h) print_usage
        exit 0
        ;;
     \?)
      echo "Invalid option: -$OPTARG" >&2
      ;;
    esac
  done
  shift $((OPTIND-1))
}

main()
{
 # Basic idea:
 #-------------------
 # 1. get current viewport and list of windows
 # 2. go to viewport 0 0 and move all windows from list
 #    to desired viewport
 # 3. go back to original viewport or follow the windows,
 #    depending on user choice
 # 4. Tell the user where they are currently

 local FOLLOW
 local NEWVP # coordinates of desired viewport
 local XMAX YMAX # must be two vals for awk to work
 local OLDVP=$(get_active_viewport | awk -F '=' '{sub(/,/," ");print $2}' )

 parse_args "$@"

 read XMAX YMAX  <<< "$(get_screen_geometry)" # move to getopts

 windows=( $(current_wins) )

 xdotool set_desktop_viewport 0 0 
 for win in ${windows[@]} ; do
    echo "$win"
    xdotool windowmove $win $NEWVP
 done
 # sleep 0.25 # uncomment if necessary

 if [ $FOLLOW  ]; then
     xdotool set_desktop_viewport $NEWVP
 else
     xdotool set_desktop_viewport $OLDVP
 fi

 sleep 0.25 # delay to allow catching active viewport
 notify-send "current viewport is $(get_active_viewport | awk -F '=' '{sub(/,/," ");print $2}' )"
 exit 0
}
main "$@"
Run Code Online (Sandbox Code Playgroud)

演示

正在运行的脚本的 Webm 记录:

https://www.youtube.com/watch?v=cJMlC41CWWo

问题

由于 Unity 的grid插件负责窗口捕捉,脚本无法移动最大化或右/左捕捉的窗口。将尝试添加该插件的瞬时取消设置和重置以使脚本适用于所有窗口,但由于取消设置和重置有时间延迟,因此可能会放弃作为一个想法。如果您希望脚本适用于所有窗口,请unity-tweak-tool在窗口管理器选项下安装和取消设置窗口捕捉。


Glu*_*ate 5

非基于 Compiz 的桌面环境(XFCE、LXDE、GNOME、KDE...)

为此,您可以使用wmctrl和的组合。xdotool首先确保安装了这两个实用程序:

sudo apt-get install xdotool wmctrl
Run Code Online (Sandbox Code Playgroud)

满足依赖关系后,您应该能够使用以下一行将当前桌面上的所有窗口移动到另一桌面:

sudo apt-get install xdotool wmctrl
Run Code Online (Sandbox Code Playgroud)

使用的命令的快速细分:

  • wmctrl -l | awk -v var=$(xdotool get_desktop) '{if ($2 == var) print $0;}' | cut -d' ' -f1

    列出所有窗口,过滤掉那些不在当前工作区的窗口,并提取它们的窗口 ID

  • wmctrl -i -t 2 -r "$i"

    将具有窗口 ID 的窗口移动$i到工作区 2。

  • 所有这些都包含在一个简单的while read ... do; done循环中,该循环遍历当前桌面上的所有窗口

基于 Compiz 的桌面环境(例如 Unity)

由于 Compiz(Unity 的窗口管理器)不使用传统意义上的桌面,因此为 Unity 等桌面环境寻找解决方案变得很困难。