How can I use a macro to close LibreOffice on document close?

Hello all,

Im using L.O 4.2.42, Ubuntu 13.10, python.

I need to code a macro that when the current document is closed, it gets executed (On document close event I guess) and closes the entire Libre Office gracefully.

I try to do it with the next macro, XDesktop.terminate() method, but most of the times Libre Office freezes.
I might be missing sth…

import uno

def closeLO(*args):
              desktop = XSCRIPTCONTENT.getDesktop()
              desktop.terminate()
              return none

g_exportedScripts = closeLO,

Is there any elegant way to close LO gracefully using a Macro without freezing it?

Thanks in advance,

Jorge.