Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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]];
  NotebookClose[tmpnb]]] 

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

Question

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.

share|improve this question
2  
Congrats with your first question by the way. I should also break the ice some day. –  Leonid Shifrin Feb 4 '12 at 21:34
    
@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). –  Heike Feb 4 '12 at 22:11
    
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. –  Leonid Shifrin Feb 4 '12 at 22:16
2  
@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. –  Heike Feb 4 '12 at 22:22
    
Yep - I can probably say the same. –  Leonid Shifrin Feb 4 '12 at 22:23

2 Answers 2

up vote 21 down vote accepted

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

First[FrontEndExecute[
  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
share|improve this answer
    
Is it possible to use this to rasterize the selection? On Windows, I tried Button["set x to selection", x = FrontEndExecute@ FrontEnd`ExportPacket[ NotebookSelection[FrontEnd`SelectedNotebook[]], "MGF"]] which returns data in a format I am not sure how to interpret. Can this be converted back to a bitmap? (This is for this palette.) Another question: when getting the selection as input text, is it possible to set the page width where it is wrapped (for this palette) –  Szabolcs Feb 6 '12 at 17:28
1  
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". This is, in fact, the gateway packet for all rasterization, but it'd take some effort for me to dig up how to convert those generic raster packets to a useful-for-you format. But you could go the long-way around by importing the PPM output (or GIF for images with fewer colors). –  John Fultz Feb 6 '12 at 18:46
1  
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. –  John Fultz Feb 6 '12 at 18:47
1  
@Szabolcs, I hadn't realized this was broken. I'd consider it a bug. But it's easy enough to work around by using the results of NotebookRead. E.g., First[FrontEndExecute[ FrontEnd`ExportPacket[BoxData@NotebookRead[nb], "PPM"]]] –  John Fultz Feb 22 '12 at 22:58
1  
@Liam Yes, I mean highlighting the bars in the right. If I do that, it works for me. If I select only a partial cell (highlight some text, but not highlight a full bar on the right), then it gives $Failed. Here's the code I tried: Button["boo", Print[FrontEndExecute[ FrontEnd`ExportPacket[NotebookSelection[InputNotebook[]], "InputText"]]]] –  Szabolcs May 28 '13 at 20:49

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 = 
    StringJoin@
        Cases[
          NotebookRead[SelectedNotebook[]] /. Cell[BoxData[data_], ___] :> data, 
          _String, 
          Infinity
        ]
 ]

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

EDIT

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[]}]},
        StringReplace[
            StringJoin@
               Cases[
                  NotebookRead[SelectedNotebook[]] /. Cell[BoxData[data_], ___] :>
                   (ToExpression[
                      (data /. "\n" :> tag),
                      StandardForm,
                      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.

share|improve this answer
2  
I may not understand the question but this turns $\sqrt{a}$ into $a$ -- is this desired? It seems dangerous. –  Mr.Wizard Feb 4 '12 at 23:10
1  
@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. –  Leonid Shifrin Feb 4 '12 at 23:39
    
@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. –  Leonid Shifrin Feb 4 '12 at 23:50
    
I am confident I still outrank you in the field of stupid mistakes. ;-) –  Mr.Wizard Feb 4 '12 at 23:53
2  
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. –  John Fultz Feb 6 '12 at 17:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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