Skip to content

Instantly share code, notes, and snippets.

@74th
Forked from zidizei/toggle-terminal.sh
Created May 16, 2021 02:42
Show Gist options
  • Save 74th/4c988b840fba4b52ec24f137e46ccd3e to your computer and use it in GitHub Desktop.
Save 74th/4c988b840fba4b52ec24f137e46ccd3e to your computer and use it in GitHub Desktop.
Toggle your GNOME Terminal window with a keyboard shortcut under Linux.
#!/bin/python
import time
import subprocess
TERMINAL_APP = "gnome-terminal"
def run(command: list[str]) -> list[str]:
return subprocess.run(command, capture_output=True, encoding="ascii").stdout.strip().splitlines()
def toggle_terminal():
output = run(["xdotool", "search" ,"--all", "--onlyvisible", "--classname", TERMINAL_APP])
if not output:
# no gnome-terminal
subprocess.run([TERMINAL_APP])
return
terminal_window = output[0]
output = run(["xdotool", "getactivewindow"])
active_window = ""
if output:
active_window = output[0]
if active_window == terminal_window:
run(["xdotool", "windowminimize", terminal_window])
return
active_desktop = run(["xdotool", "get_desktop"])[0]
subprocess.run(["xdotool", "set_desktop_for_window", terminal_window, active_desktop])
for _ in range(20):
terminal_desktop = run(["xdotool", "get_desktop_for_window", terminal_window])[0]
if terminal_desktop == active_desktop:
break
time.sleep(0.1)
subprocess.run(["xdotool", "windowraise", terminal_window])
subprocess.run(["xdotool", "windowactivate", terminal_window])
toggle_terminal()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment