Search code examples
outlookapplescript

Applescript: Save Selected Outlook Message as .eml File


Looking for some guidelines or tips on how to save a selected Outlook mail message as a .eml file on my Mac.

Is there an easy command for this or do I have to actually write the file using the contents from the selected message?


Solution

  • Hi this is an extract from a script I use.

    You just have to save the email to a file.

    It saves the message as a text file. The reason I do this is I can then Search the text file better with spotlight.

    If I open the text file in TextEdit I see the raw email. Not very readable. But I can actually drag it onto the Outlook icon in the Dock and it will open as a normal outlook email.

      set folderPath to ((path to home folder from user domain as string) & "MS_Emails") --PATH TO YOU FOLDER
    --TEST IF FOLDER EXISTS. IF NOT CREATE IT
    if (do shell script "/bin/test -e " & quoted form of (POSIX path of folderPath) & " ; echo $?") is "1" then
        -- 1 is false
        do shell script "/bin/mkdir -p " & quoted form of (POSIX path of folderPath)
    
    end if
    
    tell application "Microsoft Outlook"
        -- GET SELECTE EMAILS
        set messages_ to the selection
    
        --ITERATE THROUGH THEM AND SAVE THEM
        repeat with i from 1 to number of items in messages_
            set theMsg to item i of messages_
            set textPath to folderPath & "email.txt" as string
            save theMsg in (textPath)
    
        end repeat
    end tell
    

    If you change the extension to .eml instead of txt. Your default email app will open it when you double click on the file. In my case that will be Mail.app but yours maybe Outlook.

    Neither will have a problem reading the file

    You can use various methods to set a unique name to each file..