Let's say we have a cell:
(*newline*)
(*newline*)
Plot[
x,
{x, 0, 1}
]
(*newline*)
Plot[x, {x, 0, 2}]
How to read it expression by expression?
So e.g to read Plots
one by one, in whatever form, Boxes
, Hold[expression]
, String
?
Here's my attempt:
SelectionMove[PreviousCell[] (*cellobject in general*), All, Cell];
text = First[ FrontEndExecute[
FrontEnd`ExportPacket[NotebookSelection[], "InputText"]]
]; (*1*)
Module[{stream = StringToStream[text], temp},
temp = ReadList[stream, Hold[Expression]];
Close[stream];
temp
]
{Hold[Plot[x, {x, 0, 1}]], Hold[Plot[x, {x, 0, 2}]]}
Quite long, and problematic because it changes selection.
I think there should be better way since this is what FrontEnd is doing anyway, reading expression by expression:
{
1, Abort[]
}
2
$Aborted
2
(*1*)
- How do I extract the contents of a selected cell as plain text?
Comments
Post a Comment