Retain link formatting on Apple Notes export

Using an AppleScript, I have managed to export my Apple Notes to HTML, from which I can make other conversions. However, this removes all the inline links. Is there a way of bulk exporting them while retaining formatting?

RTF seems the obvious way to go here, because that seems to be the format Apple Notes uses under the hood. I have confirmed link formatting is preserved by copy pasting a note into TextEdit, so it’s clearly possible. But I haven’t been able to figure out how to export to RTF using AppleScript. Another way might be by using Shortcuts to go through each note, copy and paste it into TextEdit, and save all the files in a folder somewhere. But I don’t know how to do this.

Does anyone know how to do this, or know another way inline links might be maintained?

This is a duplicate of an Ask Different question. I’ll paste the comments here for easier reading:

1 What is the script doing to export the notes?

2 It’s a slightly modified version of Bear’s export AppleScript bear.app/faq/migrate-from-apple-notes

2 It works perfectly apart from removing link formatting (as well as newlines not working, but I can fix that in another script afterwards).

1 That script gets the body of the note (kinda crappy HTML content, with just about everything in a division), but it doesn’t look like any of the note properties include links.

2 How do you know the note properties don’t include links, is there Apple documentation on this? (I struggled to find any.) Yes, the problem is that inline links aren’t exported, do you know of any way to do this beyond manually copying each individual note into TextEdit?

1 There is no terminology in the scripting dictionary for links, and the body (HTML) doesn’t include them, so if it isn’t a bug then they are just not provided. While hunting around I came across a few topics that go back a few years, so it looks like the issue has been around for a while.

2 Is there any way, that you know of, of automating copy paste from one app to another (in this case, Notes to TextEdit or Pages) on Mac?

Hi @applescripter17. Welcome to MacScripter.

It’s possible to simulate the keystrokes for copying and pasting using GUI Scripting. But you’d need to select the text manually (or at least click in the note) first, so it’s only really sensible for doing one note at a time. To get you started, this saves the RTF data for the currently focused note to a file on the desktop:

tell application "Notes" to activate

tell application "System Events"
	-- Make sure Notes is frontmost.
	set frontmost of application process "Notes" to true
	-- Copy all the text of the currently focused note.
	keystroke "ac" using {command down}
end tell

-- Wait for the keystrokes to take effect.
delay 0.5
-- Retrieve the RTF data from the clipboard.
set RTFData to (the clipboard as «class RTF »)

-- Save the data to file.
set filePath to (path to desktop as text) & "Saved note.rtf" -- Edit as required.
set fRef to (open for access file filePath with write permission)
try
	write RTFData to fRef
	close access fRef
on error msg
	close access fRef
	display dialog msg buttons {"OK"}
end try

Alternatively, this pastes into a new TextEdit document:

tell application "Notes" to activate

tell application "System Events"
	-- Make sure Notes is frontmost.
	set frontmost of application process "Notes" to true
	-- Copy all the text of the currently focused note.
	keystroke "ac" using {command down}
end tell

-- Wait for the keystrokes to take effect.
delay 0.5

tell application "TextEdit"
	activate
	make new document -- Assuming new documents are in RTF format.
end tell

tell application "System Events"
	-- Paste into the frontmost TextEdit document.
	set frontmost of application process "TextEdit" to true
	keystroke "v" using {command down}
end tell

applescripter17. The shortcut included below appears to do what you want. A few comments:

  • You need to set the destination folder at the top of the shortcut where indicated.

  • The shortcut saves each note as a separate RTF file, but I suspect the notes could all be placed in one file if that’s desired.

  • Because of a bug in the Shortcuts app, it’s advisable but not absolutely essential that the Notes app be open when the shortcut is run.

  • I tested this shortcut on my Sequoia computer, and links and other formatting were retained in the created RTF files.

  • The following screenshot only shows a portion of the shortcut.

Save Notes.shortcut (23.7 KB)