Last active
October 26, 2023 15:09
-
-
Save tgran2028/169bce96217c401214c7960d67472162 to your computer and use it in GitHub Desktop.
Script that activates a window by name so it can be configured to a global hotkey. If the window is not open, then it will launch the app/program. Requires linux and genome.
This file contains 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
#!/bin/bash | |
WINDOW_NAME='Hyper' | |
# Use "wmctrl -l" to get list of all window names if needed | |
# when setting the target window name. May need to install wmctrl. | |
# | |
# Example output: | |
# 0x00e00003 0 dell-XPS Hyper | |
# 0x03600003 0 dell-XPS Script that activates a window by name so it can be configured to a global hotkey. If the window is not open, then it will launch the app/program. Requires linux and genome. and 9 more pages - Personal - Microsoft Edge Dev | |
# 0x03600083 0 dell-XPS Flathub - RedisInsight | Flathub | |
# 0x0360009a 0 dell-XPS GNOME Shell Extensions - Personal - Microsoft Edge Dev | |
# 0x06600060 0 dell-XPS Settings | |
# 0x06800003 0 dell-XPS Untitled-1 - PyGen - Visual Studio Code - Insiders | |
# 0x06800015 0 dell-XPS ● #!/bin/bash • Untitled-1 - Visual Studio Code - Insiders | |
# This function activates a window with the given name if it exists, otherwise it launches the program. | |
# If xdotool is not installed, it prompts the user to install it and exits if the user chooses not to install it. | |
# Parameters: | |
# $1: The name of the window or program to activate | |
activate_window() { | |
# Check if the number of arguments is 1. If not, print error message and exit. | |
if [ "$#" -ne 1 ]; then | |
echo "Illegal number of parameters. Please provide a single argument with the name of the window or program to activate." | |
exit 1 | |
fi | |
local window_name="$1" | |
# Check if xdotool is installed. If not, prompt user if they want to install it with yes/no. If no, exit script. | |
if ! command -v xdotool &>/dev/null; then | |
echo "xdotool could not be found" | |
read -p "Would you like to install it? (y/n) " -n 1 -r | |
echo | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
sudo apt update && sudo apt install xdotool | |
else | |
exit 1 | |
fi | |
fi | |
# Get the window ID of the last active window with the given name | |
window_id=$(xdotool search --name "$window_name" | tail -1) | |
# If the window ID is not empty, activate the window | |
if [ -n "$window_id" ]; then | |
xdotool windowactivate $window_id | |
else | |
# If the window ID is empty, launch the program | |
$1 & | |
fi | |
} | |
activate_window $WINDOW_NAME |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment