Skip to content

Instantly share code, notes, and snippets.

@jdstapleton
Last active August 20, 2020 19:26
Show Gist options
  • Save jdstapleton/f7e252eb2a64ce0dd1a57eec075cee3b to your computer and use it in GitHub Desktop.
Save jdstapleton/f7e252eb2a64ce0dd1a57eec075cee3b to your computer and use it in GitHub Desktop.
This is a script to mimimize or activate the gnome terminal that can be used as part of a global hotkey in the gnome keyboard shortcut preferences
#!/usr/bin/env python3
# Source: https://gist.github.com/jdstapleton/f7e252eb2a64ce0dd1a57eec075cee3b
# Installation:
# - copy this script somewhere on your machine, and set its executable bit
# - install `xdotool`, and `wmctrl` using your favorite package manager
# - Go to the Keyboard Shortcuts control panel
# - Scroll to the bottom to the 'Custom Shortcuts' section, click the `+` button
# - `Name` to be 'Terminal' or something similar
# - `Command` to be the path to the script where you copied it to.
# - `Shortcut` assign something that you like `ctrl-shift-~` or `F12` or something.
# Test it out by pressing the shortcut a few times to hide/show the terminal window.
import subprocess
import os
script_dir = os.path.dirname(os.path.realpath(__file__))
result = subprocess.run(['xdotool', 'getwindowfocus'], stdout=subprocess.PIPE)
focusWindowId = int(result.stdout.decode('utf-8').rstrip('\n'))
terminalWindowCommand = ['search', '--onlyvisible', '--classname', 'gnome-terminal']
result = subprocess.Popen(['xdotool', *terminalWindowCommand],
stdout=subprocess.PIPE)
gnomeTerminals = set([int(x.decode('utf-8').rstrip('\n')) for x in iter(result.stdout.readlines())])
# finally test to see if the terminal is activated, if not show it and activate, otherwise minimize it
if focusWindowId not in gnomeTerminals:
for winId in gnomeTerminals:
subprocess.run(['wmctrl', '-i', '-r', str(winId), '-b', 'add,above'])
subprocess.run(['xdotool',
*terminalWindowCommand,
'windowraise',
'windowactivate'
])
else:
for winId in gnomeTerminals:
subprocess.run(['wmctrl', '-i', '-r', str(winId), '-b', 'remove,above'])
subprocess.run(['xdotool',
*terminalWindowCommand,
'windowminimize'])
@jdstapleton
Copy link
Author

sudo apt install xdotool wmctrl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment