From: Justin Gullingsrud (justin_at_ks.uiuc.edu)
Date: Fri Nov 09 2001 - 11:14:28 CST

Hello,

I'm posting this script because many people have asked how to make VMD wait
for a file to load before executing their analysis script. This will
hopefully solve that problem once and for all:

# waitfor - Wait for VMD to load a file before executing a script.
# Justin Gullingsrud
# 11-9-2001

# Usage: Before loading your files, call waitfor with the name of the last
# file you will load and the name of a proc to execute when it does.

# Example: I want to load traj1.dcd and traj2.dcd, then execute a script named
# myproc. The structure file for this system is struc.psf

# vmd > source waitfor.tcl
# vmd > waitfor traj2.dcd myproc
# vmd > mol load psf struc.psf dcd traj1.dcd
# vmd > animate read dcd traj2.dcd

proc waitfor_callback { name1 name2 op } {
  global waitfor_file
  global waitfor_proc

  upvar $name1 arr
  set fname $arr($name2)
  if { ! [string match $fname $waitfor_file ] } { return }

  eval uplevel #0 $waitfor_proc
  after idle uplevel #0 trace vdelete vmd_trajectory_read w waitfor_callback
}
   
proc waitfor { file script } {
  global waitfor_file
  global waitfor_proc

  set waitfor_file $file
  set waitfor_proc $script

  uplevel #0 trace variable vmd_trajectory_read w waitfor_callback
}

-- 
Justin Gullingsrud      3111 Beckman Institute
H: (217) 384-4220       I got a million ideas that I ain't even rocked yet...
W: (217) 244-8946       -- Mike D