Answers:
大多数应用程序在其启动屏幕(从Dash或启动器)中打开其窗口。但是,某些应用程序没有这样做,但是可以通过以下脚本将命令重定向到运行该应用程序的方式来强制使用它们。为此,您将需要编辑相应的.desktop
文件(启动器)。
设置似乎有点复杂,但是如果遵循该过程(“如何使用”),则应该一点也不困难。
有一个缺点:如果用该命令替换该.desktop
文件的main命令以调用此脚本,则右键单击“ open with”将无法正常工作。对于像Google Chrome这样的网络浏览器来说,这不会是太大的问题。对于其他应用程序,一个简单的解决方案是添加选项以作为快捷方式在当前屏幕上打开一个新窗口(请参见下文)。
该脚本同时使用wmctrl
和xautomation
:
sudo apt-get install xautomation
sudo apt-get install wmctrl
创建目录(~/bin
如果尚不存在)。
将脚本复制到一个空文件中,将其另存为open_oncurrent
(无扩展名)~/bin
将相应的.desktop
文件从复制/usr/share/applications
到~/.local/share/applications
:
cp /usr/share/applications/google-chrome.desktop ~/.local/share/applications/google-chrome.desktop
在以下位置打开本地副本~/.local/share/applications
:
gedit ~/.local/share/applications/google-chrome.desktop
编辑文件(两个选项):
更改启动器的主要命令:
找到这行:
Exec=/usr/bin/google-chrome-stable %U
更改为
Exec=/bin/bash -c "open_oncurrent /usr/bin/google-chrome-stable"
要将选项添加为快捷方式(如上图所示):
找到这行:
X-Ayatana-Desktop-Shortcuts=NewWindow;NewIncognito;
替换为:
X-Ayatana-Desktop-Shortcuts=NewWindow;NewIncognito;New window on this screen;
然后将以下部分添加到文件的末尾:
[New window on this screen Shortcut Group]
Name=New window on this screen
Exec=/bin/bash -c "open_oncurrent /usr/bin/google-chrome-stable"
TargetEnvironment=Unity
同样,您可以将解决方案应用于其他应用程序。在文件中使用的命令的语法.desktop
类似于示例:
Exec=/bin/bash -c "open_oncurrent <command>"
脚本中还提供了有关如何处理异常的少量补充说明。
#!/usr/bin/env python3
import subprocess
import sys
import time
import getpass
t = 0; user = getpass.getuser(); application = sys.argv[1]
"""
In most cases, the command to run an application is the same as the process
name. There are however exceptions, to be listed below, if you use these appli-
cations i.c.w. this script. Just add an item to the list in the format:
["<command>", "<process_name>"],
"""
exceptions = [
["/usr/bin/google-chrome-stable", "chrome"],
]
try:
procname = [app[1] for app in exceptions if app[0] == application][0]
except IndexError:
procname = application
get = lambda cmd: subprocess.check_output(["/bin/bash", "-c", cmd]).decode("utf-8")
# initial position of the mouse (click position)
start_pos = int(get("xmousepos").strip().split()[0])
# x- position of right side of the screen
x_res = [int(s.split("x")[0]) for s in get("xrandr").split() if s.endswith("+0+0")][0]
# current windows
start_windows = get("wmctrl -l")
# open application
subprocess.call(["/bin/bash", "-c", application+"&"])
while t < 30:
procs = get("ps -u "+user).splitlines()
new = [w for w in get("wmctrl -lpG").splitlines() if not w.split()[0] in start_windows]
match = sum([[line for line in procs if w.split()[2] in line and procname[:15] in line] for w in new], [])
if len(match) == 1:
data = new[0].split(); curr_pos = int(data[3]); compare = (start_pos > x_res, curr_pos > x_res)
if compare[0] == compare[1]:
pass
else:
if compare[0] == True:
data[3] = str(int(data[3])+x_res)
else:
data[3] = str(int(data[3])-x_res)
cmd1 = "wmctrl -r "+data[0]+" -b remove,maximized_vert,maximized_horz"
cmd2 = "wmctrl -ir "+data[0]+" -e 0,"+(",").join(data[3:7])
for cmd in [cmd1, cmd2]:
subprocess.Popen(["/bin/bash", "-c", cmd])
break
t = t + 1
time.sleep(0.5)