Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Outlook 2011 Attachment Remover AppleScript
-- AppleScript to remove attachment from selected messages in Outlook 2011
-- Incorporates code from these sources:
-- (for path2url function)
on path2url(thepath)
-- Needed to properly URL encode the path
return do shell script "python -c \"import urllib, sys; print (urllib.quote(sys.argv[1]))\" " & quoted form of thepath
end path2url
tell application "Microsoft Outlook"
-- Get currently selected message
tell application "Finder"
tell me to set folder_path to (choose folder with prompt "Select a folder to save the attachments") as text
on error number -128
-- User canceled the action
display dialog "No folder selected. Please rerun the script and select a folder." with icon 2
return number
-- Exit the script
end try
end tell
set selectedMessages to current messages
set attachmentSizes to 0
set fileDownloadedList to "Attachments Removed:<br>------------------------<br>"
if selectedMessages is {} then
display dialog "Please select a message first then run the script" with icon 1
end if
-- Check for attachments
repeat with theMessage in selectedMessages
-- Get the attachments
set attachmentlist to every attachment in theMessage
repeat with anAttachment in attachmentlist
set a to anAttachment
set attachmentSize to file size of a
set fileDownloaded to folder_path & name of a
-- Save the attachment
save a in folder_path & name of a
-- Log the file sizes and file list
set attachmentSizes to attachmentSizes + attachmentSize
set fileDownloadedURL to "file://localhost" & my path2url(POSIX path of fileDownloaded)
set fileDownloadedList to fileDownloadedList & "Attachment: <a href=\"" & fileDownloadedURL & "\">" & fileDownloaded & "</a><br>"
on error
exit repeat
end try
end repeat
set content of theMessage to fileDownloadedList & "<br>" & content of theMessage
-- Need to delete the attachments here, otherwise we throw index errors
-- Becase Outlook resets the list when deleting, need to just keep deleting the first
-- item each time until all are removed
set attachmentCount to count of attachmentlist
repeat (attachmentCount) times
delete item 1 of attachmentlist
end repeat
end repeat
end tell
Copy link

Nice Script, I made a minor mod...

7-31-13 updated the script so that running the script on multiple emails does not cause the attachment list to grow. Moved "repeat with theMessage in selectedMessages" to a better place.

Copy link

Great script! How do you set the attachment size limit to 100KB?
I'm trying to get only the attachments that are over a certain size to be removed.

Copy link

dpiro commented Jul 16, 2014

Works great -- thank you!

Copy link

cweirup commented Jan 4, 2017

If you want to only save and remove attachments above a certain size, you can use the "file size" property on the attachment object, which is an integer. Something like:

if file size of a is greater than 99999 then
     -- Process the file
end if

Hope that helps!

Copy link

Has anyone updated this script to work with Microsoft Outlook 2016?

Copy link

cweirup commented Mar 14, 2018

I have a new version of this script for Outlook 2016. Specifically, it handles the issues with saving files in the Sandbox. It does require the use of Hazel to accomplish this.

Outlook 2016 Attachment Remover

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment