Created
July 26, 2025 12:13
-
-
Save metaphore/1f6e9a02e8979b4c45c20e5dd462c47d to your computer and use it in GitHub Desktop.
Find a window corresponding to the process containing a substring in its name
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
| #!/bin/bash | |
| # Check for argument | |
| if [ -z "$1" ]; then | |
| echo "Usage: $0 <process-name-substring>" >&2 | |
| exit 1 | |
| fi | |
| search_term="$1" | |
| # Use wmctrl to list windows | |
| wmctrl -lp | while read -r win_id desktop pid host title; do | |
| # Make sure the process exists and is readable | |
| if [ -r "/proc/$pid/cmdline" ]; then | |
| # Read the cmdline and make case-insensitive match | |
| cmdline=$(tr '\0' ' ' < "/proc/$pid/cmdline") | |
| if echo "$cmdline" | grep -iq "$search_term"; then | |
| echo "$win_id" | |
| exit 0 | |
| fi | |
| fi | |
| done | |
| # If no match was found | |
| exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment