2/7/2015 - 12:28 AM

mail to taskpaper.applescript

-- Quite a lot modified to input text into a plain text file like used by taskpaper. Originally based somewhere back in time on Efficient Computing's AppleScript: http://efficientcomputing.commons.gc.cuny.edu/2012/03/17/copy-email-message-in-mail-app-to-evernote-applescript/
tell application "Mail"
	--get selected messages
	set theSelection to selection
	--loop through all selected messages
	repeat with theMessage in theSelection
		--get information from message
		set theMessageDate to the date received of theMessage
		set theMessageSender to sender of theMessage
		set theMessageSubject to the subject of the theMessage
		set theMessageContent to the content of theMessage
		set theMessageURL to "message://%3c" & theMessage's message id & "%3e"
		--make a short header
		set theHeader to the all headers of theMessage
		set theShortHeader to (paragraph 1 of theHeader & return & paragraph 2 of theHeader & return & paragraph 3 of theHeader & return & paragraph 4 of theHeader & return & return)
		--import message to text file
		my WriteLog("- resolve: " & theMessageSubject & " from " & (word 1 of theMessageSender) & " " & (character 1 of word 2 of theMessageSender) & ".")
	end repeat
end tell

on WriteLog(the_text)
	set this_story to the_text
	--set this_file to (((path to desktop folder) as text) & "test.txt")
	set this_file to (POSIX file "/Users/tbendt/Dropbox/_Tim/Projects/projects.TODO.taskpaper")
	my prepend_to_file(this_story, this_file)
end WriteLog

on prepend_to_file(this_data, target_file) -- (string, file path as string, boolean)
		--set the target_file to the target_file as text
		open for access target_file
		set fileContents to (read target_file from 7 to eof)
		close access target_file
		set the open_target_file to ¬
			open for access file target_file with write permission
		set newInboxData to "Inbox:" & return & this_data
		--set fileContent to searchAndReplace(fileContents, "Inbox:", newInboxData)
		--write fileContents to the open_target_file starting at 0
		write newInboxData & fileContents to the open_target_file starting at 0
		close access the open_target_file
		return true
	on error
			close access file target_file
		end try
		return false
	end try
end prepend_to_file