简体   繁体   中英

Wait for a GUI application to fully startup before running script

If I have something like this in a bash script:

audacity &
pid=$!

wmctrl -r "Audacity" -e 0,0,0,800,600

... it will usually fail, because the process startup ( audacity & ) will finish/return much earlier than the point where the Audacity window is fully shown (and can be controlled by wmctrl ), which otherwise may take a couple of seconds.

Is there an easy way to "sync" or "wait" for a GUI application to be fully started up (that is, its window being fully rendered), before proceeding with a script? ( there is a way I've found, which I'm posting as an answer - but was wandering if there is an easier, more compact way )

EDIT: this does detect when the window is shown; but does not detect when all the menus/widgets inside it are finished with placement/layout

Ok, so first I run this script:

audacity &
pid=$!

while [ "1" ] ; do
  xwininfo -name 'Audacity'
  sleep 0.1
done

... which should be run like this, to obtain a full log:

bash testscript.sh 2>&1 | tee testscript.log

... and can see a point, where the dump from xwininfo "transitions", so to speak:

xwininfo: Window id: 0x3a000b5 (has no name)

  Absolute upper-left X:  0
  Absolute upper-left Y:  0
  Relative upper-left X:  0
  Relative upper-left Y:  0
  Width: 200
  Height: 200
  Depth: 24
  Visual: 0x21
  Visual Class: TrueColor
  Border width: 0
  Class: InputOutput
  Colormap: 0x20 (installed)
  Bit Gravity State: NorthWestGravity
  Window Gravity State: NorthWestGravity
  Backing Store State: NotUseful
  Save Under State: no
  Map State: IsUnMapped
  Override Redirect State: no
  Corners:  +0+0  -824+0  -824-400  +0-400
  -geometry 200x200+0+0

xwininfo: Window id: 0x4c00587 "Audacity"

  Absolute upper-left X:  50
  Absolute upper-left Y:  59
  Relative upper-left X:  0
  Relative upper-left Y:  18
  Width: 830
  Height: 540
  Depth: 24
  Visual: 0x21
  Visual Class: TrueColor
  Border width: 0
  Class: InputOutput
  Colormap: 0x20 (installed)
  Bit Gravity State: NorthWestGravity
  Window Gravity State: NorthWestGravity
  Backing Store State: NotUseful
  Save Under State: no
  Map State: IsViewable
  Override Redirect State: no
  Corners:  +50+59  -144+59  -144-1  +50-1
  -geometry 830x540+50-1

So, I could basically grep for xwininfo output not containing "has no name", or containing "Map State: IsViewable" ...

So, I finally try this - and it seems to work:

audacity &
pid=$!

WINREP=""
while [[ ! "`echo $WINREP | grep -l 'Map State: IsViewable'`" ]] ; do
  WINREP=$(xwininfo -name 'Audacity')
  #echo $WINREP
  sleep 0.1
done

echo Exited

# must use -F here for case-insensitive, to ensure proper window targetting
wmctrl -v -F -r "Audacity" -e 0,0,0,800,600

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM