# How to create a screen shot of a notebook

I would like to use Mathematica programmatically to take snapshots or screenshots of any open notebook. For example, I would like to get from the notebook created with this command

CreateDocument[{TextCell["Need help", "Section"],
TextCell["Can you help?"]}, WindowTitle -> "MyNotebook"]


An image like this

The output can be a file or an image. I use Windows.

-
Have you looked at: reference.wolfram.com/language/GUIKit/ref/GUIScreenShot.html? –  user85503 Jul 28 '14 at 16:47
Yes, but that does not provide the solution I need. –  Ariel Sepulveda Jul 28 '14 at 16:51
–  rm -rf Jul 28 '14 at 16:57

This answer, which is a slight modification of my answer to a previous question, takes you most of the way there:

Clear@notebookScreenshot
notebookScreenshot[nb_] :=
Module[{left, top, size, opts = AbsoluteOptions@nb},
{left, top} = WindowMargins /. opts // Diagonal;
size = WindowSize /. opts;

Needs["GUIKit"];
GUIKitGUIScreenShot[{{left, left + size[[1]]}, {top, top + size[[2]]}}]
]

notebookScreenshot@EvaluationNotebook[]


It works by finding the explicit pixel locations of the notebook and passing it to GUIScreenShot. You can pass any notebook object to the function and it will work.

The problem here (and I think this is only on OS X) is that the height of the menubar is not reflected in the WindowMargins. I suppose one could get away with offsetting it by 20 pixels (or whatever the right amount is), but I wonder if there's a neater way to get that information.

-
On my system this cuts off the bottom and side of the window. (v10, Windows 7) +1 nevertheless. –  Mr.Wizard Jul 28 '14 at 18:12
This is close to what I need. Other than the offset needed (in OS X and Windows also) there might be another issue with this solution. I think that GUIScreenShot pictures what is visible on the screen and that may not be good if the target window takes all the screen. In this case it would be preferable to create an image of the Notebook object instead of a screenshot of what is displaying in the monitor which will most probably contain the "unwanted-in-screenshot" button used to create the screenshot. –  Ariel Sepulveda Jul 28 '14 at 18:14

This isn't exactly a screenshot but maybe it meets your needs?

doc = CreateDocument[{
TextCell["Need help", "Section"],
Cell[1/0],
TextCell["Can you help?"]}, WindowTitle -> "MyNotebook"];
Rasterize@doc


-
Not enough. I need to have the entire notebook in the image. Thanks. –  Ariel Sepulveda Jul 28 '14 at 17:24
@ArielSepulveda What precisely is missing in this solution? What are you looking to include that isn't? –  Szabolcs Jul 29 '14 at 5:05

One solution is use a snapshot software. I like very much hypersnap , you can test in http://www.hyperionics.com/ with wich you can scroll down an obtain any similar to this

http://sensa.square7.ch/snap01417.jpg

The other option, with Mathematica, to obtain the Out line is...

-
I need to get the entire window including the WindowTitle. –  Ariel Sepulveda Jul 28 '14 at 17:01
@ArielSepulveda ok, for that I use hypersnap, you can see an example at sensa.square7.ch/snap01419.jpg –  Mika Ike Jul 28 '14 at 17:14

OK, based on the feedback provided I have created a solution to the problem. For the benefit of the community I'm posting this solution as an answer to my own question as your feedback, although very useful, missed some key elements.

Note that the variable mySizeAdjustments will most probably need to be adjusted by users depending on the operating system used and other system related parameters. Here is the code:

notebookScreenshot[nb_] := Block[
{left, top, size, opts, mySizeAdjustments = {18, 75}, windowMargins,
screenShot, buttonNb = SelectedNotebook[]}
,
Needs["GUIKit"];
opts = AbsoluteOptions@nb;
windowMargins = Replace[WindowMargins, opts];
SetSelectedNotebook@nb;
If[ListQ@windowMargins,
{left, top} = Diagonal[windowMargins]
,
{left, top} = {0, 0}
];
size = mySizeAdjustments + Replace[WindowSize, opts];
screenShot = GUIKitGUIScreenShot[{{left, left + size[[1]]}, {top, top + size[[2]]}}];
SetSelectedNotebook@buttonNb;
screenShot
];


Note that this solution can take screenshots of notebooks other than the ButtonNotebook or SelectedNotebook from where the command is run. To test the code you can use the following lines:

targetNb = CreateDocument[{TextCell["Need help", "Section"], TextCell["Can you help?"]}, WindowTitle -> "MyNotebook"];
notebookScreenshot@targetNb


In my case the output that the former lines produce is shown below:

-