Look at the souce code of this script to see how to do it.