我正在尝试编写一个脚本来打开一堆程序并移动/调整屏幕上的窗口大小。
例如,
#!/bin/bash
zim
wmctrl -r Zim -b toggle,maximized_vert
wmctrl -r Zim -e 0,700,0,-1,-1如果我运行这个脚本,窗口就会最大化,并稍微向右移动。但是,如果我将zim替换为firefox或acroread,则无法移动/调整窗口的大小。
如果我在终端中输入wmctrl,它确实有效,但是,我希望它在脚本中。我认为这肯定与firefox在屏幕上记忆其位置的方式有关。
编辑:我把
firefox
wmctrl -lG在脚本中,我得到以下输出:
0x03800032 0 1168 347 750 731 briareos emacs@briareos
0x02a00002 0 -2020 -1180 1920 1080 briareos XdndCollectionWindowImp
0x02a00005 0 0 24 47 1056 briareos unity-launcher
0x02a00008 0 0 0 1920 24 briareos unity-panel
0x02a0000b 0 -420 -300 320 200 briareos unity-dash
0x02a0000c 0 -420 -300 320 200 briareos Hud
0x03c00011 0 59 52 900 1026 briareos Notes - Zim这意味着脚本不知道Firefox已经启动。
发布于 2016-09-07 20:11:51
问题是,在您使用的命令组合中,需要“幸运”才能及时显示应用程序的窗口,wmctrl命令才能成功。
您的命令可能在大多数情况下适用于轻量级应用程序,可以快速启动,但会中断其他应用程序,如Inkscape、Firefox或Thunderbird。
你可以像你在评论中提到的那样,在5秒或10秒内进行构建,但是要么你不得不等待更长的时间,要么它就会中断--如果你的处理器被占用,窗口“比平常晚了”。
然后,解决方案是在脚本中包含一个过程,等待窗口出现在wmctrl -lp的输出中,然后运行命令以使窗口最大化。
在下面的python示例中,我使用xdotool来调整窗口的大小,在我的经验中比wmctrl更健壮:
#!/usr/bin/env python3
import subprocess
import getpass
import time
import sys
app = sys.argv[1]
# to only list processes of the current user
user = getpass.getuser()
get = lambda x: subprocess.check_output(x).decode("utf-8")
# get the initial window list
ws1 = get(["wmctrl", "-lp"]); t = 0
# run the command to open the application
subprocess.Popen(app)
while t < 30:
# fetch the updated window list, to see if the application's window appears
ws2 = [(w.split()[2], w.split()[0]) for w in get(["wmctrl", "-lp"]).splitlines() if not w in ws1]
# see if possible new windows belong to our application
procs = sum([[(w[1], p) for p in get(["ps", "-u", user]).splitlines() \
if app[:15].lower() in p.lower() and w[0] in p] for w in ws2], [])
# in case of matches, move/resize the window
if len(procs) > 0:
subprocess.call(["xdotool", "windowsize", "-sync", procs[0][0] , "100%", "100%"])
break
time.sleep(0.5)
t = t+1wmctrl和xdotool:sudo apt-get安装wmctrl xdotool。resize_window.pywmctrl命令获取窗口列表的可能性很小。如果遇到问题,我们需要为整个过程添加一个try/except。如果是的话,请告诉我。https://askubuntu.com/questions/822238
复制相似问题