Capture Screen Shot of all spaces on macOS

I have code which captures a screen shot of all displays, but what I really want is to capture screen shots of all spaces. Can someone point me at how i do this? Thanks!
Capture Screen Shot of all spaces on macOS
 
 
Q