#!/bin/sh # # xwin_find [-v|-q] [timeout] window_name_regex # # Look for the given window (using an awk regular expression), and print the # windows xwindow ID. If a timeout is given (in seconds)continue to look for # the windows ID for this amount of time before returning. # # If no such window is found output nothing, just exit with an error status. # # OPTIONS # -v verbose, print the full matching xwininfo line on stderr # -q do not print windows ID on stdout # # The script is typically used to wait for a specific application window to # appear. The returned 'id' can then be used to modifiy the application # using programs such as "xprop", "xwit", "wmctrl" and "xdotool". # # For example... # # # Launch a firefox application # ( firefox & ) & # # # wait for window to appear. # if id=`xwin_find 60 ".* Mozilla Firefox"`; then # echo "Main firefox window found (id=$id)" # # set the windows size and position and finally iconify it # xwit -resize 820 1000 -move 530 70 -iconify -id $id # else # echo >&2 "Timeout waiting for firefox window to appear. # fi # #### # # Anthony Thyssen September 2005 # PROGNAME=`type $0 | awk '{print $3}'` # search for executable on path PROGDIR=`dirname $PROGNAME` # extract directory of program PROGNAME=`basename $PROGNAME` # base name of program Usage() { echo >&2 "$PROGNAME:" "$@" sed >&2 -n '/^###/q; s/^#$/# /; 3s/^#/# Usage:/; 3,$s/^# //p;' \ "$PROGDIR/$PROGNAME" exit 10; } timeout=0 # just do one search and exit indent= while [ $# -gt 0 ]; do case "$1" in [0-9]*) timeout=`date +%s` timeout=`expr $timeout + $1 + 1` || Usage ;; -q) QUIET=true ;; # don't print the final window ID, just status -v) VERBOSE=true ;; # output the full xwininfo line on stderr # -d) shift; indent=`expr 2 + "$1" \* 3` ;; # indent in xwininfo output --) shift; break ;; # end of user options -*) Usage "Unknown option \"$1\"" ;; *) break ;; # end of user options esac shift # next option done [ $# -lt 1 ] && Usage "Missing window search regex" [ $# -gt 1 ] && Usage "Too many arguments." search="$1" # -------Subroutines ----- find_win() { # nice added to let it give way to starting processes #if [ "$indent" ]; then # # Look for window of specific depth # line=`nice xwininfo -root -tree |\ # awk '/"'"$search"'":/ && /^ {'"$indent"'}[^ ]/ {print; exit}'` #else line=`nice xwininfo -root -tree | awk '/"'"$search"'":/ {print; exit}'` #fi [ "$VERBOSE" ] && echo >&2 "$line" echo "$line" | sed 's/^ *//;s/ .*//' } # ------ Main Loop ----- while :; do id=`find_win "$search"` if [ "$id" ]; then # was the window ID found [ -z "$QUIET" ] && echo $id # the window ID found exit 0; # return success fi [ `date +%s` -ge $timeout ] && break usleep 100 done exit 1 # window was not found -- return failure