我使用的是大型 4K 显示器,其中有多个工作空间,每个工作空间都有多个应用程序。重新启动 Ubuntu 18.04 时,应用程序窗口的位置会丢失,我需要几分钟才能重新配置它们。如何保存和恢复工作空间内的窗口位置?
答案1
Gnome 扩展“自动移动窗口”将在打开应用程序时将其移动到指定的工作区。应用程序窗口在工作区内的位置也会被记住。请参阅https://extensions.gnome.org/extension/16/auto-move-windows/
请注意,对于使用脚本的 Ubuntu 早期版本(带 Unity),有解决方案。但是,Ubuntu 在 18.04 中切换回使用 Gnome。(例如,保存和恢复窗口位置和如何启动具有预定义窗口大小和位置的应用程序?)
答案2
答案3
我刚刚编写了这个 python 脚本来打开不同的应用程序并在启动时将它们移动到特定的工作区。它在 Ubuntu 20 上对我有用。除了 python 之外的唯一依赖项是wmctrl
(在 Ubuntu 上可能默认安装?)。让它在启动时运行 - 我把它放在python3 /path/to/script.py
命令字段中的启动应用程序中。
这里可能需要进行优化,但留给任何遇到此问题的人使用/更改。如果按原样使用脚本,请更改列表apps
以保存所需的程序。仅更改列表中每个元组的前 3 个元素 - 第一个是从终端启动程序的命令,第二个是工作区编号,第三个是用于在输出中识别程序的字符串- 例如,使用输出中的wmctrl -l
行,搜索词可以是。0x02200005 8 fiona New Tab - Google Chrome
wmctrl
Google Chrome
import subprocess
import time
import datetime
def run_command_in_background(command):
# command is list of strings
subprocess.Popen(command)
def get_wmctrl_lines():
return subprocess.run(["wmctrl", "-l"], capture_output=True, text=True).stdout.splitlines()
finito_ids = set()
def get_running_app_window_id_desktop_nb(search_term):
for line in get_wmctrl_lines():
if search_term in line.lower():
split = line.split()
window_id = split[0]
if window_id not in finito_ids:
return split[0], int(split[1])
return None, None
def move_window_to_desktop(window_id, desktop_number):
# desktop = workspace??
run_command_in_background(["wmctrl", "-i", "-r", str(window_id), "-t", str(desktop_number)])
apps = [
# app_command, desktop_nb, search_term, should_be_full_screen, has_been_spawned, has_been_formatted
(["vivaldi"],2,"vivaldi",False,False,False),
(["terminator"],3,"/bin/bash",True,False,False),
(["terminator"],4,"/bin/bash",True,False,False),
(["terminator"],5,"/bin/bash",True,False,False),
(["snap","run","spotify"],6,"spotify",False,False,False),
(["google-chrome"],8,"chrome",False,False,False),
]
# move apps
while False in [a[-1] for a in apps]:
new_apps = []
for app in apps:
app_command, desktop_nb, search_term, should_be_full_screen, has_been_spawned, has_been_formatted = app
if not has_been_spawned:
run_command_in_background(app_command)
has_been_spawned = True
else:
app_window_id, current_desktop_nb = get_running_app_window_id_desktop_nb(search_term)
if app_window_id is not None:
run_command_in_background(["wmctrl", "-i", "-r", app_window_id, "-t", str(desktop_nb)])
if should_be_full_screen:
run_command_in_background(["wmctrl", "-i", "-r", app_window_id, "-b", "toggle,fullscreen"])
has_been_formatted = True
finito_ids.add(app_window_id)
else:
print("app %s still not running" % search_term)
new_apps.append((app_command, desktop_nb, search_term, should_be_full_screen, has_been_spawned, has_been_formatted))
apps = new_apps
time.sleep(1)
# send notification that everything is ready
run_command_in_background(["notify-send", "Hola we", "apps/workspaces are set up"])