How to attach file to Outlook from terminal using Applescript

applescriptms officeterminal

I'm writing an Applescript so that I can attach a file to Outlook from the terminal in the following way:

$ attachToOutlook myreport.xlsx

Where attachToOutlook is aliased to osascript /path/to/my/script/attach

This is my current implementation:

on run argv
  tell application "Microsoft Outlook"
    set theContent to ""
    set theAttachment to item 1 of argv
    set theMessage to make new outgoing message with properties {subject: ""}
    tell content
      make new attachment with properties {file: (item 1 of argv )} at the end of theMessage
    end tell
    open theMessage -- for further editing
  end tell
end run

but I'm getting the following error:

attach:263:264: script error: Expected expression, etc. but found “:”. (-2741)

How can I fix this?

Best Answer

A couple of issues. First, you need to make the new attachment to the message, not to the content. Second, the attachment should be a posix file. Third, you can't just send a file name, you have to tell Outlook where the file is by including the full path.

I've written my own version included here, adding the subject and content as variables passed through the command line.

Note: Call with the following, replacing with the actual account name.

osascript testterm.scpt '/Users/<user name>/Desktop/test2.rtf' 'Test Subject' '<p>This is a test content for an email test</p>'

AppleScript code in testterm.scpt:

on run argv
    set theAttachment to item 1 of argv
    set theAttachment to theAttachment as POSIX file--convert to posix file
    set theSubject to item 2 of argv
    set theContent to item 3 of argv
    tell application "Microsoft Outlook"
        set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent}
        tell theMessage--tell theMessage (not theContent) to add the attachment
            make new attachment with properties {file:theAttachment}
        end tell
    open theMessage
    end tell
end run