Unfortunately the method that you already have the code for is (to my knowledge) the only way of doing this. I was looking for something similar a long time ago, and ended up using a code like the one you've already supplied.