Simple Solution to Integrate Office 365 / Outlook Web with DT3

I use Office 365 (OWA - Outlook Web App) for my email. I have posted a number of times about my biggest need for software automation – importing individual emails in Outlook Web to DT3 along with their attachments.

While my past solutions have worked, somehow I may have overlooked the simplest solution of all. Or maybe the interface in OWA changed at some point and I had not noticed.

In any event, next to each message in OWA are 3 dots - in that menu there is an option to Download the message to an .eml file in your Downloads folder:

I set up Hazel to process emails to my Downloads folder via a script (adapted from an old post - thank you @BLUEFROG - my version uses the group selector rather than a specific import location ):

tell application id "DNtp"

set newFile to import theFile to display group selector

set {recName, recURL} to {name, reference URL} of newFile

if (exists newFile) then tell application "Finder" to delete theFile

end tell

This works very well so all I need to do is to click Download on a message and then the Group Selector shows up to let me choose where to put the message within my set of DT3 databases.

If there are attachments within the email they remain embedded in the original email inside DT3 so the messages are thus retained precisely in native email form. However often it is desirable to place those messages within a subgroup. I adapted this script (which I run from Keyboard Maestro) to do just that - it mostly comes from a post on this Forum but unfortunately I do not remember who authored it (thank you whoever it was - I suspect @cgrunenberg )

tell application id "DNtp"
	set theSelection to the selection
	set tmpFolder to path to temporary items
	set tmpPath to POSIX path of tmpFolder
	
	with timeout of 14400 seconds
		repeat with theRecord in theSelection
			if type of theRecord is unknown and path of theRecord ends with ".eml" then
				set theRTF to convert record theRecord to rich
				set theURL to reference URL of theRecord
				set newGroup to false
				
				try
					if type of theRTF is rtfd then
						set thePath to path of theRTF
						set theGroup to parent 1 of theRecord
						set theName to name of theRecord
						set theModificationDate to the modification date of theRecord
						set theCreationDate to the creation date of theRecord
						set theAdditionDate to the addition date of theRecord
						
						tell text of theRTF
							if exists attachment in attribute runs then
								
								tell application "Finder"
									set filelist to every file in ((POSIX file thePath) as alias)
									repeat with theFile in filelist
										set theAttachment to POSIX path of (theFile as string)
										
										if theAttachment does not end with ".rtf" and theAttachment does not end with ".png" then
											try
												with timeout of 7200 seconds
													
													-- Importing skips files inside the database package,
													-- therefore let's move them to a temporary folder first
													
													set theAttachment to move ((POSIX file theAttachment) as alias) to tmpFolder with replacing
													set theAttachment to POSIX path of (theAttachment as string)
													tell application id "DNtp"
														if newGroup is false then
															set newGroup to create record with {name:theName, type:group, modification date:theModificationDate, creation date:theCreationDate, addition date:theAdditionDate} in theGroup
														end if
														set importedFile to import theAttachment to newGroup
														set URL of importedFile to theURL
														set the modification date of importedFile to theModificationDate
														set the creation date of importedFile to theCreationDate
														set the addition date of importFile to theAdditionDate
														
													end tell
												end timeout
											end try
										end if
									end repeat
									
								end tell
								if newGroup is not false then
									tell application id "DNtp"
										move record theRecord to newGroup
									end tell
								end if
							end if
						end tell
					end if
				on error msg
					display dialog msg
				end try
			end if
			
			delete record theRTF
		end repeat
	end timeout
end tell

on makeDate(dateString)
	set theDate to date dateString
end makeDate

All told this works really well for importing specific email messages and immediately filing them in the relevant Group in DT3. Since my desktop and laptop computers both sync DT3 to a common WebDav sync store on my Synology, it works equally well from either the desktop or laptop computer.

6 Likes

Thank you, @rkaplan, for providing the Keyboard Maestro AppleScript. It’s very slick indeed.

I just starting experimenting with Outlook for Mac and your script is the final piece of the puzzle I was looking for.

1 Like

@rkaplan, @BLUEFROG, or anyone who can offer assistance…

After dragging an email from Outlook for Mac to DEVONthink’s dock icon, the second AppleScript above works well when invoked within Keyboard Maestro; however, I’d really like to run it in a Smart Rule. As it stands now an AppleScript in a Smart Rule invokes a keyboard combination which Keyboard Maestro recognizes causing the AppleScript to run. Works well, but it’s kind of a Rube Goldberg setup.

Does anyone know what I need to do to run the script within a Smart Rule? Thanks…

What would the trigger be to run it as a Smart Rule?

I’ve dealt with this in support already. :slight_smile:

Yes, Jim did attempt to assist me; however, I’m using the new version of Outlook for Mac which does not work well with AppleScript.

My current workflow in Outlook is to drag an email to the DEVONthink icon in the dock, set the destination location in the popup panel, after which the following Smart Rule activates:

image

The AppleScript that executes within the Smart Rule is below. I set the Keyboard Maestro macro to be triggered by the hot key combination ⌥+o Not pretty, but it works.

1 Like

Interesting tweak on the original concept - thanks

For various reasons I don’t always want to extract the attachments; sometimes I like to keep them in original native form so I prefer to execute the script manually. But I can see how this can be a preferred solution for many people.

The Keyboard Maestro macro can be modified to run only when a certain condition is met. I have modified it to run the AppleScript only when the “x” key is depressed. If that key is not depressed, the email will be copied to your DEVONthink database without attachments separated out.

See below…