如何将应用程序(及其所有新窗口)锁定到特定工作区?

OHL*_*ÁLÁ 11 unity workspaces matlab 14.04

Matlabworkspace 1. 这会生成几个图。与此同时,我切换到workspace 2那里工作。我的问题是情节在workspace 2. 是否可以将软件锁定到工作区中。因此,虽然在 中Matlab生成图workspace 1,我可以在workspace 2不中断弹出图的情况下工作?

Jac*_*ijm 8

重要编辑

下面是第一个答案(如下)中脚本的重写版本。区别:

  • 该脚本现在的资源非常少(就像使用后台脚本一样)。这些动作现在被安排为在(且仅当)需要时执行。该循环几乎什么都不做,只是检查是否出现新窗口。
  • BotWM_CLASS和目标工作区现在是运行脚本的参数。仅使用WM_CLASS(见下文:如何使用)的第一或第二(识别)部分
  • 脚本现在将焦点放在当前活动的窗口上(实际上会在一瞬间重新聚焦)
  • 当脚本启动时,它会显示一个通知(示例gedit):

    在此处输入图片说明

剧本

#!/usr/bin/env python3
import subprocess
import sys
import time
import math

app_class = sys.argv[1]
ws_lock = [int(n)-1 for n in sys.argv[2].split(",")]

def check_wlist():
    # get the current list of windows
    try:
        raw_list = [
            l.split() for l in subprocess.check_output(
                ["wmctrl", "-lG"]
                ).decode("utf-8").splitlines()
            ]
        ids = [l[0] for l in raw_list]
        return (raw_list, ids)
    except subprocess.CalledProcessError:
        pass

def get_wssize():
    # get workspace size
    resdata = subprocess.check_output(["xrandr"]).decode("utf-8").split()
    i = resdata.index("current")
    return [int(n) for n in [resdata[i+1], resdata[i+3].replace(",", "")]]

def get_current(ws_size):
    # vector of the current workspace to origin of the spanning desktop
    dt_data = subprocess.check_output(
        ["wmctrl", "-d"]
        ).decode("utf-8").split()
    curr = [int(n) for n in dt_data[5].split(",")]
    return (int(curr[0]/ws_size[0]), int(curr[1]/ws_size[1]))

def get_relativewinpos(ws_size, w_data):
    # vector to the application window, relative to the current workspace
    xpos = int(w_data[2]); ypos = int(w_data[3])
    xw = ws_size[0]; yw = ws_size[1]
    return (math.ceil((xpos-xw)/xw), math.ceil((ypos-yw)/yw))

def get_abswindowpos(ws_size, w_data):
    # vector from the origin to the current window's workspace (flipped y-axis)
    curr_pos = get_current(ws_size)
    w_pos = get_relativewinpos(ws_size, w_data)
    return (curr_pos[0]+w_pos[0], curr_pos[1]+w_pos[1])

def wm_class(w_id):
    # get the WM_CLASS of new windows
    return subprocess.check_output(
        ["xprop", "-id", w_id.strip(), "WM_CLASS"]
        ).decode("utf-8").split("=")[-1].strip()

ws_size = get_wssize()
wlist1 = []
subprocess.Popen(["notify-send", 'workspace lock is running for '+app_class])

while True:
    # check focussed window ('except' for errors during "wild" workspace change)
    try:
        focus = subprocess.check_output(
            ["xdotool", "getwindowfocus"]
            ).decode("utf-8")
    except subprocess.CalledProcessError:
        pass
    time.sleep(1)
    wdata = check_wlist() 
    if wdata !=  None:
        # compare existing window- ids, checking for new ones
        wlist2 = wdata[1]
        if wlist2 != wlist1:
            # if so, check the new window's class
            newlist = [[w, wm_class(w)] for w in wlist2 if not w in wlist1]
            valids = sum([[l for l in wdata[0] if l[0] == w[0]] \
                          for w in newlist if app_class in w[1]], [])
            # for matching windows, check if they need to be moved (check workspace)
            for w in valids:
                abspos = list(get_abswindowpos(ws_size, w))
                if not abspos == ws_lock:
                    current = get_current(ws_size)
                    move = (
                        (ws_lock[0]-current[0])*ws_size[0],
                            (ws_lock[1]-current[1])*ws_size[1]-56
                        )
                    new_w = "wmctrl -ir "+w[0]+" -e "+(",").join(
                        ["0", str(int(w[2])+move[0]),
                         str(int(w[2])+move[1]), w[4], w[5]]
                        )
                    subprocess.call(["/bin/bash", "-c", new_w])
                    # re- focus on the window that was focussed
                    if not app_class in wm_class(focus):
                        subprocess.Popen(["wmctrl", "-ia", focus])
        wlist1 = wlist2
Run Code Online (Sandbox Code Playgroud)

如何使用

  1. 该脚本需要wmctrlxdotool

    sudo apt-get install wmctrl xdotool
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将上面的脚本复制到一个空文件中,另存为 lock_towspace.py

  3. 在您的特定应用程序中,找出WM_CLASS:打开您的应用程序,在终端中运行:

    xprop WM_CLASS and click on the window of the application
    
    Run Code Online (Sandbox Code Playgroud)

    输出将如下所示(在您的情况下):

    WM_CLASS: WM_CLASS(STRING) = "sun-awt-X11-XFramePeer", "MATLAB R2015a - academic use"
    
    Run Code Online (Sandbox Code Playgroud)

    使用命令中的第一部分或第二部分来运行脚本。

  4. 然后运行脚本的命令是:

    python3 /path/to/lock_towspace.py "sun-awt-X11-XFramePeer" 2,2
    
    Run Code Online (Sandbox Code Playgroud)

    在命令中,最后一节;2,2是您要将应用程序锁定到的工作区(没有空格:(!) column, row),采用“人类”格式;第一列/行是1,1

  5. 通过运行脚本来测试它。运行时,打开您的应用程序,让它照常生成窗口。所有窗口都应出现在目标工作区上,如命令中设置的那样。

过时的答案:

(二)测试版

下面的脚本将特定应用程序锁定到其初始工作区。如果脚本启动,它将确定应用程序驻留在哪个工作区。应用程序生成的所有其他窗口将在瞬间移动到同一个工作区。

通过自动重新聚焦在生成附加窗口之前聚焦的窗口来解决聚焦问题。

剧本

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

如何使用

  1. 该脚本同时需要wmctrlxdotool

    sudo apt-get install wmctrl xdotool
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将脚本复制到一个空文件中,另存为 keep_workspace.py

  3. 通过打开应用程序来确定您的应用程序的“WM_CLASS”,然后打开终端并运行命令:

    xprop WM_CLASS
    
    Run Code Online (Sandbox Code Playgroud)

    然后单击您的应用程序的窗口。复制输出,看起来像"sun-awt-X11-XFramePeer", "MATLAB R2015a - academic use"您的情况,并将其放在脚本头部的引号之间,如图所示。

  4. 使用以下命令运行脚本:

    python3 /path/to/keep_workspace.py
    
    Run Code Online (Sandbox Code Playgroud)

如果它如你所愿,我会添加一个切换功能。虽然它已经在我的系统上工作了几个小时,但是它可能需要先进行一些调整。

笔记

尽管您不应该注意到它,但该脚本确实为系统增加了一些处理器负载。在我的老年系统中,我注意到增加了 3-10%。如果您喜欢它的工作方式,我可能会进一步调整它以减少负载。

该脚本假定辅助窗口与主窗口属于同一类,就像您在评论中指出的那样。但是,通过(非常)简单的更改,辅助窗口可以属于另一类。

解释

尽管对于普通读者来说可能不是很有趣,但该脚本通过计算向量来工作。启动时,脚本计算:

  • 从原点到当前工作空间的向量,输出为 wmctrl -d
  • 应用程序窗口的向量,相对于当前工作区,由输出 wmctrl -lG
  • 根据这两个,脚本计算应用程序窗口在跨桌面上的绝对位置(一个矩阵中的所有工作区)

从那时起,脚本会查找同一应用程序的新窗口,输出为xprop WM_CLASS, 以与上述相同的方式查找它们的位置并将它们移动到“原始”工作区。

由于新创建的窗口“窃取”了用户上次使用的窗口的焦点,因此焦点随后被设置为之前具有焦点的窗口。