I just want to know if this can be done in one command without any interactions in macOS. There's an interactive way to do this (I first switched to the user abc and then switched back so that abc had a hidden desktop):
(in sudo mode) login abc (type the password) screencapture <png-file-name>
In my test, this would take a screenshot of abc's hidden desktop and save it to the file. But the weird thing is that when I use
-f option (only applicable in sudo mode) to eliminate the need of typing password, the screenshot becomes the current user's desktop, not abc's desktop:
(in sudo mode) login -f abc /usr/sbin/screencapture <png-file-name> OR login -f abc bash -c "/usr/sbin/screencapture <png-file-name>"
Both don't work as expected.
sudo also can't meet my requirements, because they also take the screenshots of current user's desktop.
You may wonder why I want that. In fact I don't want the screenshots, I want security. I want to make the shell of this another user completely isolated from the current user, so that any malicious code running in this shell will not be able to take screenshot of the current user.