The code to use is
code:
var myVar = Messenger.CurrentChats;
If you are just looking for a script to do it rather than learning the language, you could just use my script, PSM+, which can do this (amongst many other things). The link should be in my signature
EDIT:
Beaten to it!