Title says it all.
I have often thought this would be handy. It's an easy command in windows anyway. I'm not sure about Mac, but given it's Unix foundation should be just as simple.
Now about the coding? I know nothing trivial is ever trivial.
[Y]
Add to this option: prevent sleep and/or shutdown while indexing