在没有焦点的情况下在 X 时间后自动最小化程序?

L42*_*L42 6 scripts window-manager automation focus minimize

有没有办法在一段时间内没有焦点后自动最小化程序?

Jac*_*ijm 8

完美运行,和你描述的完全一样。

1. 在没有焦点的情况下在 x 时间后最小化窗口的脚本

下面的后台脚本将在没有焦点的任意时间后最小化窗口。

剧本

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

def getwindowlist():
    # get windowlist
    try:
        return [
            l.split()[0] for l in \
            subprocess.check_output(["wmctrl", "-l"]).decode("utf-8")\
            .splitlines()
            ]
    except subprocess.CalledProcessError:
        pass

def getactive():
    # get active window, convert to hex for compatibility with wmctrl
    wid = str(hex(int(
        subprocess.check_output(["xdotool", "getactivewindow"])\
        .decode("utf-8"))))
    return wid[:2]+str((10-len(wid))*"0")+wid[2:]

# round down on 2 seconds (match needs to be exact)
minitime = (int(sys.argv[1])/2)*2

wlist1 = []
timerlist = []

while True:
    time.sleep(2)
    wlist2 = getwindowlist()
    if wlist2:
        # clean up previous windowlist; remove non- existent windows
        try:
            timerlist = [
                wcount for wcount in timerlist if wcount[0] in wlist2
                ]
        except IndexError:
            pass
        for w in wlist2:
            # add new windows, zero record
            if not w in wlist1:
                timerlist.append([w, 0])
        # add two to account(s)
        for item in timerlist:
            item[1] += 2
        active = getactive()
        for w in timerlist:
            # minimize windows that reach the threshold
            if w[1] == minitime:
                subprocess.Popen(["xdotool", "windowminimize", w[0]])
            # set acoount of active window to zero
            w[1] = 0 if w[0] == active else w[1]
        wlist1 = wlist2
Run Code Online (Sandbox Code Playgroud)

如何使用

  1. 该脚本需要wmctrlxdotool

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

  3. 以所需的时间(以秒为单位(最小化之前))测试运行它,作为参数,例如:

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

    ...在 5 分钟无焦点后最小化窗口

  4. 如果一切正常,请将其添加到启动应用程序:Dash > Startup Applications > Add。添加命令:

    /bin/bash -c "sleep 15 && python3 /path/to/minimize_timer.py 300"
    
    Run Code Online (Sandbox Code Playgroud)

笔记

  • 运行脚本时,我没有注意到处理器有任何额外的负担。
  • 脚本在两秒内“舍入”时间。如果一个窗口有焦点,例如只有 0.5 秒,它可能不会被注意到为“聚焦”。

解释

  • 该脚本记录所有打开的窗口。除非窗口具有焦点,否则脚本每两秒向窗口的“帐户”添加两秒。
  • 如果窗口有焦点,它的帐户被设置为 0
  • 如果帐户达到某个阈值,在参数中设置,窗口将最小化xdotool's windowminimize

如果一个窗口不再存在,它将从记录列表中删除。


2. 应用特定版本

下面的版本将在 x 秒后最小化任意应用程序的所有窗口。

剧本

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

如何使用

  1. 脚本需要xdotool

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

  3. 在头部部分,将应用程序设置为最小化
  4. 以所需的时间(以秒为单位(最小化之前))测试运行它,作为参数,例如:

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

    ...在 5 分钟无焦点后最小化窗口

  5. 如果一切正常,请将其添加到启动应用程序:Dash > Startup Applications > Add。添加命令:

    /bin/bash -c "sleep 15 && python3 /path/to/minimize_timer.py 300"
    
    Run Code Online (Sandbox Code Playgroud)