Import From Apple Mail (Plain Text)

Summary: Imports the selected or rule-processed messages from Apple Mail as .txt files.
Requires: EagleFiler
Install Location: ~/Library/Application Scripts/com.apple.mail or ~/Library/Scripts/Applications/Mail/
Last Modified: 2021-02-08

Description

Note: This script creates a human-readable plain text file. To import the full message source (including attachments), you should instead use the Import From Apple Mail script.

The recommended way to import from Apple Mail 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:

Unlike when using the capture key, the unread/flagged status and MailTags are not preserved.

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

Script

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

on run
    
tell application "Mail"
        
if my isLionOrBetter() and (count of message viewers) is 1 then
            
-- If there's more than one, and full screen mode is active, we can't tell which is frontmost
            
return my importFromViewer(message viewer 1)
        
else
            
repeat with _viewer in message viewers
                
if index of _viewer's window is 1 then
                    
my importFromViewer(_viewer)
                
end if
            
end repeat
        
end if
    
end tell
end run

on isLionOrBetter()
    
considering numeric strings
        
return (system version of (system info)) ≥ "10.7"
    
end considering
end isLionOrBetter

using terms from application "Mail"
    
on perform mail action with messages _messages
        
my importMessages(_messages)
    
end perform mail action with messages
end using terms from

on importFromViewer(_viewer)
    
tell application "Mail"
        
set _messages to (get selected messages of _viewer)
        
repeat with _message in _messages
            
my importMessage(_message)
        
end repeat
    
end tell
end importFromViewer

on importMessage(_message)
    
tell application "Mail"
        
set _subject to subject of _message
        
set _from to _message's sender
        
set _date to _message's date sent
        
set _to to content of header "to" of _message
        
try
            
set _cc to content of header "cc" of _message
        
on error
            
set _cc to ""
        
end try
        
set _body to _message's content
        
set _messageID to _message's message id
    
end tell
    
set _text to "Subject: " & _subject & return
    
set _text to _text & "From: " & _from & return
    
set _text to _text & "Date: " & _date & return
    
set _text to _text & "To: " & _to & return
    
if _cc is not "" then
        
set _text to _text & "Cc: " & _cc & return
    
end if
    
set _text to _text & return & _body
    
tell application "EagleFiler"
        
set {_record} to import plain text _text
        
set _record's title to _subject
        
set _record's basename to _subject
        
set _record's from name to _from
        
set _record's modification date to _date
        
set _record's creation date to _date
        
set _record's source URL to my URLFromMessageID(_messageID)
    
end tell
end importMessage

on URLFromMessageID(_messageID)
    
set _components to current application's NSURLComponents's alloc's init
    
_components's setScheme:"message"
    
_components's setHost:("<" & _messageID & ">")
    
return _components's |URL|'s absoluteString() as Unicode text
end URLFromMessageID