Import from Apple Notes

Have successfully imported notes from Evernote using DT’s very efficient tool.
Lovely job - well done, DT !

During my migration away from Evernote, I started using Apple Notes, which is pretty good, but some indexing sync problems, so want to move these over to DT

But how ?

Apple Notes doesn’t seem to have much export capability

DT doesn’t have Apple Notes import … yet

Is it a case of scripting ?
Any tips / experience/ Applescript recipes ?

Importing from Apple Notes is not a trivial thing (not matter how simple Notes may appear to be).

You could try this script. However, Notes’ AppleScript support seems to be broken on Sierra & El Capitan and the import of attachments doesn’t work:


-- Import items & attachments from Notes.app

tell application id "com.apple.Notes"
	try
		set theTempFolder to path to temporary items
		tell application id "DNtp"
			set theDatabase to current database
			set theNotesGroup to missing value
		end tell
		
		set theFolders to the folders
		repeat with theFolder in theFolders
			set theNotes to notes of theFolder
			tell application id "DNtp"
				if theNotesGroup is missing value then
					set theNotesGroup to create location "/Notes" in theDatabase
					try
						set pathToApp to (path to application id "com.apple.Notes" as string)
						set pathToIcon to POSIX path of (pathToApp & "Contents:Resources:AppIcon.icns")
						set thumbnail of theNotesGroup to pathToIcon
					end try
					set theGroup to create location "/Notes/" & (name of theFolder) in theDatabase
				end if
			end tell
			repeat with theNote in theNotes
				set theName to name of theNote
				set theBody to body of theNote
				set theCreation to creation date of theNote
				set theModification to modification date of theNote
				tell application id "DNtp" to create record with {name:theName, type:formatted note, content:theBody, creation date:theCreation, modification date:theModification} in theGroup
				
				repeat with theAttachment in attachments of theNote
					set theAttachmentName to the name of theAttachment
					set theAttachmentPath to (theTempFolder & theAttachmentName) as string
					save theAttachment in file theAttachmentPath -- as native format -- Seems to be broken on El Capitan & Sierra
					tell application id "DNtp" to import theAttachmentPath to theGroup
				end repeat
			end repeat
		end repeat
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
	end try
end tell

Depending on the number of your notes maybe Note2Txt would be helpful? With this you can export each of your notes to a txt file (one for each note) and then import those into DT.

I am not using it myself so I cannot tell you if it delivers, but I read at MacSparky about it:

macsparky.com/blog/2016/5/e … h-note2txt

itunes.apple.com/us/app/note2tx … z7&ct=blog

Hope this helps.

I was able, just now, on 10.11.6, to get this script to import from Notes to DEVONthink – without attachments, of course. The trick is that Notes on OS X has to finish syncing with iCloud before the script is successful. If you run the script while Notes is syncing it will not import the notes even if they are old and already-synced. I assume Notes is locking its database during sync and so AppleScript cannot access the data while the lock is active.

Interesting. And the script does probably fail silently, right?

Yes. It just stops as if nothing happened – no errors. I suppose I could add a counter and a message reporting the number of notes added to DEVONthink, for verification purposes.

Thank you everyone - very valuable - will check out the script

I have 1000+ notes so cannot do anything on a note-by-note basis

I have also found :

  • writeapp.net/notesexporter/ : exports to .txt which can be renamed easily as a bath using something like EasyRename to .htm And then DEVONthink can import

  • Notes Export.app : not so easy to find - go for V0.4. This exports to .htm directly and seems bit better

Haven’t solved the attachments issue and formatted notes like checklists. But very few attachments so may be able to handle this manually.

Biggest learning for me : only select a system which is easy to import and export data to avoid lock-in. Fortunately DT ticks these boxes

Here’s a revised script that should be able to import the attachments on El Capitan & Sierra too:


-- Import items & attachments from Notes.app

tell application id "com.apple.Notes"
	set theHomeFolder to (path to home folder) as string
	if theHomeFolder contains "Library:Containers:com.apple.Notes:" then -- Sandboxed?
		set od to AppleScript's text item delimiters
		set AppleScript's text item delimiters to ":"
		set theAttachmentsFolder to ((text items 1 thru 4 of theHomeFolder) as string) & ":Group Containers:group.com.apple.notes:Media:"
		set AppleScript's text item delimiters to od
	else
		set theAttachmentsFolder to missing value
	end if
	
	try
		set theTempFolder to path to temporary items
		tell application id "DNtp"
			set theDatabase to current database
			set theNotesGroup to missing value
			set theAttachmentsGroup to missing value
		end tell
		
		set theFolders to the folders
		repeat with theFolder in theFolders
			set theNotes to notes of theFolder
			set theCurrentGroup to missing value
			set theCurrentAttachmentsGroups to missing value
			
			tell application id "DNtp"
				if theNotesGroup is missing value then
					set theNotesGroup to create location "/Notes" in theDatabase
					set theAttachmentsGroup to create location "/Notes/Attachments/" in theDatabase
					
					try
						set pathToApp to (path to application id "com.apple.Notes" as string)
						set pathToIcon to POSIX path of (pathToApp & "Contents:Resources:AppIcon.icns")
						set thumbnail of theNotesGroup to pathToIcon
					end try
				end if
			end tell
			repeat with theNote in theNotes
				if theCurrentGroup is missing value then tell application id "DNtp" to set theCurrentGroup to create location "/Notes/" & (name of theFolder) in theDatabase
				
				set theName to name of theNote
				set theBody to body of theNote
				set theCreation to creation date of theNote
				set theModification to modification date of theNote
				tell application id "DNtp" to create record with {name:theName, type:formatted note, content:theBody, creation date:theCreation, modification date:theModification} in theCurrentGroup
				
				repeat with theAttachment in attachments of theNote
					if theCurrentAttachmentsGroups is missing value then tell application id "DNtp" to set theCurrentAttachmentsGroups to create location "/Notes/Attachments/" & (name of theFolder) in theDatabase
					
					set theAttachmentName to the name of theAttachment
					set theAttachmentPath to (theTempFolder & theAttachmentName) as string
					save theAttachment in file theAttachmentPath -- as native format -- Seems to be broken on El Capitan & Sierra
					tell application id "DNtp" to import theAttachmentPath to theCurrentAttachmentsGroups
				end repeat
			end repeat
			
		end repeat
		
		if theAttachmentsFolder is not missing value then
			tell application "Finder"
				set theFolders to every folder of folder theAttachmentsFolder
				set theHashes to {}
				repeat with theFolder in theFolders
					set theFiles to every file of theFolder
					repeat with theFile in theFiles
						set theHash to (name of theFile as string) & (size of theFile as string)
						if theHashes does not contain theHash then -- Ensure that each attachment is imported once. No idea why Apple Notes requires multiple copies.
							set thePath to (POSIX path of (theFile as alias))
							tell application id "DNtp"
								if theCurrentAttachmentsGroups is missing value then set theCurrentAttachmentsGroups to create location "/Notes/Attachments/" & (name of theFolder) in theDatabase
								import thePath to theCurrentAttachmentsGroups
							end tell
							set theHashes to theHashes & theHash
						end if
					end repeat
				end repeat
			end tell
		end if
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
	end try
end tell

Appreciate the revised script. Thank you for posting it! It’s a huge improvement for me.

On my machine the script imported all the notes and placed them in groups named after my folders in Notes. The script also successfully imported all the attachments and placed them in a subgroup named 01AD5BDD-344B-4EA5-9EB8-6A85BD3119A0 under an Attachment group. There were other subgroups under the Attachment group whose names mirrored the Notes folders, but these subgroups were all empty. And in general there seemed to be no association between the notes and the attachments, i.e. it seems one has to go through the attachments and (re)associate each one with its original note manually.

Does this describe the intended results of the script? Or is there tweaking I should be attempting?

Due to the scripting issues of Apple Notes, there’s currently no association between the notes and the attachments.

Is there no way to handle this — even manually, from the user’s end? (Well, systematically, at least.)

I’ve ended up with a few dozen notes imported, along with nearly 10,000 attachments (mostly belonging to long-deleted Apple Notes notes, which could reveal why it never worked well for me), including hundreds of files named “Image.gif” and “Image.jpeg” (mostly attached to long-deleted Evernote data that was migrated (poorly) to Apple Notes).

You can print a Note and save to DEVONthink as a PDF in the print dialog. Unfortunately, you cannot select and print multiple notes in Notes.

I recently switched from Apples Notes to Notability. Great for sharing unimportant short term notes between units via iCloud.

Is there a way to INDEX the files in NOTABILITY to DevonThink PRO Office?

Thinking it might be a “hidden” folder somewhere with all the notes inside.

I was able to index Apples Notes before, worked fine.

Not directly. You can configure Notability to backup to Google Drive, Dropbox, Box or WebDAV – and to set the backups as PDF or “notes”. Use PDF since the “notes” format will not be useful. If you backup to Dropbox, for example, your backup will be at /Dropbox/Notability. You can index that.

HOWEVER be careful of conflicts. Don’t configure both Notability iOS and Notability OS X to backup. One or the other. I prefer activating backup from iOS since that’s where I use Notabilty the most. And be aware that a note backup will get overwritten the next time you modify the note.

Ok thanks. I will not try that. Just using it for less important things like car wash codes, articles to read later but all I save is inside DTPO.

I have exactly this problem too. I once exported evernote to notes, which promptly made notes completely unusable. Eventually, I turned off icloud for notes, I cleared out my notes folder on all my macs and ios devices, went into icloud.com and deleted there too, then turned icloud back on device by device.
Notes works fine and correctly shows 1 note in one folder on all my devices (I created a test note). Everything syncs in notes as it should.
When I try “importing folders and attachments from notes” in DevonThink, I get the note and hundreds of attachments, all with meaningless names - left over from Evernote. I can’t find these anywhere on any of my systems or on icloud. I’ve even deleted the notes containers and stores on my machine.
Where is Devonthink loading notes from?
At the moment I can’t use notes with Devonthink and would like to.