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


Click here to return to the 'Store mail as audio file' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Store mail as audio file
Authored by: gjw on Jan 29, '08 02:51:47PM
The following script will turn e-mail into audio files stored in iTunes. The sender will be the artist and the track will be listed as part of a compilation album called MailTunes.

property kDeleteTempFiles : false -- not yet supported
property kMarkAsRead : true
property kReaderVoice : "Victoria"

on cleanBody(inBodyText)
	set theParagraphCount to number of paragraphs of inBodyText
	repeat with theIndex from 1 to theParagraphCount
		set theParagraph to paragraph theIndex of inBodyText
		if theParagraph = "-- " then
			return (paragraphs 1 through (theIndex - 1) of inBodyText) as string
		end if
	end repeat
	return inBodyText
end cleanBody

on formatDate(inDate)
	set theYear to year of inDate
	set theMonth to month of inDate
	set theDay to day of inDate
	set theDateString to (theYear * 10000) + (theMonth * 100) + theDay as string
	set theTime to time of inDate
	set theHour to (theTime / hours) div 1
	set theMinute to ((theTime - (theHour * hours)) / minutes) div 1
	set theSecond to theTime - ((theHour * hours) + (theMinute * minutes))
	set theHourString to theHour as string
	if theHour < 10 then set theHourString to "0" & theHourString
	set theMinuteString to theMinute as string
	if theMinute < 10 then set theMinuteString to "0" & theMinuteString
	set theTimeString to theHourString & theMinuteString & (theSecond as string)
	set theResult to theDateString & theTimeString
	return theResult
end formatDate

on findSaveLocation()
	tell application "Finder"
		if true then -- Test code to make our temp folder inside the temp directory.
			set theTempPath to path to temporary items as string
			set theRootFolder to folder theTempPath
		else
			set theRootFolder to folder "Desktop" of home
		end if
		
		try
			set theResult to folder "MailTunes" of theRootFolder
		on error
			set theResult to make new folder at theRootFolder with properties {name:"MailTunes"}
		end try
	end tell
	return theResult
end findSaveLocation

on processOneMessage(inMessage, inTempFolder)
	-- Copy the message data from Mail
	tell application "Mail"
		set theSender to sender of inMessage
		set theSenderName to extract name from theSender
		set theSubject to subject of inMessage
		set theDate to date sent of inMessage
		set theFileName to theSubject & " " & my formatDate(theDate) & ".aiff"
		set theBody to my cleanBody(content of inMessage)
	end tell
	
	display dialog theBody
	
	-- Save off the audio recording
	set thePath to inTempFolder & theFileName
	say theBody using "Victoria" saving to thePath
	
	-- Mark the message as read if we're supposed to
	if kMarkAsRead then tell application "Mail" to set read status of inMessage to true
	
	-- Build an alias list to import
	tell application "Finder" to set theFiles to file thePath as alias as list
	
	-- Import into iTunes
	tell application "iTunes"
		try
			set thePlaylist to playlist "MailTunes"
		on error
			set thePlaylist to make new playlist with properties {name:"MailTunes"}
		end try
		set theTrack to add theFiles
		set theConverted to convert theTrack
		repeat with theItem in theConverted
			set album of theItem to "MailTunes"
			set artist of theItem to theSenderName
			set compilation of theItem to true
			duplicate theItem to thePlaylist
		end repeat
		set theLocation to location of theTrack as string
		delete theTrack
	end tell
	
	-- Move the imported file to the trash
	tell application "Finder" to move file theLocation to trash
end processOneMessage

on run
	set theDestination to my findSaveLocation() as string
	tell application "Mail"
		-- First the primary inbox
		if the unread count of inbox > 0 then
			set theMessages to (every message of inbox whose read status is false)
			repeat with theMessage in theMessages
				my processOneMessage(theMessage, theDestination)
			end repeat
		end if
		-- Now handle other mailboxes
		set theMailboxes to every mailbox whose name is not "Junk"
		repeat with theMailBox in theMailboxes
			if (offset of "Junk" in (name of theMailBox as string)) = 0 then
				if the unread count of theMailBox > 0 then
					set theMessages to (every message of theMailBox whose read status is false)
					repeat with theMessage in theMessages
						my processOneMessage(theMessage, theDestination)
					end repeat
				end if
			end if
		end repeat
	end tell
	tell application "Finder" to move folder theDestination to trash
end run

using terms from application "Mail"
	on perform mail action with messages inMessageList
		set theTempFolder to my findSaveLocation() as string
		repeat with theMessage in inMessageList
			my processOneMessage(theMessage, theTempFolder)
		end repeat
		tell application "Finder" to move folder theTempFolder to trash
	end perform mail action with messages
end using terms from


[ Reply to This | # ]