Treat MacBook screen as monitor even when closed?
I'm not sure that this is quite the solution that you're looking for, but the utility Stay does make sure that windows return to a set position when connecting an external display. I haven't had a chance to use it myself, but since purchasing a new external display, I must say it's tempted me. 

You might also check out this related thread on .SX, which has more suggestions.

A quick search pulled up this article, which might also solve your question by disabling the sleep signal sent when you close the lid… Someone with stronger terminal-fu than myself might know some direct CLI commands to do this directly.