Jump to content

Scripting OSX Mail app


This topic is 6956 days old. Please don't post here. Open a new topic instead.

Recommended Posts

I'm trying to create an applescript that will save the contents of a mailbox to a text file which I can then import into Filemaker. I searched here and at macscripter.net and can't find anything related to this.

To do this manually in Mail, simply select all the messages in the mailbox and then select Save As.. from the file menu.

Here is my script:


set destination_file to ((path to desktop as string) & "MailOut.txt")

tell application "Mail"

	set exportmessages to every message in mailbox "MailtoExport"

	save exportmessages in destination_file as text

end tell

From the event log, it appears to be getting the messages into "exportmessages" okay, but no file is created and nothing gets saved. There are no execution errors though.

Maybe there is a more direct way to get the messages into FM without saving in a text file.

Link to comment
Share on other sites

Hi, for me this was falling down unless i specified the mailbox and account name. Setting exportmessages to every message of the mailbox only gave me a list of references e.g

{message 1 of mailbox "Inbox" of account "accountname" of application "Mail", message 2 of mailbox "Inbox" of account "accountname" of application "Mail"}

and not a list of the actual email itself as you would get when you do it manually. Therefore using "Save As" in applescript doesn't seem to work although there may be a way to do it.

The way around it is to go through this list of messages, message 1, message 2 etc etc and extract "the source" -- this is all the information from the email.

This information is then copied to a single list called "thestring". Then you get the source of the next email and copy that on the the end of "thestring" and so on until you have copied all the emails to the list "thestring"

You then write "thestring" to a text file.

  

set theString to {}





using terms from application "Mail"

	tell application "Mail"

		activate

		

		set messagelist to every message of mailbox "Inbox" of account "youraccountname"

		repeat with theCount from 1 to (count of messagelist)

			set theMessage to item theCount of messagelist

			

			[color:"red"] set thesource to the source of theMessage [/color] 



			copy thesource to end of theString

		end repeat

		

	end tell

end using terms from





set startpath to (path to startup disk) as string



set filepath to (startpath & "MAILOUT.txt")



set fileref to open for access file filepath with write permission



try

	write theString to fileref

	close access fileref

on error

	close access fileref

end try



 



You will notice that when you get "the source" of the email there is nore information that you would normally get when you do a manual "Save as"



You can be more selective, rather than getting "the source" you can select individual items so as "the content", "the date received", "the subject" and so on. So rather than getting the source, get the individual items you want, create a small string with them and then copy them on to the end of "thestring". Do do this you w ould replace the line in red with:



 
  		set theSubject to the subject of theMessage

			set thedate to the date received of the message

			set thecontent to the content of theMessage

			set the thesource to theSubject & (ASCII character 13) & thedate & (ASCII character 13) & thecontent(ASCII character 13)

Link to comment
Share on other sites

This topic is 6956 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.