Convert RTF/RTFD note from DEVONthink to Apple Notes

This script is more proof of concept than final product.

Select an RTF or RTFD (i.e., an RTFD with images) document in DEVONthink and run the script. It will create a note in Apple Notes (Sierra was tested). The note will contain images, if any, as embedded in the Note.

The script does not work with any other file type at present. Feel free to modify it and expand its capabilities. The script only works with a single selected item at a time. Feel free to make it work with a batch of documents.

The note is created from the “source” of the DEVONthink record, which is an HTML version kept by DEVONthink in the database. In Notes, the body text of notes is HTML. Any images present in the RTFD in DEVONthink are converted to binary and inserted into the HTML.

Apple Notes under Sierra has an odd behavior when a script creates an HTML-based note with embedded images: the images don’t appear right away. It seems that quitting Notes, waiting a second then launching it again will soon make the images appear inside the notes. I cannot explain this at this time. Just be a little patient and your notes will be made whole by whatever processing Notes is doing in the background. The problem does not occur with RTF originals – documents that did not contain images.

The script is mainly code from Sal Soghoian’s MacOSXautomation.com site, with a DEVONthink wrapper. Sal lead AppleScript and other automation tool development at Apple for years, and so if anyone knows about Notes, he knows.

(*
	This script will convert an RTF or RTFD note to a note in Apple Notes by first
	converting the note to HTML and then using the HTML source as the body of the
	new Note.
	
	Thanks for Sal Soghoian's https://www.macosxautomation.com/applescript/notes/02.html
	for half the source code.
	
	There is a glitch in Apple Notes.   Any images in the Notes note will at first not appear.
	Then after a while it will.   Usually quiting then reopening Notes will kickoff
	the image creation process.
	
	k 20170902.1
	k 20170902.2 mod per Bluefrog concept
*)


try
	tell application id "DNtp"
		
		if the kind of the content record is not in {"RTF", "RTFD"} then error "Sorry, only RTF or RTFD files work with the script"
		
		set theSource to the source of the content record
		
		tell application "Notes"
			activate
			set thisAccountName to my getNameOfTargetAccount("Choose an account:")
			display dialog "Enter the title for the new note:" default answer ¬
				"New Note" with icon 1 with title "New Note with Clipboard"
			set the noteTitle to the text returned of the result
			tell account thisAccountName
				make new note at folder "Notes" with properties {name:noteTitle, body:theSource}
			end tell
		end tell
		
	end tell
	
on error errorMessage
	display dialog errorMessage
	
end try

on getNameOfTargetAccount(thisPrompt)
	tell application "Notes"
		if the (count of accounts) is greater than 1 then
			set theseAccountNames to the name of every account
			set thisAccountName to ¬
				(choose from list theseAccountNames with prompt thisPrompt)
			if thisAccountName is false then error number -128
			set thisAccountName to thisAccountName as string
		else
			set thisAccountName to the name of account 1
		end if
		return thisAccountName
	end tell
end getNameOfTargetAccount


1 Like

Thanks for the script korm, could be most useful. I get an error message when trying to compile it—see attachment.

It’s fixed, now, in the original note above. Copy from there.

Sorry. Fat fingered the pasting.

Thanks korm for quick fix :slight_smile: works well now.