Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Convert multiple text files to Mail notes Apps
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.]
    •    
  • Currently 3.20 / 5
  You rated: 5 / 5 (5 votes cast)
 
[5,777 views]  

Convert multiple text files to Mail notes | 4 comments | Create New Account
Click here to return to the 'Convert multiple text files to Mail notes' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Convert multiple text files to Mail notes
Authored by: Doc Drang on Apr 13, '12 08:49:20PM

The &#092; in the 8th line is clearly intended to be a backslash. Either the author encoded it as an HTML entity, thinking that was necessary to get it to display as a backslash, or the CMS messed up when it imported his code.

However it got there, it should be fixed before unwary readers try to run the script and get garbage in their Notes.

Edited on Apr 13, '12 09:32:55PM by Doc Drang



[ Reply to This | # ]
Convert multiple text files to Mail notes
Authored by: kirkmc on Apr 14, '12 02:15:31AM

Thanks for the heads up. I've fixed it. (It is, indeed, the CMS that has always had issues with certain characters in code style.)

---
Mac OS X Hints editor - Macworld senior contributor
http://www.mcelhearn.com



[ Reply to This | # ]
Convert multiple text files to Mail notes
Authored by: paczor on Jul 30, '12 09:50:55AM
I used this script on Snow Leopard 10.6.8 and have some problems. First of all OSX didn't allowed me to run "click the menu item" lines in Applescript part of code until I've enabled access for assistive devices. To enable it run "System Preferences", then "Universal Access" and check "Enable access of assistive devices" on the bottom.
Another problem was with assigning Applescript part as "cmd" variable. I have no time to find out why so I moved lines:
       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 
to separate Applescript file (I opened Applescript Editor, pasted above lines and saved it in my homedir as make_note.scpt).

Then I've changed very last line of Python code: instead of
os.system(cmd)
I used:
os.system("osascript /Users/paczor/make_note.scpt")
It worked very well.

If one will run this script he will end up with many notes windows opened, I had 88 notes so have to close them all. ;) If bother add following line:
click the menu item "Close" of the menu "File" of menu bar 1
after this line:
click the menu item "Paste" of the menu "Edit" of menu bar 1


[ Reply to This | # ]
Convert multiple text files to Mail notes
Authored by: kodosaru on Jul 21, '14 06:55:12AM

Two Things

1. (related to paczor's issue) On Mac OS X 10.9.4, Python 2.7.6 (brew version), you can't have a tab character in front of the END""". Should be:

24 end tell
25 end tell
26 END"""

or you get a, "258:261: execution error: Canít get end. (-1728)." Must be HEREDOC requirement for Python?

2. When you import RTF instead of TEXT, you the RTF format strings become visible in the note. Might be nice improvement to script to filter those.

Nice script, thanks.



[ Reply to This | # ]