I don’t think this is going to be simple.
If you inspect the body of the note with one of these Safari links with the preview, there is nothing added to the note body. I made a test note called “Test Note,” added a link as you specified, and ran this:
tell application "Notes" to set noteBody to the body of note "Test Note"
Which yields:
And if I delete the link out, then run it again, the note body does not change.
So what are these things? They’re attachments.
tell application "Notes" to set noteAttachments to the attachments of note "Test Note"
Returns:
Now that we know the thing you want is a Core Data attachment, it makes it look like this is a non-trivial programming task.
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreData/KeyConcepts.html#//apple_ref/doc/uid/TP40001075-CH30-SW1
https://macscripter.net/viewtopic.php?pid=148245
However, I don’t know exactly what the “data” is in that core data attachment; perhaps it’s possible to make some sort of file of the proper data, save it in the regular file system instead of making it a core data entity, and then attach it to the note. Or set the clipboard to contain the right format to paste this into the note in the format you want. I didn’t have any luck trying that, but perhaps it’s possible.
I did notice that you can copy and paste these previews around in notes; so one of the available formats it’s putting this thing on in the clipboard maintains it in the style you want.
With one copied, running
return clipboard info
Yields
{{«class rtfd», 492}, {«class RTF », 401}, {«class utf8», 12}, {string, 13}, {scrap styles, 42}, {Unicode text, 24}, {scrap styles, 42}}
Or, as I’ve just remembered Shane has warned me, “clipboard info” is Carbon API data that can be misleading.
This:
use framework "Foundation"
use framework "AppKit"
use scripting additions
set thePasteboard to current application's NSPasteboard's generalPasteboard()
set theTypes to thePasteboard's |types|() as list
Yields:
{"public.rtf", "NeXT Rich Text Format v1.0 pasteboard type", "public.utf8-plain-text", "NSStringPboardType", "public.utf16-external-plain-text", "CorePasteboardFlavorType 0x75743136", "dyn.ah62d4rv4gk81n65yru", "CorePasteboardFlavorType 0x7573746C", "com.apple.traditional-mac-plain-text", "CorePasteboardFlavorType 0x54455854", "dyn.ah62d4rv4gk81g7d3ru", "CorePasteboardFlavorType 0x7374796C"}
I’m not sure which of those options yields the “pretty preview” version you want, or what it would take to convert a plain URL on the clipboard to that format. But perhaps this at least gives you a track to start researching.
I mean, if, for example, this preview version you want is an RTFD, you probably could script converting a copied URL into an RTF, including a little site preview you could get with a “do shell script” CURL call, and then put that RTF on the clipboard.
It would take a little work.