Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Scripting OSX Mail app

Featured Replies

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.

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)

  • Author

Thanks very much. I'll give that a try.

Create an account or sign in to comment

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.