Funny, i created a small plugin to do this once, maybe you can read it from that. (Attached) License is GPLv3 Yes, it is a script plugin