Import From Outlook

Summary: Imports the selected messages from Outlook as .eml files.
Requires: EagleFiler
Install Location: ~/Library/Scripts/Applications/Microsoft Outlook/
Last Modified: 2017-03-03

Description

The recommended way to import from Outlook is to select some messages and press the capture key. That will import the messages as a new mailbox file. This script is for situations where you want to:

The unread/flagged status and categories in Outlook are converted to tags in EagleFiler.

If you want the script to bring up the options window asking you where import the messages, remove the -- before with asking for options.

Installation Instructions · Download in Compiled Format · Download in Text Format

Script

on run
    
tell application "Microsoft Outlook"
        
set _messages to my messagesFromSelection()
        
my importMessages(_messages)
    
end tell
end run

on messagesFromSelection()
    
tell application "Microsoft Outlook"
        
with timeout of 5 * 60 seconds
            
set _selection to selected objects
            
if _selection is {} then
                
try
                    
return selected folder's messages -- can't get "selected folder" if "Sent Items" is selected
                
end try
            
end if
            
return current messages
        
end timeout
    
end tell
end messagesFromSelection

on importMessages(_messages)
    
repeat with _message in _messages
        
tell application "Microsoft Outlook"
            
set _subject to my subjectFromMessage(_message)
            
set _source to source of _message
        
end tell
        
set _path to my createTempFolder() & "/" & my makeFileName(_subject)
        
my writeData(_path, _source)
        
tell application "EagleFiler"
            
import files {_path} tag names my tagsFromMessage(_message) -- with asking for options
        
end tell
    
end repeat
end importMessages

on tagsFromMessage(_message)
    
tell application id "com.microsoft.Outlook"
        
set _tags to {}
        
if _message's todo flag is not not flagged then copy "flagged" to end of _tags
        
if _message's forwarded then copy "forwarded" to end of _tags
        
if not _message's is read then copy "unread" to end of _tags
        
if _message's redirected then copy "redirected" to end of _tags
        
if _message's replied to then copy "replied" to end of _tags
        
set _categories to _message's category
        
repeat with _category in _categories
            
copy name of _category to end of _tags
        
end repeat
        
return _tags
    
end tell
end tagsFromMessage

on subjectFromMessage(_message)
    
tell application "Microsoft Outlook"
        
set _subject to subject of _message
        
if _subject is missing value or _subject is "" then
            
return "<no subject>"
        
else
            
return _subject
        
end if
    
end tell
end subjectFromMessage

on writeData(_path, _data)
    
set _file to POSIX file _path
    
set _fd to open for access _file with write permission
    
write _data to _fd
    
close access _fd
end writeData

on createTempFolder()
    
set _tempFolder to do shell script "mktemp -d -t 'EFImportOutlook'"
    
return _tempFolder
end createTempFolder

on makeFileName(_subject)
    
set _clean to my replace(_subject, "/", ":")
    
set _clean to my replace(_clean, ":", "-")
    
set _extension to ".eml"
    
set _hfsPlusLimit to 255
    
set _max to _hfsPlusLimit - (length of _extension)
    
set _shortened to my substringToIndex(_clean, _max)
    
return _shortened & _extension
end makeFileName

on substringToIndex(_string, _index)
    
if length of _string > _index then
        
set _end to _index
    
else
        
set _end to length of _string
    
end if
    
return my join(characters 1 thru _end of _string, "")
end substringToIndex

on replace(_string, _source, _replacement)
    
return my join(my split(_string, _source), _replacement)
end replace

on join(_list, _sep)
    
set _temp to AppleScript's text item delimiters
    
set AppleScript's text item delimiters to _sep
    
set _result to _list as string
    
set AppleScript's text item delimiters to _temp
    
return _result
end join

on split(_string, _sep)
    
set _temp to AppleScript's text item delimiters
    
set AppleScript's text item delimiters to _sep
    
set _result to text items of _string
    
set AppleScript's text item delimiters to _temp
    
return _result
end split