Skip to content

Instantly share code, notes, and snippets.

@leiserfg
Created August 27, 2025 08:00
Show Gist options
  • Select an option

  • Save leiserfg/1411bee7e05221200f54f8467b65fe4b to your computer and use it in GitHub Desktop.

Select an option

Save leiserfg/1411bee7e05221200f54f8467b65fe4b to your computer and use it in GitHub Desktop.
Better than hyprland swallow
#!/usr/bin/env python3
import sys
import subprocess
import json
import os
from pathlib import Path
def ancestor_pids(pid):
"""Yield ancestor PIDs, starting from pid up to init."""
while pid > 1:
yield pid
try:
stat = Path(f"/proc/{pid}/stat").read_text()
fields = stat.split()
pid = int(fields[3]) # ppid is the 4th field
except Exception:
break
def get_hypr_clients_map() -> dict[int, dict]:
out = subprocess.check_output(["hyprctl", "clients", "-j"])
return {cl.get("pid"): cl for cl in json.loads(out)}
def main():
if len(sys.argv) < 2:
print("Usage: hawktuah <program> [args...]")
sys.exit(1)
clients = get_hypr_clients_map()
window = None
for pid in ancestor_pids(os.getpid()):
window = clients.get(pid)
if window:
break
if not window:
print("Could not find a matching Hyprland window for this process tree.")
sys.exit(1)
window_addr = window["address"]
orig_workspace = window["workspace"]["id"]
subprocess.run(
[
"hyprctl",
"dispatch",
"movetoworkspacesilent",
"special:myworkspace,address:" + window_addr,
]
)
try:
subprocess.run(sys.argv[1:])
finally:
subprocess.run(
[
"hyprctl",
"dispatch",
"movetoworkspacesilent",
f"{orig_workspace},address:{window_addr}",
]
)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment