0

My company recently switched from PCs to Macs. I am very new to AppleScript. I have been trying to cobble together a script to save selected emails from Outlook to a designated folder.

So far I have the following which gives a Syntax Error - Expected end of line, etc. but found an identifier.

-- Prompt the user to choose a folder to save the emails
set saveFolder to choose folder with prompt "Select a folder to save the emails"

tell application "Microsoft Outlook"
    -- Get the selected emails
    set selectedEmails to selected objects
    repeat with theEmail in selectedEmails
        -- Get the subject of the email
        set emailSubject to subject of theEmail
        
        -- Generate a unique file name for each email
        set timeStamp to do shell script "date +%Y%m%d%H%M%S"
        set fileName to emailSubject & "_" & timeStamp & ".eml"
        
        -- Save the email as an .eml file in the selected folder
        save theEmail in saveFolder as "eml" with name fileName
        
    end repeat
end tell

I also have tried the following code that runs with no errors but doesn't actually save anything to the prompted folder.

-- Prompt the user to choose a folder to save the emails
set saveFolder to choose folder with prompt "Select a folder to save the emails"

tell application "Microsoft Outlook"
    -- GET SELECTE EMAILS
    --set messages_ to the selection
    set selectedEmails to selected objects
    
    --ITERATE THROUGH THEM AND SAVE THEM
    repeat with theEmail in selectedEmails
        
        set theMsg to item i of selectedEmails
        
        -- Get the subject of the email
        set emailSubject to subject of theEmail
        
        -- Generate a unique file name for each email
        set timeStamp to do shell script "date +%Y%m%d%H%M%S"
        set fileName to emailSubject & "_" & timeStamp & ".eml"
        
        
        set textPath to saveFolder & fileName as string
        save theMsg in (textPath)
        
    end repeat
end tell

I have tried different iterations of the two and don't seem to be making any progress.

Can anyone point me in the right direction? I hate to say but I kinda miss VBA.

1 Answer 1

1

(I'm not where I can test this atm... and don't use Outlook. So this should be interpreted as general commentary for Applescript)

Your "save theEmail" line in the first example is not right. fileName should be the complete pathname for the file you want the email saved in.

ref Outlook AS dictionary:

save v : Save an object.

save specifier : the object to save, usually a document or window

[in file] : The file in which to save the object.

[as text] : The file type in which to save the data.

Your second example doesn't specify the path correctly, you are not adding a path separator between the path specification and the filename.

I'd probably use POSIX path too.

1
  • I made some progress. How do I add a reply with the new code? I have one script that will save the first email with the file name I want (date stamp included) but then errors out. I have another script that will save all selected emails but doesn't include the date stamp in the file name. Also, the scripts only run if I switch to the old Outlook interface. The new interface doesn't appear to allow scripts to run yet.
    – LVG8R
    May 21, 2023 at 17:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .