Convert multiple text files to Mail notes

Apr 13, '12 07:30:00AM

Contributed by: Anonymous

I have been using plain text files (.txt) for storing my notes since the arrival of Notational Velocity a while ago. When I saw that Mountain Lion will have a dedicated Notes app, I decided it would be great to switch over to Mail's notes system in preparation for the new OS.

I couldn't find a suitable method for rapidly importing my text notes into apple mail, so I combined a script found on MacRumors with some python, which can then be packaged into a Service using Automator ("Service receives selected files and folders").

import sys
import os
print sys.argv
for filename in sys.argv[1:]:
    print filename
    text = open(filename,'r').readlines()
    title = os.path.splitext(os.path.basename(filename))[0]
    text = title +'\n'+' '.join(text)

    # Store file contents in clipboard
    outf = os.popen("pbcopy", "w")
    outf.write(text)
    outf.close()

    cmd = """osascript< < END
	tell application "Mail"
		activate
	end tell

	tell application "System Events"
		tell process "Mail"
			click the menu item "New Note" of the menu "File" of menu bar 1
			click the menu item "Paste" of the menu "Edit" of menu bar 1
		end tell
	end tell
    END"""

    os.system(cmd)
[kirkmc adds: I haven't tested this. But I do agree that it will be practical to use Apple's Notes app on Mountain Lion. However, I'm not prepared to use Mail for notes; I don't like the display options.]

Comments (4)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20120224235738709