Scripting MS Outlook

Trying to automate extraction of emails from MS Outlook 2016 on OS X High Sierra. I’ve been fiddling with both Applescript and Automator and getting very mixed results. I’ll go through each of these separately.

First, I tried Automator, and there are some packaged actions available there, and the one that was most interesting for my purposes was to “Get selected items from Outlook”. Ok, fair enough. But that means that you have to manually select the items in the UI before the action returns any results. Not ideal, and not what I want.

Automator has another packaged action “Save Outlook items as Files”, (which is ultimately what I want to do with the “selected items”) and that works great. Unfortunately this forces me into the position of manually selecting items from the GUI.

So I turned to Applescript and got a basic Outlook folder iterator working nicely. But when I iterate through the messages in a particular folder and try to invoke the “save” operation on a message, via the script, I’m getting an error that seems to indicate that Outlook doesn’t really know what “save” means in the context of saving a message.

Anyone have any suggestions for how to get this fully automated, so all I have to do is enter the name of an Outlook folder and get the messages saved as files?

FYI, I’m doing an NLP project using my email messages as a corpus, in case you want to know why I’m going through this exercise.

Not sure it is possible to programmatically save email messages out of Outlook 2016 for Mac to the Finder. The Save function is just used for saving drafts (open a new email and click save and it goes into Drafts folder).

Here is a script that can move the messages into a “On My Computer” folder with the current month as the name, but then I think the only option currently to get them out of Outlook as files is via drag-and-drop.

tell application "Microsoft Outlook"
	set folderName to "Inbox"
	if name of selected folder is not equal to folderName then set selected folder to inbox
	set selectedMessages to messages of selected folder
	repeat with theMessages in selectedMessages
		set {year:y, month:m, day:d} to time received of theMessages
		set datedFolder to "" & y & "-" & m & ""
		if not (exists mail folder datedFolder of on my computer) then
			make new mail folder with properties {name:datedFolder} at on my computer
		end if
		set destFolder to mail folder datedFolder of on my computer
		move theMessages to destFolder
	end repeat
end tell

Seems that in 2016 all files are saved deep in the user’s library in SQL format and therefore aren’t email files just saved in a folder. The drag out of Outlook creates the actual email files.