Write a generated image to another notebook?

I’m learning the NotebookWrite[] interface and have hit a wall on inserting images.

What I’ve done is a script that generates a map that I then want to insert the image that results from the code into the daily notebook, but everything I’ve tried thus far instead inserts the raw code, not the map image.

nb = CreateDocument;
(NotebookWrite[nb, Cell[“New content”, “Subsection”]];)
ExampleData[{“TestImage”, “JellyBeans”}] //
NotebookWrite[nb,
Cell[BoxData[MakeBoxes[#, StandardForm]], “Output”]] &

You have to convert the expression you want to display to boxes. MakeBoxes[] is HoldAllComplete, so you have to evaluate the code that produces the image (even if the “code” is simply a variable) before passing the resulting image to MakeBoxes. You can use With[] instead of a pure function, if you prefer.

When trying to figure this stuff, the menu command Cell > Show Expression can be helpful, in case you hadn’t run across it yet. You can use it to see how the system creates the cells you type and compute and mimic it.

Your code did not insert the image, only the “New Content” portion.

The code you offered had an error ([] where it didn’t belong) and when run only entered the “New Content” subsection and not the image.

Sorry about the []. It wasn’t in the code I pasted. Probably it got there when editing. For some reason, the mouse clicks are mapped to the wrong location when I’m using the editor. Sometimes I don’t see where I’ve typed. Anyway I took it out.

Works perfectly for me. Don’t know why it wouldn’t work for you.

I commented out the second line. It’s not necessary and may be a distraction. Works either way for me anyway.

As I understand your reply, nothing is written into nb by the last NotebookWrite[], which seems strange. And no error (I suppose).

Okay, the following worked, although the graphic size was rather small (I’ll need to figure out the sizing). I guess I need to wrap larger code in Module or something…

nb = CreateDocument[];
GeoGraphics[GeoMarker[Here]] // 
 NotebookWrite[nb, 
   Cell[BoxData[MakeBoxes[#, StandardForm]], "Output"]] &

The actual application is as follows (the token obscured for obvious reasons):

nb = CreateDocument[];
data = {};
offset = 0;
checkins = 
  Import["https://api.foursquare.com/v2/users/self/checkins?limit=250&\
v=20231001&oauth_token=XXXXXXXXXXXXXX", "RawJSON"][["response", 
    "checkins", "items"]];
While[Length[checkins] > 0,
      Do[
   	PrependTo[checkins[[i]], 
    "timestamp" -> FromUnixTime[checkins[[i, "createdAt"]]]];
   	AppendTo[data, checkins[[i]]]
   	, {i, Length[checkins]}
   ];
  offset += 250;
  checkins = 
   Import["https://api.foursquare.com/v2/users/self/checkins?offset=" \
<> ToString[offset] <> "&limit=250&v=20231001&oauth_token=XXXXXXXX", 
     "RawJSON"][["response", "checkins", "items"]];	
  ];
GeoListPlot[Table[
   GeoPosition[{data[[i, "venue", "location", "lat"]], 
     data[[i, "venue", "location", "lng"]]}]
   , {i, Length@data}], 
  GeoBackground -> {"VectorClassic", "StreetMapLabelsOnly"}, 
  ImageSize -> Large] // 
 NotebookWrite[nb, 
   Cell[BoxData[MakeBoxes[#, StandardForm]], "Output"]] &

Great. For me, the created notebook has an image a few pixels larger than the standard output of GeoGraphics[GeoMarker[Here]]. In any case, there’s the ImageSize option you can use to control the size of the output image.

The code Michael provided works perfectly for me on V14.1