This is a generalization of this question, which already has very nice answers.
Suppose str is a string of valid code, so that ToExpression[str] generates an output. I can copy/paste the contents of str into a cell and then evaluate this cell manually.
But I'd like to automate the process of generating an input cell containing the contents of str. That is, I'm looking for a function makeCell[str_] that generates an input cell with the contents of str. This seems doable because it can be done by copy/pasting, and functions like ToExpression and CellPrint exist (see above post and this one for specific examples). But so far I haven't found a solution.
Is this possible?
CellPrint[ Cell[BoxData@"Module[{x}, x = 1]", "Input", InitializationCell -> True]]. I can't find a way to do this usingNotebookWrite, and some people in the linked post warn against usingCellPrintfor various reasons, but it works fine for me. – WillG Jul 21 '22 at 19:38Celland notCellPrint. Thanks! – WillG Jul 21 '22 at 19:48;). – Kvothe Mar 06 '23 at 13:32"Module[{x}, x = 1];\n%+1"this works for me on Win. – Kuba Mar 06 '23 at 14:58