Thanks for your reply, pipitas. I should maybe have mentioned that I do have a lot of experience programming and scripting, just not with IM...
However, trying to run your code from a .bat file produces this error message:
i was unexpected at this time.