You can do this in Python now with :
https://github.com/tcsenpai/pybooklid
here is an example that plays a sound via Pygame
https://github.com/Petess/MacLidPythonSound
This avoids having to deal with Xcode.