9
$\begingroup$

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 entire notebook screenshot

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

$\endgroup$
6

5 Answers 5

5
$\begingroup$

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`"];
        GUIKit`GUIScreenShot[{{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.

$\endgroup$
2
  • $\begingroup$ On my system this cuts off the bottom and side of the window. (v10, Windows 7) +1 nevertheless. $\endgroup$
    – Mr.Wizard
    Jul 28, 2014 at 18:12
  • $\begingroup$ 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. $\endgroup$ Jul 28, 2014 at 18:14
5
$\begingroup$

It seems there is something you need. Undocumented so who knows.

MathLink`CallFrontEnd[
 FrontEnd`NotebookImage[ EvaluationNotebook[] ]
 ]

enter image description here

$\endgroup$
6
  • $\begingroup$ I would love to know how to access those many undocumented features of MMA. Thanks a lot! $\endgroup$ Mar 24, 2015 at 12:46
  • $\begingroup$ Nice! Where did you find this? $\endgroup$
    – Silvia
    May 30, 2015 at 5:26
  • $\begingroup$ I think we can use an invisible window as a mask to record a specific area of the screen. $\endgroup$
    – Silvia
    May 30, 2015 at 5:41
  • $\begingroup$ @Silvia I'm just taking a look around in undocumented contexts :) Yes, that's what I've thought! Unfortunately it takes about 0.11 s to take a ss on my laptop so it is not fast enough to make smooth videos. It also does not capture mouse cursor. And the last thing is I had no time to implement this but I'm looking forward to your answer/topic about using this :) $\endgroup$
    – Kuba
    May 30, 2015 at 6:23
  • $\begingroup$ I've written an answer using your function, hope you don't mind :) $\endgroup$
    – Silvia
    May 30, 2015 at 6:49
2
$\begingroup$

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

enter image description here

$\endgroup$
2
  • $\begingroup$ Not enough. I need to have the entire notebook in the image. Thanks. $\endgroup$ Jul 28, 2014 at 17:24
  • $\begingroup$ @ArielSepulveda What precisely is missing in this solution? What are you looking to include that isn't? $\endgroup$
    – Szabolcs
    Jul 29, 2014 at 5:05
1
$\begingroup$

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...

enter image description here

$\endgroup$
2
  • $\begingroup$ I need to get the entire window including the WindowTitle. $\endgroup$ Jul 28, 2014 at 17:01
  • $\begingroup$ @ArielSepulveda ok, for that I use hypersnap, you can see an example at sensa.square7.ch/snap01419.jpg $\endgroup$
    – Mika Ike
    Jul 28, 2014 at 17:14
1
$\begingroup$

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 = GUIKit`GUIScreenShot[{{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: enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.