Notes.app Recovery/Missing Notes

Hi all, :lol:

I came to the shocking realization that some important notes I’d taken weren’t showing up in the app’s interface; it seemed they were gone. Basically, I’d like to offer my help in the form of an applet, in case anyone else has had the same issue. After investigating, I realized the notes were actually contained within the app itself, and after surfing around some forums, patched together a working utility. Posting the script online for those who don’t wanna bother D/Ling another program. Quick tip: don’t run this if you already have an important file on your desktop called “Notes.html” or it’ll overwrite/delete it.

End result: program writes Notes.app’s database into an HTML, which is opened with default browser for easy copy/pasting. I’m eager for feedback. :smiley: As always from me, the app size is very small.

Download from Dropbox

--Version 1.0.0
--Recovers missing/corrupted notes from Notes.app
--Opens in default browser
--Notes will be duplicated in the output HTML file
--Upon multiple iterations, output appended to HTML file
--Utilizes code from the following:
--	http://bit.ly/1guzQxA
--	http://bit.ly/UXv5To
--	http://bit.ly/15rLvbR

on replaceText(find, replace, someText)
	set prevTIDs to text item delimiters of AppleScript
	set text item delimiters of AppleScript to find
	set someText to text items of someText
	set text item delimiters of AppleScript to replace
	set someText to "" & someText
	set text item delimiters of AppleScript to prevTIDs
	return someText
end replaceText

on write_to_file(this_data, target_file, append_data) -- (string, file path as string, boolean)
	try
		set the target_file to the target_file as text
		set the open_target_file to ¬
			open for access file target_file with write permission
		if append_data is false then ¬
			set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file

on WriteLog(the_text)
	set this_story to the_text
	set this_file to (((path to desktop folder) as text) & "Notes.html")
	my write_to_file(this_story, this_file, true)
end WriteLog

try
	do shell script "rm -rf ~/Desktop/Notes.html"
end try

set out to do shell script ("defaults read com.apple.LaunchServices.plist | grep -A 1 public.html")
set out to replaceText("            LSHandlerContentType = \"public.html\";
            LSHandlerRoleAll = \"", "", out)
set out to replaceText("\";", "", out)

set x to (do shell script "cat ~/Library/Containers/com.apple.Notes/Data/Library/Notes/NotesV1.storedata | grep -aio '<html>.*</html>'")

WriteLog(x)

set x to (do shell script "cat ~/Library/Containers/com.apple.Notes/Data/Library/Notes/NotesV1.storedata-wal | grep -aio '<html>.*</html>'")

WriteLog(x)

tell application id out to open (((path to desktop folder) as text) & "Notes.html")