我在中运行Matlab
脚本workspace 1
。这将生成多个图。同时,我转到workspace 2
那里工作。我的问题是,地块正在弹出workspace 2
。是否可以将软件锁定到工作区中。因此,在中Matlab
生成图时workspace 1
,我可以在workspace 2
不中断弹出图的情况下进行工作吗?
xprop WM_CLASS
,然后单击窗口吗?)还请添加Matlab的WM_CLASS。
我在中运行Matlab
脚本workspace 1
。这将生成多个图。同时,我转到workspace 2
那里工作。我的问题是,地块正在弹出workspace 2
。是否可以将软件锁定到工作区中。因此,在中Matlab
生成图时workspace 1
,我可以在workspace 2
不中断弹出图的情况下进行工作吗?
xprop WM_CLASS
,然后单击窗口吗?)还请添加Matlab的WM_CLASS。
Answers:
下面是第一个答案的脚本重写版本(下)。区别:
WM_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
该脚本同时需要wmctrl
和xdotool
:
sudo apt-get install wmctrl xdotool
将上面的脚本复制到一个空文件中,另存为 lock_towspace.py
在您的特定应用程序中,找到WM_CLASS
:打开您的应用程序,在终端中运行:
xprop WM_CLASS and click on the window of the application
输出看起来像(在您的情况下):
WM_CLASS: WM_CLASS(STRING) = "sun-awt-X11-XFramePeer", "MATLAB R2015a - academic use"
使用命令的第一部分或第二部分来运行脚本。
然后,运行脚本的命令是:
python3 /path/to/lock_towspace.py "sun-awt-X11-XFramePeer" 2,2
在命令的最后一部分;2,2
是您要以“人类”格式将应用程序锁定到的工作空间(无空格:(!)column,row);第一列/行是1,1
下面的脚本将特定的应用程序锁定到其初始工作空间。如果脚本已启动,它将确定应用程序驻留在哪个工作区上。该应用程序产生的所有其他窗口将在瞬间移到相同的工作区。
通过自动重新聚焦在生成其他窗口之前聚焦的窗口,可以解决焦点问题。
#!/usr/bin/env python3
import subprocess
import time
import math
app_class = '"gedit", "Gedit"'
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):
# get vector of the current workspace to the origin of the spanning desktop (flipped y-axis)
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
xw = ws_size[0]; yw = ws_size[1]
return (math.ceil((w_data[1]-xw)/xw), math.ceil((w_data[2]-yw)/yw))
def get_abswindowpos(ws_size, w_data):
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):
return subprocess.check_output(["xprop", "-id", w_id, "WM_CLASS"]).decode("utf-8").split("=")[-1].strip()
def filter_windows(app_class):
# find windows (id, x_pos, y_pos) of app_class
try:
raw_list = [l.split() for l in subprocess.check_output(["wmctrl", "-lG"]).decode("utf-8").splitlines()]
return [(l[0], int(l[2]), int(l[3]), l[4], l[5]) for l in raw_list if wm_class(l[0]) == app_class]
except subprocess.CalledProcessError:
pass
ws_size = get_wssize()
init_window = get_abswindowpos(ws_size, filter_windows(app_class)[0])
valid_windows1 = filter_windows(app_class)
while True:
focus = subprocess.check_output(["xdotool", "getwindowfocus"]).decode("utf-8")
time.sleep(1)
valid_windows2 = filter_windows(app_class)
if all([valid_windows2 != None, valid_windows2 != valid_windows1]):
for t in [t for t in valid_windows2 if not t[0] in [w[0] for w in valid_windows1]]:
absolute = get_abswindowpos(ws_size, t)
if not absolute == init_window:
current = get_current(ws_size)
move = ((init_window[0]-current[0])*ws_size[0], (init_window[1]-current[1])*ws_size[1]-56)
new_w = "wmctrl -ir "+t[0]+" -e "+(",").join(["0", str(t[1]+move[0]), str(t[2]+move[1]), t[3], t[4]])
subprocess.call(["/bin/bash", "-c", new_w])
focus = str(hex(int(focus)))
z = 10-len(focus); focus = focus[:2]+z*"0"+focus[2:]
if not wm_class(focus) == app_class:
subprocess.Popen(["wmctrl", "-ia", focus])
valid_windows1 = valid_windows2
该脚本同时需要wmctrl
和xdotool
sudo apt-get install wmctrl xdotool
将脚本复制到一个空文件中,另存为 keep_workspace.py
通过打开应用程序来确定应用程序的“ WM_CLASS”,然后打开一个终端并运行以下命令:
xprop WM_CLASS
然后单击您的应用程序窗口。复制输出,看起来像"sun-awt-X11-XFramePeer", "MATLAB R2015a - academic use"
您的情况,然后将其放在脚本头部的单引号之间,如所示。
使用以下命令运行脚本:
python3 /path/to/keep_workspace.py
如果您愿意,我将添加一个切换功能。尽管它在我的系统上已经工作了几个小时,但是它可能需要先进行一些调整。
尽管您不会注意到它,但是该脚本确实会给系统增加一些处理器负载。在我的老年人系统上,我注意到增加了3-10%。如果您喜欢它的工作方式,我可能会进一步调整它以减少负载。
实际上,脚本假定次要窗口与主窗口属于同一类,就像您在注释中指出的那样。通过(非常)简单的更改,辅助窗口可以属于另一类。
尽管对于普通读者来说可能不是很有趣,但是该脚本通过向量进行工作。启动时,脚本将计算:
wmctrl -d
wmctrl -lG
从那时起,该脚本将查找同一应用程序的新窗口,并输出xprop WM_CLASS
,以与上述相同的方式查找它们的位置,然后将其移至“原始”工作区。
由于新创建的窗口从用户最近使用的窗口中“窃取”了焦点,因此随后将焦点设置为之前具有焦点的窗口。
application
和设置的workspace
文件。如果遇到可能的错误,请提及!
WM_CLASS
是相同的,因此第二个将被移至您在命令中设置的那个。