Take desktop screenshots with electron


is it possible to take desktop screenshots with electron?


I’d also be interested if that’s possible. Potentially it’s possible to “shell out” from electron and call something like screencapture on OS X and similar utilities on other platforms?


Here is an easy way to do it on mac.

(1) Download the “shelljs” npm module so that you can run shell command as if you were in the terminal using the .exec() method.
(2) Then just run normal mac commands for taking screenshots. [Maclife][1] had a decent intro to the command for taking screenshots on a mac. But you would instead run those command using the “shelljs” module.


var shell = require("shelljs");  

then do

shell.exec("screencapture electron_pic.jpg", function(){});

Its really that easy.

PS. The screenshots were being saved in the root of my app instead of the desktop. You can copy it wherever after you take a pic.

EDIT: I didn’t see that @martinklepsch had mentioned to use the screencapture command. He was on the right track.
[1]: http://www.maclife.com/article/columns/terminal_101_taking_screenshots_through_command_line


If shelljs allows the redirect of stdout and screencapture allows outputting the image to stdout, you can even omit the intermediate file.


screencapture allows storing the screenshot in the clipboard. Not sure if that helps.


Ooohh… i didn’t know screencapture can output to stdout. That makes it a lot easier!


I’m not saying you can, I was just pointing out a theoretical possibility… Never used screencapture


Oh, gotcha. I just looked at the man page for screencapture and it says: screencapture -- capture images from the screen and save them to a file or the clipboard.

So it looks like you cannot create a file stream or anything directly from screencapture. But if you can declare the path right when taking a screencapture, I guess you wouldn’t really need to pipe the image at all, its just an extra step (unless you wanted to resize or process the image).


You could save it to a temp file and process it all you like before storing it where it should ultimately go.


Maybe -c to capture to the clipboard, followed by pbpaste?