Created
August 16, 2021 18:21
-
-
Save JoshMock/aa4fa469cd90b698802754934cbe4080 to your computer and use it in GitHub Desktop.
Quitter clone for Linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Linux-compatible clone of Quitter for Mac https://marco.org/apps#quitter | |
# implementation inspired by: https://github.com/acenturyandabit/mini-miza/blob/master/autohide.py | |
import subprocess | |
import time | |
import os | |
apps = { | |
"slack": 600, | |
"discord": 600, | |
"element": 600, | |
} | |
INTERVAL = 5 | |
FNULL = open(os.devnull, "w") | |
def get(cmd): | |
try: | |
return subprocess.check_output(cmd, stderr=FNULL).decode("utf-8").strip() | |
except subprocess.CalledProcessError: | |
return None | |
idle_times = {} | |
while True: | |
time.sleep(INTERVAL) | |
for app in apps.keys(): | |
windows = get(["xdotool", "search", "--all", "--classname", app]) | |
if windows: | |
windows = windows.splitlines() | |
for w in windows: | |
if w in idle_times: | |
idle_times[w] += INTERVAL | |
# if window has been unfocused for too long, quit app | |
if idle_times[w] >= apps[app]: | |
subprocess.Popen(["xdotool", "windowquit", w]) | |
idle_times.pop(w, None) | |
else: | |
idle_times[w] = 0 | |
# if window is focused, reset its timer | |
if get(["xdotool", "getactivewindow"]) in windows: | |
idle_times[get(["xdotool", "getactivewindow"])] = 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment