Import files into the Notes.app in Mountain Lion

The new Notes.app in Mountain Lion is scriptable which is great. I wanted to import some files into Notes.app so I wrote this script.

Notice that this script is setup for an “iCloud” account and the notes are added to a folder called “Import Folder”. You’ll also notice that this converts the file’s contents into html code. It seems notes can take plain text but in my testing it doesn’t recognize line breaks and paragraphs in plain text. As such I convert all file types into html code and then send that to Notes because Notes natively understands html anyway.

This script will work on any file type that textutil can understand which is a lot (txt, rtf, rtfd, html, word files etc.).

One bad thing… although we give the new note a name you will notice that the name will change to the first line of text once you start editing it. This can’t be helped because for some reason this is how Notes.app gets the names of a note. So just realize that once you edit a new note the name may change if the first line of the note is not the same as the name you assign to it.

One other caveat… if your file has images embedded in it this will not work with those images correctly. I suggest you drag/drop your images into the note later by hand. If anyone wants to update this script to handle images then by all means make that improvement.

Good luck!

-- this imports files into Notes.app in MacOSX 10.8 Mountain Lion
-- it converts files into a format compatible with Notes.app and creates a new note from that converted text
-- find lots of Notes scripts here: http://www.macosxautomation.com/applescript/notes/index.html

-- get a file to import to a note
set noteFile to choose file with prompt "Import which file?"

-- get the note name from the file name
set {noteName, ext} to getName_andExtension(noteFile)

-- get the name of the account that you want to add the note to
set accountName to getNameOfTargetAccount()

-- get the name of the folder to add the note to
set folderName to getNameOfTargetFolderOfAccount(accountName)

-- get the file's text as html
if ext is "html" then
	set noteText to read noteFile
else
	-- convert the file's contents into html code
	set noteText to do shell script "/usr/bin/textutil -stdout -convert html " & quoted form of POSIX path of noteFile & " | /usr/bin/tidy -ib -utf8"
end if

tell application "Notes"
	tell account accountName
		-- make sure the folder exists
		if not (exists folder folderName) then make new folder with properties {name:folderName}
		
		-- add the note to the folder
		tell folder folderName
			make new note with properties {name:noteName, body:noteText}
		end tell
	end tell
end tell


(****************** SUBROUTINES *****************)
-- this lets you choose which acount you want to target
-- if there's only 1 account then that account name is returned
on getNameOfTargetAccount()
	tell application "Notes"
		if (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 "Add note to which account?"
			if thisAccountName is false then error number -128
			set thisAccountName to thisAccountName as text
		else
			set thisAccountName to the name of first account
		end if
		return thisAccountName
	end tell
end getNameOfTargetAccount

-- this lets you choose which folder you want to target from an account
-- if there's only 1 folder then that folder name is returned
on getNameOfTargetFolderOfAccount(accountName)
	set folderName to missing value
	tell application "Notes"
		tell account accountName
			if (count of folders) is greater than 1 then
				set theseFolderNames to the name of every folder
				set folderName to choose from list theseFolderNames with prompt "Add note to which folder?"
				if folderName is false then error number -128
				set folderName to folderName as text
			else
				set folderName to the name of first folder
			end if
		end tell
	end tell
	return folderName
end getNameOfTargetFolderOfAccount

on getName_andExtension(f)
	set f to f as text
	set {name:nm, name extension:ex} to info for file f without size
	if ex is missing value then
		set ex to ""
	else
		set nm to text 1 thru ((count nm) - (count ex) - 1) of nm
	end if
	return {nm, ex}
end getName_andExtension

EDIT: Using some techniques learned from the link in Shane’s post below, I made some modifications to make this script better.

Hank,

You might find this useful:

www.macosxautomation.com/applescript/notes/index.html

I haven’t had a chance myself…

Thanks for the pointer Shane. I hadn’t seen that and there’s a lot of good information there that can be used to help tailor this script.

Thanks, this is very helpful information.

I have been, however, unable to create an attachment for a note. I wonder if you have any insights as to how to do that?

Thanks.
nl

Is it possible to modify the above script to allow selection of multiple text files? I need to import over 2000 files. I am not familiar with scripting but need a solution.
Any help would be much appreciated.
Thanks!

You can change the script to this. Now you choose a folder and all of the files in that folder (even subfolders) will be imported into Notes.

NOTE: I did not test this script but I think it will work. Good luck! :smiley:

-- this imports all the files in a folder into Notes.app in MacOSX 10.8 Mountain Lion
-- it converts files into a format compatible with Notes.app and creates a new note from that converted text
-- find lots of Notes scripts here: http://www.macosxautomation.com/applescript/notes/index.html

-- choose the folder with the notes
set notesFolder to choose folder

-- find al the files in that folder
tell application "Finder"
	set noteFiles to (files of entire contents of notesFolder) as alias list
end tell

-- get the name of the account that you want to add the notes to
set accountName to getNameOfTargetAccount()

-- get the name of the folder to add the notes to
set folderName to getNameOfTargetFolderOfAccount(accountName)

-- make sure the folder exists in Notes
tell application "Notes"
	tell account accountName
		if not (exists folder folderName) then make new folder with properties {name:folderName}
	end tell
end tell

repeat with noteFile in noteFiles
	-- get the note name from the file name
	set {noteName, ext} to getName_andExtension(noteFile)
	
	-- get the file's text as html
	if ext is "html" then
		set noteText to read noteFile
	else
		-- convert the file's contents into html code
		set noteText to do shell script "/usr/bin/textutil -stdout -convert html " & quoted form of POSIX path of noteFile & " | /usr/bin/tidy -ib -utf8"
	end if
	
	-- add the note to the folder
	tell application "Notes"
		tell account accountName
			tell folder folderName
				make new note with properties {name:noteName, body:noteText}
			end tell
		end tell
	end tell
end repeat


(****************** SUBROUTINES *****************)
-- this lets you choose which acount you want to target
-- if there's only 1 account then that account name is returned
on getNameOfTargetAccount()
	tell application "Notes"
		if (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 "Add note to which account?"
			if thisAccountName is false then error number -128
			set thisAccountName to thisAccountName as text
		else
			set thisAccountName to the name of first account
		end if
		return thisAccountName
	end tell
end getNameOfTargetAccount

-- this lets you choose which folder you want to target from an account
-- if there's only 1 folder then that folder name is returned
on getNameOfTargetFolderOfAccount(accountName)
	set folderName to missing value
	tell application "Notes"
		tell account accountName
			if (count of folders) is greater than 1 then
				set theseFolderNames to the name of every folder
				set folderName to choose from list theseFolderNames with prompt "Add note to which folder?"
				if folderName is false then error number -128
				set folderName to folderName as text
			else
				set folderName to the name of first folder
			end if
		end tell
	end tell
	return folderName
end getNameOfTargetFolderOfAccount

on getName_andExtension(f)
	set f to f as text
	set {name:nm, name extension:ex} to info for file f without size
	if ex is missing value then
		set ex to ""
	else
		set nm to text 1 thru ((count nm) - (count ex) - 1) of nm
	end if
	return {nm, ex}
end getName_andExtension

I can’t get this script to run. I get an error like this:

Working but stopping on some txt files wit errors

Error
Warning trimming empty >span> Doctype blablabla

Need help on this one???

Take out

Work perfect…

Ditto… worked great after removal of that line… It crashed on one note, i don’t know why, but now I can get rid of Notational Velocity nvalt and stick w/ Apple Notes. Somebody should turn this into an app w/ proper error messages and submit to macupdate.com … Thank you all for your help!

Thanks. The script does create Notes in ML.

But there is a glitch when they sync to iOS Notes.

The first line syncs over and the rest is blank.

However, if I make any edit in the notes record in OSX Notes, the full note text body appears in iOS.

Odd. Something about the way Notes creates note titles from text I think.