2
$\begingroup$

As in the following sreenshots, roughly speaking, Out[43] is CellObject[97601].

enter image description here enter image description here

I want to set var1 = the content of CellObject[97601]. So that

In[44] var1=SomeFunction[CellObject[97601]]
Out[44] 123456789101112

But I do not know how to get the content of CellObject[97601]. How can I construct SomeFunction?

-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Thank you, Michael E2, it works well :

enter image description here

Copy As Cell Object : CellObject[99121]

enter image description here

Similar method does not work for Text Cell. But the solution is simpler :

enter image description here

$\endgroup$

1 Answer 1

3
$\begingroup$

NotebookRead gives the cell expression (see the first example in the docs for CellObject); ToExpression can convert the boxes:

NotebookRead[cobj]
(*
Cell[BoxData[
  RowBox[{"Integrate", "[", 
    RowBox[{RowBox[{"1", "/", SqrtBox[RowBox[{"1", "-", "x"}]]}], ",",
       "x"}], "]"}]], "Input", CellChangeTimes -> {3.86329*10^9}]
*)

ToExpression[
 First@First@NotebookRead[cobj], StandardForm, HoldComplete]

(*  HoldComplete[\[Integral]1/Sqrt[1 - x] \[DifferentialD]x]  *)

The cell read was an input cell containing Integrate[1/Sqrt[1 - x], x], but ToExpression does convert boxes to InputForm.

$\endgroup$
1
  • $\begingroup$ Thank you, NotebookRead is important. $\endgroup$
    – imida k
    Jun 4, 2022 at 8:48

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.