1 | #!/bin/sh
|
2 | #
|
3 | # xwin_find [-v|-q] [timeout] window_name_regex
|
4 | #
|
5 | # Look for a window of the windows full name given by a awk regular
|
6 | # expression, and print the windows xwindow ID.
|
7 | #
|
8 | # If a timeout is given (in seconds)continue to look for the windows ID
|
9 | # for this amount of time before returning. (EG default a single search)
|
10 | #
|
11 | # If no such window is found output nothing, just exit
|
12 | #
|
13 | # OPTIONS
|
14 | # -v verbose, print the full matching xwininfo line on stderr
|
15 | # -q do not print windows ID on stdout
|
16 | #
|
17 | ####
|
18 | # Anthony Thyssen September 2005
|
19 | #
|
20 | PROGNAME=`type $0 | awk '{print $3}'` # search for executable on path
|
21 | PROGDIR=`dirname $PROGNAME` # extract directory of program
|
22 | PROGNAME=`basename $PROGNAME` # base name of program
|
23 | Usage() {
|
24 | echo >&2 "$PROGNAME:" "$@"
|
25 | sed >&2 -n '/^###/q; s/^#$/# /; 3s/^#/# Usage:/; 3,$s/^# //p;' \
|
26 | "$PROGDIR/$PROGNAME"
|
27 | exit 10;
|
28 | }
|
29 |
|
30 | timeout=0
|
31 |
|
32 | while [ $# -gt 0 ]; do
|
33 | case "$1" in
|
34 | [0-9]*) timeout=`date +%s`
|
35 | timeout=`expr $timeout + $1 + 1` || Usage
|
36 | ;;
|
37 | -q) QUIET=true ;; # don't print the final window ID, just status
|
38 | -v) VERBOSE=true ;; # output the full xwininfo line on stderr
|
39 |
|
40 | --) shift; break ;; # end of user options
|
41 | -*) Usage "Unknown option \"$1\"" ;;
|
42 | *) break ;; # end of user options
|
43 | esac
|
44 | shift # next option
|
45 | done
|
46 |
|
47 | [ $# -lt 1 ] && Usage "Missing window search regex"
|
48 | [ $# -gt 1 ] && Usage "Too many arguments."
|
49 |
|
50 |
|
51 | find_win() {
|
52 | # nice added to let it give way to starting processes
|
53 | if [ "$VERBOSE" ]; then
|
54 | line=`nice xwininfo -root -tree | awk '/"'"$1"'":/ {print; exit}'`
|
55 | echo >&2 $line # VERBOSE - xwininfo output
|
56 | echo "$line" | sed 's/ .*//'
|
57 | else
|
58 | nice xwininfo -root -tree | awk '/"'"$1"'":/ {print $1; exit}'
|
59 | fi
|
60 | }
|
61 |
|
62 | while :; do
|
63 | id=`find_win "$1"`
|
64 | if [ "$id" ]; then
|
65 | [ -z "$QUIET" ] && echo $id # the window ID found
|
66 | exit 0;
|
67 | fi
|
68 | [ `date +%s` -ge $timeout ] && break
|
69 | done
|
70 |
|
71 | exit 1 # window was not found
|
72 |
|