Export Apple Notes to XML

Tech | 0 comments

This AppleScript (written by Claude) will export ALL of your Apple Notes to a file on your desktop.

-- Export all Apple Notes to a single XML file.
-- Output: ~/Desktop/AppleNotes.xml  (change OUTPUT_PATH below if you want)
-- Run from Script Editor (Run button) or Terminal:
--    osascript ~/path/to/export-notes-to-xml.applescript
-- First run: macOS will prompt to allow automation access to Notes.

property OUTPUT_PATH : (POSIX path of (path to desktop folder)) & "AppleNotes.xml"

on replaceText(theText, oldStr, newStr)
	set saveTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to oldStr
	set theItems to text items of theText
	set AppleScript's text item delimiters to newStr
	set theText to theItems as text
	set AppleScript's text item delimiters to saveTID
	return theText
end replaceText

on escapeAttr(s)
	try
		set s to s as text
	on error
		set s to ""
	end try
	set s to my replaceText(s, "&", "&")
	set s to my replaceText(s, "<", "&lt;")
	set s to my replaceText(s, ">", "&gt;")
	set s to my replaceText(s, "\"", "&quot;")
	set s to my replaceText(s, (ASCII character 13), " ")
	set s to my replaceText(s, (ASCII character 10), " ")
	return s
end escapeAttr

on escapeCData(s)
	try
		set s to s as text
	on error
		set s to ""
	end try
	-- Guard against ]]> sequences inside the body breaking the CDATA section.
	return my replaceText(s, "]]>", "]]]]><![CDATA[>")
end escapeCData

on writeLine(fileRef, theText)
	write (theText & linefeed) to fileRef as «class utf8»
end writeLine

on run
	set fileRef to open for access POSIX file OUTPUT_PATH with write permission
	set eof of fileRef to 0
	my writeLine(fileRef, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
	my writeLine(fileRef, "<notes exported=\"" & ((current date) as string) & "\">")

	set noteCount to 0
	tell application "Notes"
		set allNotes to every note
		repeat with n in allNotes
			set noteCount to noteCount + 1
			try
				set noteName to name of n
			on error
				set noteName to ""
			end try
			try
				set noteBody to body of n
			on error
				set noteBody to ""
			end try
			try
				set noteId to id of n
			on error
				set noteId to ""
			end try
			try
				set noteCreated to (creation date of n) as string
			on error
				set noteCreated to ""
			end try
			try
				set noteModified to (modification date of n) as string
			on error
				set noteModified to ""
			end try
			try
				set noteFolder to name of container of n
			on error
				set noteFolder to ""
			end try
			try
				set noteAccount to name of account of n
			on error
				set noteAccount to ""
			end try

			set openTag to "  <note id=\"" & my escapeAttr(noteId) ¬
				& "\" name=\"" & my escapeAttr(noteName) ¬
				& "\" folder=\"" & my escapeAttr(noteFolder) ¬
				& "\" account=\"" & my escapeAttr(noteAccount) ¬
				& "\" created=\"" & my escapeAttr(noteCreated) ¬
				& "\" modified=\"" & my escapeAttr(noteModified) & "\">"
			my writeLine(fileRef, openTag)
			my writeLine(fileRef, "    <body><![CDATA[" & my escapeCData(noteBody) & "]]></body>")
			my writeLine(fileRef, "  </note>")
		end repeat
	end tell

	my writeLine(fileRef, "</notes>")
	close access fileRef
	return "Exported " & noteCount & " notes to " & OUTPUT_PATH
end run

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.