My aim is to extract the contents of an Input cells as plain text. Ideally I would like to be able to select one or more Input cells and press a button which would then extract the text from these cells as a string and assign it to a variable. However, this turns out to be harder than it seems (to me at least).

One way which was suggested in a chat conversation was to copy the cell as plain text by doing something like this

Button["Set x to selection",
  FrontEndTokenExecute[SelectedNotebook[], "CopySpecial", "PlainText"];
  x = (NotebookGet@ClipboardNotebook[])[[1, 1, 1]]

This seems to work; for example, if I type this into an input cell, select the cell, and press the button

a + b == c;
d = "x";

then x is set to the string "a+b==c;\nd=\"x\";" which is what I want. The only downside of this method as far as I can tell is that it destroys the clipboard.

To preserve the clipboard I tried using NotebookRead instead to copy the contents but I only got that to work if I changed the display form of the selected cell to "TextForm" first, e.g.

Button["Set x to selection 2",
 Module[{tmpnb, nb},
  nb = SelectedNotebook[];
  tmpnb = CreateDocument[WindowSelected -> False];
  NotebookWrite[tmpnb, NotebookRead[nb], All]; 
  FrontEndTokenExecute[tmpnb, "SelectionDisplayAs", "TextForm"];
  SelectionMove[tmpnb, All, CellContents];
  x = ToString[NotebookRead[tmpnb]];

Of course the downside of this approach is that it creates a temporary notebook every time I press the button.


Both methods seem to do their job, but they both rely on the FrontEnd manipulating the contents of the selected cell before being able to extract the string. This made me wonder if there is an easier way to get the same result.


String, Text, Cell, Export, ExportString, ToString, InputForm

  • 3
    $\begingroup$ Congrats with your first question by the way. I should also break the ice some day. $\endgroup$ Commented Feb 4, 2012 at 21:34
  • $\begingroup$ @LeonidShifrin Thanks. At least you have a good excuse for not having posted a question yet. You already know everything about Mathematica (or so it seems to me at least). $\endgroup$
    – Heike
    Commented Feb 4, 2012 at 22:11
  • 1
    $\begingroup$ Thanks, but I think I know a very tiny fraction of things there are to know about it. Actually, one thing which is quite amazing (to me anyways) about Mathematica is how much new stuff you can learn even after years of experience. I learn a lot here on SO / SE, which is a big motivation for me to hang around. $\endgroup$ Commented Feb 4, 2012 at 22:16
  • 2
    $\begingroup$ @LeonidShifrin I agree. I've learned more about Mathematica in the 7 months I've been on SO and now here on SE than in all the years before that. $\endgroup$
    – Heike
    Commented Feb 4, 2012 at 22:22
  • $\begingroup$ Yep - I can probably say the same. $\endgroup$ Commented Feb 4, 2012 at 22:23

2 Answers 2


Assuming nb is your notebook object, then this will do what you want without touching the clipboard:

  FrontEnd`ExportPacket[NotebookSelection[nb], "InputText"]]]

Some notes about this solution:

  • It preserves evaluation semantics precisely, regardless of typesetting.
  • It does not dirty the clipboard
  • If you prefer to get the appearance as opposed to the evaluation semantics, you can use "PlainText" (for example, grids copy as tabular looking things as opposed to as lists)
  • I tested this in 8.0.1, but it might not work in earlier versions

This FE packet only supports a limited number of formats. The public formats include "GIF", "PPM", "EnhancedMetafile" (Windows), "PICT" (Mac) , "PostScript", "RTF", "PDF", and "SVG".

I should say that the first argument of ExportPacket can also be any Notebook, Cell, or Box expression. Also, a NotebookObject, in which case it'd convert the entire notebook rather than just the selection.

When the selection does not contain a full cell it is enough to work around by using the results of NotebookRead. E.g.:

First[FrontEndExecute[ FrontEnd`ExportPacket[BoxData @ NotebookRead[nb], "PPM"]]]
  • 2
    $\begingroup$ @JohnFultz it seems that it converts \n to \r\n while using context menu does not do this. Any tips how to prevent it? e.g.: InputForm@ First[FrontEndExecute[ FrontEnd`ExportPacket[ BoxData@RowBox[{"{", " ", RowBox[{"a", "\n", ",", " ", "b"}], "\n", "}"}], "PlainText"]]] $\endgroup$
    – Kuba
    Commented May 25, 2018 at 7:46

NOTE: This answer is provided for illustrative purposes only, since it shows some techniques of working with boxed data. While it illustrates how one could emulate the correct behavior in some cases, this code should NOT be used in practice (as a solution for this particular problem), because doing so may be both fragile and dangerous. Please read the discussion comments below the answer to get a more complete picture.

This is what usually works for me:

Button["Set x to selection", 
   x = 
          NotebookRead[SelectedNotebook[]] /. Cell[BoxData[data_], ___] :> data, 

In particular, I used similar code in my syntax highlighter generator, so I've tested that this works, on many examples (although, the code of the generator is perhaps not the best place to consult, if one wants to stay sane).


Here is a code which is supposed to work also for non-text boxes (such as SqrtBox), but it is pretty ugly and may also be fragile, to the point that the clipboard solution seems much better.

Button["Set x to selection",
  x = 
      With[{tag = StringJoin[ ToString /@ {Unique["tag"], Unique[]}]},
                  NotebookRead[SelectedNotebook[]] /. Cell[BoxData[data_], ___] :>
                      (data /. "\n" :> tag),
                      Function[dt, MakeBoxes[InputForm[dt]], HoldAll]
                    ] /. InterpretationBox[StyleBox[code_String, ___], ___] :> code
                  _String, {0, Infinity}
            tag :> "\n"

For cases when you only have code, you should be able to use the first version though.

  • 2
    $\begingroup$ I may not understand the question but this turns $\sqrt{a}$ into $a$ -- is this desired? It seems dangerous. $\endgroup$
    – Mr.Wizard
    Commented Feb 4, 2012 at 23:10
  • 1
    $\begingroup$ @Spartacus Yes, you are right - I was mostly using this for code cells, so did not run into this problem. I know how to avoid this, but it include ToExpression - MakeBoxes cycle ( along these lines: ToExpression[data, StandardForm, Function[dt, MakeBoxes[InputForm[dt]], HoldAll]]), and I lose the new lines (they get converted to Null-s). This is surely surmountable, but I've got to go now. Will correct tomorrow, if no one does that first. $\endgroup$ Commented Feb 4, 2012 at 23:39
  • $\begingroup$ @Spartacus I should watch myself - the number of typos I make increased dramatically in the recent days, some of which are very stupid and not something I'd normally make. $\endgroup$ Commented Feb 4, 2012 at 23:50
  • 1
    $\begingroup$ I am confident I still outrank you in the field of stupid mistakes. ;-) $\endgroup$
    – Mr.Wizard
    Commented Feb 4, 2012 at 23:53
  • 5
    $\begingroup$ The clipboard code is very careful to deal with all kinds of special cases like TagBox, InterpretationBox, all of the various script boxes, etc. I applaud your inventiveness, but I find this shotgun approach very scary, and would never personally recommend its use. The answer I separately posted uses the same mechanism used by the clipboard. $\endgroup$
    – John Fultz
    Commented Feb 6, 2012 at 17:05

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.