outlook Mac AppleScript to save attachments

2.5k Views Asked by At

I'm working with Outlook for Mac 15.30(161251)

I've a script that saves all attachments to a folder. It recently stopped working and I'm trying to figure out why.

The old save command save theAttachment in file savePath

gives Microsoft Outlook got an error: An error has occurred.

I changed to save theAttachment in POSIX file savePath

and now I'm getting Microsoft Outlook got an error: Parameter error.

I've checked the path and it seems to be fine.

Any thoughts?

2

There are 2 best solutions below

2
On

I can't see all your code (assuming there is more than these two lines) so I am unable to test it with your scenario.
However, I was able to find lots of forums with people having the same question.

Take a look at this code from this forum:

set saveToFolder to (choose folder with prompt "Choose the destination folder") as string
set prefix to the text returned of (display dialog "Enter the text to prefix the saved attachment names with" default answer "" buttons {"Cancel", "OK"} default button 2)
set ctr to 0

tell application "Microsoft Outlook"
    set topFolder to mail folder "inbox" of on my computer
    set srcFolder to mail folder "myfolder" of topFolder
    set destFolder to folder "myFolder2" of topFolder

    set selectedMessages to messages of srcFolder
    repeat with msg in selectedMessages
        set ctr to ctr + 1
        set attFiles to attachments of msg
        repeat with f in attFiles
            set attName to (get the name of f)
            log attName
            set saveAsName to saveToFolder & prefix & ctr & attName
            log saveAsName
            save f in saveAsName
        end repeat
        move msg to destFolder
    end repeat
end tell
display dialog "" & ctr & " messages were processed" buttons {"OK"} default button 1
return ctr
0
On

Here is a working example, including dealing with posix paths:

set saveToFolder to POSIX path of (choose folder with prompt "Choose the destination folder")
set ctr to 0
tell application "Microsoft Outlook"
    set srcFolder to mail folder "SomeFolder" of on my computer
    set selectedMessages to messages of srcFolder
        repeat with msg in selectedMessages

            set sentstamp to time sent of msg

            set y to year of sentstamp
            set m to month of sentstamp
            set d to day of sentstamp
            set rdate to y & "-" & m & "-" & d
            set ctr to ctr + 1

            set attFiles to attachments of msg
            set actr to 0
            repeat with f in attFiles
                set attName to (get the name of f)
                log attName
                set saveAsName to saveToFolder & "mrp-" & rdate & "-" & actr & ".csv"

                set actr to actr + 1

                save f in POSIX file saveAsName
            end repeat
        end repeat
end tell

display dialog "" & ctr & " messages were processed" buttons {"OK"} default button 1
return ctr